Lychrel numbers: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
|||
Line 2,919: | Line 2,919: | ||
3 lychrel palindromes: 4994, 8778, 9999 |
3 lychrel palindromes: 4994, 8778, 9999 |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
<lang Picat>go => |
|||
Limit = 500, |
|||
Lychrel = [], |
|||
Cache = new_set(), % caching the found Lychrel numbers |
|||
Seeds = [], |
|||
Palindromes = [], |
|||
foreach(N in 1..10_000) |
|||
if lychrel(N,Cache,Limit,Seed) then |
|||
Lychrel := Lychrel ++ [N], |
|||
if Seed then Seeds := Seeds ++ [N] end, |
|||
if rev(N) == N then Palindromes := Palindromes ++ [N] end |
|||
end |
|||
end, |
|||
println(num_lychrel_numbers=Seeds.len), |
|||
println(seeds=Seeds), |
|||
println(palindromes=Palindromes), |
|||
println(num_palindromes=Palindromes.len), |
|||
println(num_related=(Lychrel.len-Seeds.len)), |
|||
nl. |
|||
table |
|||
rev(N) = N.to_string().reverse().to_int(). |
|||
lychrel(N,Cache,Limit,Seed) => |
|||
Found = false, % true if we found a non Lychrel number |
|||
Seq = [N], |
|||
Seed1 = false, |
|||
OldHit = cond(Cache.has_key(N),true,false), % found a Lychrel number from cache |
|||
I = 0, |
|||
while (OldHit == false,Found == false,I <= Limit) |
|||
I := I+1, |
|||
N := N + rev(N), |
|||
if Cache.has_key(N) then |
|||
OldHit := true |
|||
else |
|||
Seq := Seq ++ [N], |
|||
if N == rev(N) then Found := true end |
|||
end |
|||
end, |
|||
if Found == false, OldHit == false then |
|||
Seed1 := true, % this is a seed, so add all elements to the cache |
|||
foreach(S in Seq) Cache.put(S) end |
|||
end, |
|||
Seed = Seed1, |
|||
% The check: have we found a Lyncrel number |
|||
(OldHit == true ; Found == false).</lang> |
|||
Output: |
|||
<pre>num_lychrel_numbers = 5 |
|||
seeds = [196,879,1997,7059,9999] |
|||
palindromes = [4994,8778,9999] |
|||
num_palindromes = 3 |
|||
num_related = 244</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |