Perfect numbers: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, changed comments in various header and output sections.
m (→‎Lucas-Lehmer + other optimizations: changed indentations in the isPerfect function.)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed comments in various header and output sections.)
Line 1,690:
 
===Classic REXX version of PL/I===
This version is a Classic REXX version of the PL/I program as of 14-Sep-2013,   a REXX   '''say'''   statement was added to display the perfect numbers.   Also, an epilog was written for the re-written function.
<br>was added to display the perfect numbers. &nbsp; Also, an epilog was written for the re-worked function.
<lang rexx>/*REXX version of the PL/I program (code was modified to run with Classic REXX). */
parse arg low high . /*obtain the specified number(s).*/
Line 1,714 ⟶ 1,715:
===traditional method===
Programming note: &nbsp; this traditional method takes advantage of a few shortcuts:
:::* testing only goes up to the (integer) square root of &nbsp; '''X'''
:::* testing bypasses the test of the first and last factors
:::* athe ''corresponding factor'' is used when a factor is found
:::* testing is stopped if the sum of the factors exceeds &nbsp; '''X'''
<lang rexx>/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain optional arguments from the CL*/
Line 1,741 ⟶ 1,742:
return s==x /*if the sum matches X, it's perfect! */</lang>
'''output''' &nbsp; when using the default inputs:
<pre>
6 is a perfect number.
28 is a perfect number.
Line 1,747 ⟶ 1,749:
33550336 is a perfect number.
</pre>
For 10,000 numbers tested, this version is &nbsp; '''19.6''' &nbsp; times faster than the ooRexx program logic.<br>
For 10,000 numbers tested, this version is &nbsp; '''25.6''' &nbsp; times faster than the &nbsp; PL/I &nbsp; program logic.
<br><br>Note: &nbsp; For the above timings, only 10,000 numbers were tested.
 
===optimized using digital root===
This REXX version makes use of the fact that all &nbsp; ''known'' &nbsp; perfect numbers > 6 have a &nbsp; ''digital root'' &nbsp; of &nbsp; '''1'''.
<lang rexx>/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain the specified number(s). */
Line 1,782 ⟶ 1,784:
end /*j*/ /*(above) is marginally faster. */
return s==x /*if the sum matches X, it's perfect! */</lang>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about &nbsp; '''5.3''' &nbsp; times faster &nbsp; (testing 34,000,000 numbers).
 
===optimized using only even numbers===
Line 1,816 ⟶ 1,818:
end /*j*/ /*(above) is marginally faster. */
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 &nbsp; '''11.5''' &nbsp; times faster &nbsp; (testing 34,000,000 numbers).
 
===Lucas-Lehmer method===
This version uses memoization to implement a fast version of the Lucas-Lehmer test,
<br>itand isit's faster than the traditional method by an order of magnitude.
<lang rexx>/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain the optional arguments from CL*/
Line 1,855 ⟶ 1,857:
end /*j*/ /*(above) is marginally faster. */
return s==x /*if the sum matches X, it's perfect!*/</lang>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about &nbsp; '''75''' &nbsp; times faster &nbsp; (testing 34,000,000 numbers).
 
===Lucas-Lehmer + other optimizations===