Anonymous user
Hailstone sequence: Difference between revisions
→{{header|REXX}}: changed indentation, comments, whitespace, allowed specification of 1st and 2nd arguments, corrected program to handle less than 4 numbers in sequence for displaying. -- ~~~~
(→{{header|REXX}}: changed indentation, comments, whitespace, allowed specification of 1st and 2nd arguments, corrected program to handle less than 4 numbers in sequence for displaying. -- ~~~~) |
|||
Line 3,816:
=={{header|REXX}}==
===non-optimized===
<lang REXX>/*REXX pgm tests a number and a range for hailstone (Collatz) sequences.*/
parse arg x y .
if y=='' | y==',' then y=100000-1 /*Any 2nd argument? Use default.*/
numeric digits 20; @.=0 /*handle big #s; initialize array*/
$=hailstone(x) /*═══════════════════task 1═════════════════════════*/
say ' and starts with: ' subword($,1,4) " ∙∙∙"▼
say
▲say ' and starts with: ' subword($, 1, 4) " ∙∙∙"
say ' and ends with: ∙∙∙' subword($, max(1, words($)-3))
say
if y==0 then exit /*═══════════════════task 2═════════════════════════*/
w=0; do j=1 for y /*traipse through the numbers. */
call hailstone j /*compute the hailstone sequence.*/
if #hs<=w then iterate /*Not big 'nuff? Then keep going.*/
bigJ=j; w=#hs /*remember what # has biggest HS.*/
end /*j*/
▲say '(between 1──►99,999) ' bigJ " has the longest hailstone sequence:" w
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HAILSTONE subroutine────────────────*/
Line 3,847 ⟶ 3,850:
and ends with: ∙∙∙ 8 4 2 1
(between
</pre>
|