How to resolve the algorithm Happy numbers step by step in the Objeck programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Happy numbers step by step in the Objeck 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 Objeck programming language
Source code in the objeck programming language
use IO;
use Structure;
bundle Default {
class HappyNumbers {
function : native : IsHappy(n : Int) ~ Bool {
cache := IntVector->New();
sum := 0;
while(n <> 1) {
if(cache->Has(n)) {
return false;
};
cache->AddBack(n);
while(n <> 0) {
digit := n % 10;
sum += (digit * digit);
n /= 10;
};
n := sum;
sum := 0;
};
return true;
}
function : Main(args : String[]) ~ Nil {
num := 1;
happynums := IntVector->New();
while(happynums->Size() < 8) {
if(IsHappy(num)) {
happynums->AddBack(num);
};
num += 1;
};
Console->Print("First 8 happy numbers: ");
each(i : happynums) {
Console->Print(happynums->Get(i))->Print(",");
};
Console->PrintLine("");
}
}
}
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Dart programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the D programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Nanoquery programming language