Anonymous user
Perfect numbers: Difference between revisions
m
→optimized using only even numbers: added/changed comments and whitespace.
m (→traditional method: added/changed comments and whitespace.) |
m (→optimized using only even numbers: added/changed comments and whitespace.) |
||
Line 1,792:
===optimized using only even numbers===
This REXX version uses the fact that all ''known'' perfect numbers are ''even''.
<lang rexx>/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain
if high=='' & low=='' then high=34000000 /*if no
if low=='' then low=1 /*if no LOW, then assume unity. */
if high=='' then high=low /*if no HIGH, then assume LOW. */
w=length(high) /*use W for formatting the output. */
numeric digits max(9,w+2) /*ensure enough digits to handle
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isPerfect: procedure; parse arg x 1 y /*
if x==6 then return 1 /*handle the special case of six. */
if r\==1 then return 0 /*
s=3 + x%2 /*the first 3 factors of X. ___*/
if s>x
return s==x
'''output''' is the same as the traditional version and is about '''11.5''' times faster (testing 34,000,000 numbers).
|