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 x=='' then x=27 /*get the optional firstarguments argumentfrom CL.*/
numeric digits 20 if x=='' | x==',' then x=27 /*beAny able1st toargument? handle largerUse numsdefault. */
if y=='' | y==',' then y=100000-1 /*Any 2nd argument? Use default.*/
$=hailstone(x) /*═════════════task 1════════════*/
numeric digits 20; @.=0 /*handle big #s; initialize array*/
say x ' has a hailstone sequence of ' #hs
$=hailstone(x) /*═══════════════════task 1═════════════════════════*/
say ' and starts with: ' subword($,1,4) " ∙∙∙"
say 'x ' has anda hailstone endssequence of with:' ∙∙∙' subwordwords($,#hs-3)
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 99999 /*═════════════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,9991──►'y") '" bigJ " ' has the longest hailstone sequence:"' w
 
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 1──►99,9991──►99999) 77031 has the longest hailstone sequence: 351
</pre>