28 lines
1.3 KiB
Text
28 lines
1.3 KiB
Text
/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/
|
|
limit = arg[0] /*get argument for LIMIT. */
|
|
say limit
|
|
if limit = null, limit ='' then limit=8 /*if not specified, set LIMIT to 8*/
|
|
haps = 0 /*count of happy numbers so far. */
|
|
|
|
loop n=1 while haps < limit /*search integers starting at one.*/
|
|
q=n /*Q may or may not be "happy". */
|
|
a=0
|
|
|
|
loop forever /*see if Q is a happy number. */
|
|
if q==1 then do /*if Q is unity, then it's happy*/
|
|
haps = haps + 1 /*bump the count of happy numbers.*/
|
|
say n /*display the number. */
|
|
iterate n /*and then keep looking for more. */
|
|
end
|
|
|
|
sum=0 /*initialize sum to zero. */
|
|
|
|
loop j=1 for q.length /*add the squares of the numerals.*/
|
|
sum = sum + q.substr(j,1) ** 2
|
|
end
|
|
|
|
if a[sum] then iterate n /*if already summed, Q is unhappy.*/
|
|
a[sum]=1 /*mark the sum as being found. */
|
|
q=sum /*now, lets try the Q sum. */
|
|
end
|
|
end
|