How to resolve the algorithm Happy numbers step by step in the Seed7 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Happy numbers step by step in the Seed7 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 Seed7 programming language
Source code in the seed7 programming language
$ include "seed7_05.s7i";
const type: cacheType is hash [integer] boolean;
var cacheType: cache is cacheType.value;
const func boolean: happy (in var integer: number) is func
result
var boolean: isHappy is FALSE;
local
var bitset: cycle is bitset.value;
var integer: newnumber is 0;
var integer: cycleNum is 0;
begin
while number > 1 and number not in cycle do
if number in cache then
number := ord(cache[number]);
else
incl(cycle, number);
newnumber := 0;
while number > 0 do
newnumber +:= (number rem 10) ** 2;
number := number div 10;
end while;
number := newnumber;
end if;
end while;
isHappy := number = 1;
for cycleNum range cycle do
cache @:= [cycleNum] isHappy;
end for;
end func;
const proc: main is func
local
var integer: number is 0;
begin
for number range 1 to 50 do
if happy(number) then
writeln(number);
end if;
end for;
end func;
You may also check:How to resolve the algorithm Pascal's triangle/Puzzle step by step in the MiniZinc programming language
You may also check:How to resolve the algorithm Host introspection step by step in the Tcl programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the jq programming language
You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the jq programming language