How to resolve the algorithm Happy numbers step by step in the NetRexx programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Happy numbers step by step in the NetRexx programming language

Table of Contents

Problem Statement

From Wikipedia, the free encyclopedia:

Find and print the first   8   happy numbers. Display an example of your output here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Happy numbers step by step in the NetRexx programming language

Source code in the netrexx programming language

/*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

  

You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Nim programming language
You may also check:How to resolve the algorithm Boolean values step by step in the jq programming language
You may also check:How to resolve the algorithm Kronecker product based fractals step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the VBA programming language
You may also check:How to resolve the algorithm Classes step by step in the Fantom programming language