Actually, I did it a bit more intelligently than that. Instead of a for-loop the main loop is a while loop runs so long as the temporary number is greater than 0. I then find the factor of that number (n) and push the factor, then divide the number. At the end if there is a leftover 2 in the number I just push it to the end and make n 0.
So in the end my loop looks like this:[pascal] while n > 0 do
if (n = 2) or (IsPrimeRM(n)) then begin
pushValue(Result,n);
n := 0;
end else begin
for i := (n div 2) downto 2 do
if IsPrimeRM(i) then
if n mod i = 0 then begin
pushValue(Result,i);
if n <> i then
n := n div i
else
n := 0;
end;
end;[/pascal]
The 'if n <> i then' part came into play because I was getting an end duplicate factor because it would then decrement the remainder to 1 and loop infinitely. So that helps bust the loop.
In the end, my results are accurate and it was a good learning experience at figuring out better function design (ha! What a stack overflow won't teach you...).
If someone wants the code I'll post it, otherwise you're better off writing it yourself.
Code:
Enter a number from 4 to 65535 and one number in specific to look for separated
by a space. Looking for 0 means just factor the number. Two zero's exits.
x = 45670 5
factors: 5 x 9134
sum of parts: 9139
factors of sum of parts: 2 x 3 x 5 x 7 x 11 x 13 x 19 x 37
x = 45670 0
factors: 2 x 5 x 4567
sum of parts: 4574
factors of sum of parts: 2 x 2287
x = 65470
0
factors: 2 x 5 x 6547
sum of parts: 6554
factors of sum of parts: 2 x 29 x 113
x =
Bookmarks