Perfect numbers: Difference between revisions

m
→‎optimized using only even numbers: changed indentations for the ISPERFECT function.
m (→‎Lucas-Lehmer method: added/changed comments and whitespace.)
m (→‎optimized using only even numbers: changed indentations for the ISPERFECT function.)
Line 1,808:
/*──────────────────────────────────────────────────────────────────────────────────────*/
isPerfect: procedure; parse arg x 1 y /*obtain the number to be tested. */
if x==6 then return 1 if x==6 then return 1 /*handle the special case of six. */
 
do until y<10 do until y<10 /*find the digital root of Y. */
parse var y 1 r 2; do k=2 for length(y)-1; r=r+substr(y,k,1); end /*k*/
y=r y=r /*find digital root of the digital root*/
end /*until*/ end /*until*/ /*wash, rinse, repeat ··· */
 
if r\==1 then return 0 if r\==1 then return 0 /*Digital root ¬ 1 ? Then ¬ perfect.*/
s=3 + x%2 s=3 + x%2 /*the first 3 factors of X. ___*/
do j=3 while j*j<=x /*starting at 3, find the factors ≤√ X */
if x//j\==0 then iterate /*J isn't a factor o f X, so skip it.*/
s = s + j + x%j s = s + j + x%j /* ··· add it and the other factor. */
if s>x then return 0 if s>x then return 0 /*Is the sum too big? It ain't perfect*/
end /*j*/ end /*j*/ /*(above) is marginally faster. */
return s==x return s==x /*if sum matches X, then it's perfect!*/</lang>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about '''11.5''' times faster &nbsp; (testing 34,000,000 numbers).