Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Happy-numbers/Elena/happy-numbers.elena
Normal file
44
Task/Happy-numbers/Elena/happy-numbers.elena
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import extensions;
|
||||
import system'collections;
|
||||
import system'routines;
|
||||
|
||||
isHappy(int n)
|
||||
{
|
||||
auto cache := new List<int>(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<int>(8);
|
||||
int num := 1;
|
||||
while (happynums.Length < 8)
|
||||
{
|
||||
if (isHappy(num))
|
||||
{
|
||||
happynums.append(num)
|
||||
};
|
||||
|
||||
num += 1
|
||||
};
|
||||
console.printLine("First 8 happy numbers: ", happynums.asEnumerable())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue