mercredi 11 juillet 2018

Prime Factorisation Loop in SAS

I am working on little tasks to improve my coding and efficiency, the problem I'm working on today is from project Euler, problem 3:

"Find the largest Prime of 600851475143"

the code I have written is:

data test;

a = 600851475143;
/*The subsequent a's are the next parts of the loop I'm trying to incorporate*/
/*a = 8462696833;*/
/*a = 10086647;*/
/*a = 6857;*/

if mod(a,2) = 0 then do;

a = a / 2;

end;

else do;

    do i = 3 to a until(flag);

        if mod(a,i) = 0 and i < a then do;

            b = i ;
            a = a / b ;
            flag = 1;
            output;

        end;
    end;

end;

run;

How do I make the variable a loop and get smaller and then terminate when there is no more a, i.e. the last iteration does not produce a dataset because there is no factorisation.

I am also happy to receive any tips on how to make this code more efficient because I am trying to learn

Aucun commentaire:

Enregistrer un commentaire