Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 329206

Submitted on 4 Oct 2013 by Alfonso Nieto-Castanon

Correct

27Size
Leading solution size is 25.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%% Clean workspace
!/bin/cp two_fractions.m safe
!/bin/rm *.*
!/bin/mv safe two_fractions.m
% Clean user's function from some known jailbreaking mechanisms
functions={'!','feval','eval','str2func','str2num','regex','system','dos','unix','perl','assert','fopen','write','save','setenv','path','please'};
fid = fopen('two_fractions.m');
  st = char(fread(fid)');
  for n = 1:numel(functions)
    st = regexprep(st, functions{n}, 'error(''No fancy functions!''); %','ignorecase');
  end
fclose(fid)
ans =
     0
2
Pass
 
%%
x = 1; y_correct = 1; assert(isequal(two_fractions(x),y_correct))

                    
3
Pass
 
%%
x = 10; y_correct = 5; assert(isequal(two_fractions(x),y_correct))

                    
4
Pass
 
%%
x = 120; y_correct = 32; assert(isequal(two_fractions(x),y_correct))

                    
5
Pass
 
%%
x = 9240; y_correct = 284; assert(isequal(two_fractions(x),y_correct))

                    
6
Pass
 
%%
x = 510510; y_correct = 1094; assert(isequal(two_fractions(x),y_correct))

                    
7
Pass
 
%%
x = 223092869; y_correct = 14; assert(isequal(two_fractions(x),y_correct))

                    
8
Pass
 
%%
x = 223092870; y_correct = 9842; assert(isequal(two_fractions(x),y_correct))

                    
9
Pass
 
%%
x = 223092871; y_correct = 5; assert(isequal(two_fractions(x),y_correct))

                    
10
Pass
 
%%
assert(isequal(two_fractions(two_fractions(10^7)+two_fractions(10^8)),14))

                    
11
Pass
 
%%
tic
satf=sum(arrayfun(@(x) two_fractions(x),1:10000));
assert(isequal(satf,186991))
toc
Elapsed time is 2.008717 seconds.