hailstone_demo: proc options (main); %replace true by '1'b, false by '0'b; dcl (slen, longest) fixed bin(15), (n, n_longest,limit) fixed decimal(12), answer char(1); put skip list ('Display hailstone sequence for what number? '); get list (n); slen = hailstone(n, true); put skip list ('Sequence length = ', slen); put skip(2) list ('Search for longest sequence (y/n)? '); get list (answer); if ((answer ^= 'y') & (answer ^= 'Y')) then stop; put list ('Search to what limit? '); get list (limit); longest = 1; n = 2; do while (n < limit); slen = hailstone(n, false); if slen > longest then do; longest = slen; n_longest = n; end; n = n + 1; end; put skip edit ('Longest sequence =',longest,' for n =',n_longest) (a,f(4),a,f(6)); /* compute, and optionally display, hailstone sequence for n */ hailstone: procedure(n, show) returns (fixed binary); dcl (len, col) fixed binary, (n, k) fixed decimal(12), show bit(1); /* make local copy since n is passed by reference */ k = n; col = 1; len = 1; do while ((k ^= 1) & (k > 0)); if (show) then /* print 8 columns across */ do; put edit (k) (f(8)); col = col + 1; if col > 8 then do; put skip; col = 1; end; end; if (mod(k,2) = 0) then k = k / 2; else k = k * 3 + 1; len = len + 1; end; if (show) then put edit (k) (f(8)); return (len); end hailstone; end hailstone_demo;