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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Happy numbers step by step in the Elena 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 Elena programming language

Source code in the elena programming language

import extensions;
import system'collections;
import system'routines;
 
isHappy(int n)
{
    auto cache := new List(5);
    int sum := 0;
    int num := n;
    while (num != 1)
    {
        if (cache.indexOfElement:num != -1)
        {
            ^ false
        };
        cache.append(num);
        while (num != 0)
        {
            int digit := num.mod:10;
            sum += (digit*digit);
            num /= 10
        };
        num := sum;
        sum := 0
    };
 
    ^ true
}
 
public program()
{
    auto happynums  := new List(8);
    int num := 1;
    while (happynums.Length < 8)
    {
        if (isHappy(num))
        {
            happynums.append(num)
        };
 
        num += 1
    };
    console.printLine("First 8 happy numbers: ", happynums.asEnumerable())
}

  

You may also check:How to resolve the algorithm Loops/Continue step by step in the Sather programming language
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the jq programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the ooRexx programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Clojure programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the Swift programming language