Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Happy-numbers/XPL0/happy-numbers.xpl0
Normal file
45
Task/Happy-numbers/XPL0/happy-numbers.xpl0
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
int List(810); \list of numbers in a cycle
|
||||
int Inx; \index for List
|
||||
include c:\cxpl\codes;
|
||||
|
||||
|
||||
func HadNum(N); \Return 'true' if number N is in the List
|
||||
int N;
|
||||
int I;
|
||||
[for I:= 0 to Inx-1 do
|
||||
if N = List(I) then return true;
|
||||
return false;
|
||||
]; \HadNum
|
||||
|
||||
|
||||
func SqDigits(N); \Return the sum of the squares of the digits of N
|
||||
int N;
|
||||
int S, D;
|
||||
[S:= 0;
|
||||
while N do
|
||||
[N:= N/10;
|
||||
D:= rem(0);
|
||||
S:= S + D*D;
|
||||
];
|
||||
return S;
|
||||
]; \SqDigits
|
||||
|
||||
|
||||
int N0, N, C;
|
||||
[N0:= 0; \starting number
|
||||
C:= 0; \initialize happy (starting) number counter
|
||||
repeat N:= N0;
|
||||
Inx:= 0; \reset List index
|
||||
loop [N:= SqDigits(N);
|
||||
if N = 1 then \happy number
|
||||
[IntOut(0, N0); CrLf(0);
|
||||
C:= C+1;
|
||||
quit;
|
||||
];
|
||||
if HadNum(N) then quit; \if unhappy number then quit
|
||||
List(Inx):= N; \if neither, add it to the List
|
||||
Inx:= Inx+1; \ and continue the cycle
|
||||
];
|
||||
N0:= N0+1; \next starting number
|
||||
until C=8; \done when 8 happy numbers have been found
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue