function factorthis(n) % help for factorthis(n) % copyright 1998, D. Estep % % PURPOSE % % this function computes all the integer factors of the input integer n. % % FUNCTION STATEMENT % % factorthis(n) % % REQUIRED VARIABLES % % n % % USAGE % % >>factorthis(integer) % Author: Don Estep % Date: 11/24/98 % first check to make sure that n is an integer. we use the % floor command, which rounds down to the nearest integer if (floor(n) ~= n) % the comparision ~= means not equal disp('input integers please!') break % this break will halt execution of the function end disp('now factoring') m = floor(sqrt(n)); % m is the largest integer we have to test for i = 1: m j = floor(n/i); % divide n by i and then round down to get j if (j*i == n) % j*i = n means a perfect factor fprintf('found factors %d and %d\n',i,j) % We use a more sophisticated print statement. % fprintf( ) prints what is inside the ( ). % In this case we say to print the set of characters % found factors and % the "\n" means go to the next line % We use %d to denote the location and format of the numbers to % be printed. For each %d% we should follow with a variable. % Here we insert the value of i for the first %d and j for the second. % %10.5e would print the numbers in exponential format using % 10 digits total with 5 following the decimal point % %10.5f prints the numbers in floating format using 10 digits % total and 5 following the decimal point end end %output %>>factorthis(10203032100) %now factoring %found factors 1 and 1.020303e+010 %found factors 2 and 5.101516e+009 %found factors 3 and 3.401011e+009 %found factors 4 and 2.550758e+009 %found factors 5 and 2040606420 %found factors 6 and 1700505350 %found factors 10 and 1020303210 %found factors 12 and 850252675 %found factors 15 and 680202140 %found factors 20 and 510151605 %found factors 25 and 408121284 %found factors 30 and 340101070 %found factors 50 and 204060642 %found factors 60 and 170050535 %found factors 75 and 136040428 %found factors 100 and 102030321 %found factors 150 and 68020214 %found factors 300 and 34010107