Lychrel numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Picat}}: Added {{out}})
m (syntax highlighting fixup automation)
Line 76:
{{trans|D}}
 
<langsyntaxhighlight lang="11l">F rev(n)
R BigInt(reversed(String(n)))
 
Line 118:
print(seeds.len‘ Lychrel seeds: ’seeds)
print(related.len‘ Lychrel related’)
print(palin.len‘ Lychrel palindromes: ’palin)</langsyntaxhighlight>
 
{{out}}
Line 131:
<br>
Uses the associative array from the Associative array iteration task.
<langsyntaxhighlight lang="algol68">PR read "aArray.a68" PR
 
# number of additions to attempt before deciding the number is Lychrel #
Line 298:
OD;
print( ( newline ) )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 307:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on Lychrels(numberLimit, iterationLimit)
script o
property digits : missing value -- Digits of the current startNumber or a derived sum.
Line 439:
end task
 
task()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Lychrel numbers between 1 and 10000:
 
5 seed Lychrels: 196, 879, 1997, 7059, 9999
244 related Lychrels
3 palindromic Lychrels: 4994, 8778, 9999"</langsyntaxhighlight>
 
=={{header|BASIC}}==
Line 457:
 
I highly recommend to run this code in QB64. If this code is run in the other related versions, it can take about 20 minutes to run. In QB64 it only takes just seconds (11 seconds in my computer). This code has been tested to calculate up to 15000 numbers. BASIC doesn't have a way to manage such large numbers, but I implemented a way to do the sum through strings (an easy way to create a kind of array).
<langsyntaxhighlight lang="qbasic">
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' Lychrel Numbers V1.0 '
Line 783:
 
END FUNCTION
</syntaxhighlight>
</lang>
 
Output while running:
Line 815:
{{trans|D}}
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <vector>
Line 882:
print_vector(palindromes);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 893:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(ns lychrel.core
(require [clojure.string :as s])
(require [clojure.set :as set-op])
Line 968:
(println count-palindromes "Lychrel palindromes found:" palindrom-n))
)
</syntaxhighlight>
</lang>
 
{{out}}
Line 979:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun Lychrel (number &optional (max 500))
"Returns T if number is a candidate Lychrel (up to max iterations), and a second value with the sequence of sums"
(do* ((n number (+ n (parse-integer rev-str)))
Line 1,005:
Number of related found: ~D~%Palyndrome Lychrel numbers: ~D => ~a~%"
n max (length seeds) (nreverse (mapcar #'car seeds)) related (length palyndromes) (nreverse palyndromes)) ))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,020:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">require "set"
require "big"
Line 1,072:
pals = (lychrel + l_related).select{|x| palindrome?(x)}.sort
puts " Number of Lychrel palindromes: #{pals.size}"
puts " Lychrel palindromes: #{pals}"</langsyntaxhighlight>
 
{{out}}
Line 1,086:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.conv, std.range, std.typecons, std.bigInt;
 
auto rev(in BigInt n) {
Line 1,136:
writeln(related.length, " Lychrel related");
writeln(palin.length, " Lychrel palindromes: ", palin);
}</langsyntaxhighlight>
{{out}}
<pre>5 Lychrel seeds: [196, 879, 1997, 7059, 9999]
Line 1,160:
The growth of the numbers was impressive, so a simple investigation: what of the growth per step for 196? The digit strings lengthened in a linear-looking way, so, what of a plot of step as x, vs Log10(n) as y? The result for 500 numbers went from step 0 and Log10(196) = 2·292256 to step 499 and 215·266386 and the linear correlation coefficient came out as 1·000 with y = 0·43328*x + 2·1616, which is to say that each advance increased the number by a factor of about 2·7. However, the plot (which alas, can't be posted here) does ''not'' show a random scatter about the straight line, it instead shows the points wobbling above and below the fitted line. A plot of the residuals shows the deviations coming in waves, but also cannot be posted here...
 
<syntaxhighlight lang="fortran">
<lang Fortran>
SUBROUTINE CROAK(GASP) !A dying message.
CHARACTER*(*) GASP !The message.
Line 1,552:
WRITE (6,*) "Unused STASH entries =",AVAILS(0)
END
</syntaxhighlight>
</lang>
 
Output: edited to put twenty numbers per line and omit the (1:''n'') of the output from BIGWRITE...
Line 1,589:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 13-09-2015
' compile with: fbc -s console
 
Line 1,682:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> Testing numbers: 1 to 10000
Line 1,694:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,824:
fmt.Println("Number of Lychrel palindromes:", len(pals))
fmt.Println(" Lychrel palindromes:", pals)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,836:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">module Main where
 
import Data.List
Line 1,885:
putStrLn $ show palindromicLychrel ++ " are palindromic Lychrel numbers."
putStrLn $ show lychrelSeeds ++ " are Lychrel seeds."
putStrLn $ "There are " ++ show relatedCount ++ " related Lychrel numbers."</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,898:
Reverse digits:
 
<langsyntaxhighlight Jlang="j">revdig=:('x',~|.)&.":"0</langsyntaxhighlight>
 
Note that we need extended precision numbers because 500 iterations gets us into very large numbers even when we start small. For example, starting from 12:
 
<langsyntaxhighlight Jlang="j"> (+revdig)^:500(12)
989330226271793404132120335101444022368928728236373385750622261099268167362912850818170039990942038798808908830140199820181718948328173760873880172226156484473632718819962221534191534021132404387162721132989</langsyntaxhighlight>
 
Test whether a number is a lychrel (true or related) number within that 500 iteration limit:
 
<langsyntaxhighlight Jlang="j">lychrel500=: (~: revdig)@((+^:~: revdig)^:500)@(+revdig)"0</langsyntaxhighlight>
 
Number of these lychrel numbers not exceeding 10000 (we start from 0 here, because that's natural for J's primitives, so we need 10001 integers if we are to reach 10000):
 
<langsyntaxhighlight Jlang="j"> #L=:I.lychrel500 i.10001
249</langsyntaxhighlight>
 
(What are they? Note that this isn't a task requirement - just curiosity.)
 
<langsyntaxhighlight Jlang="j"> 3 83$L
196 295 394 493 592 689 691 788 790 879 887 978 986 1495 1497 1585 1587 1675 1677 1765 1767 1855 1857 1945 1947 1997 2494 2496 2584 2586 2674 2676 2764 2766 2854 2856 2944 2946 2996 3493 3495 3583 3585 3673 3675 3763 3765 3853 3855 3943 3945 3995 4079 4169 4259 4349 4439 4492 4494 4529 4582 4584 4619 4672 4674 4709 4762 4764 4799 4852 4854 4889 4942 4944 4979 4994 5078 5168 5258 5348 5438 5491 5493
5528 5581 5583 5618 5671 5673 5708 5761 5763 5798 5851 5853 5888 5941 5943 5978 5993 6077 6167 6257 6347 6437 6490 6492 6527 6580 6582 6617 6670 6672 6707 6760 6762 6797 6850 6852 6887 6940 6942 6977 6992 7059 7076 7149 7166 7239 7256 7329 7346 7419 7436 7491 7509 7526 7581 7599 7616 7671 7689 7706 7761 7779 7796 7851 7869 7886 7941 7959 7976 7991 8058 8075 8079 8089 8148 8165 8169 8179 8238 8255 8259 8269 8328
8345 8349 8359 8418 8435 8439 8449 8490 8508 8525 8529 8539 8580 8598 8615 8619 8629 8670 8688 8705 8709 8719 8760 8778 8795 8799 8809 8850 8868 8885 8889 8899 8940 8958 8975 8979 8989 8990 9057 9074 9078 9088 9147 9164 9168 9178 9237 9254 9258 9268 9327 9344 9348 9358 9417 9434 9438 9448 9507 9524 9528 9538 9597 9614 9618 9628 9687 9704 9708 9718 9777 9794 9798 9808 9867 9884 9888 9898 9957 9974 9978 9988 9999</langsyntaxhighlight>
 
To figure out which of these lychrel numbers were "true" lychrel numbers, we will need to work with sequences of sums seeded from these numbers. So let's just find those sequences:
 
<langsyntaxhighlight Jlang="j"> S=:(+revdig)^:(i.501)"0 L</langsyntaxhighlight>
 
And, let's take a peek at some of the numbers in these sequences (the first 10 values in each lychrel sequence, starting from the first lychrel candidate seeds):
 
<langsyntaxhighlight Jlang="j"> 10 10 {.S
196 887 1675 7436 13783 52514 94039 187088 1067869 10755470
295 887 1675 7436 13783 52514 94039 187088 1067869 10755470
Line 1,937:
788 1675 7436 13783 52514 94039 187088 1067869 10755470 18211171
790 887 1675 7436 13783 52514 94039 187088 1067869 10755470
879 1857 9438 17787 96558 182127 903408 1707717 8884788 17759676</langsyntaxhighlight>
 
So most of these are related... which ones are not? (An integer is owned if it is in the sequence for this lychrel candidate or for any previous candidates. A lychrel candidate is a true lychrel number if none of its sequence is owned by any previous candidate.)
 
<langsyntaxhighlight Jlang="j"> owned=: <@~.@,\S
#T=: (-. (<"1 S) +./@e.&> a:,}:owned)#L
5
T
196 879 1997 7059 9999</langsyntaxhighlight>
 
And, with that, we can find the count of "just related" lychrel numbers:
 
<langsyntaxhighlight Jlang="j"> L -&# T
244</langsyntaxhighlight>
 
And, finally, which of the (true or related) lychrel numbers were themselves palindromes?
 
<langsyntaxhighlight Jlang="j"> (#~(=revdig))L
4994 8778 9999</langsyntaxhighlight>
 
To reiterate, here's the collected definitions with the task required results (everything above this point was basically just documentation - J tends to need that kind of documentation, because of the nature of the language):
 
<langsyntaxhighlight Jlang="j"> revdig=:('x',~|.)&.":"0
lychrel500=: (~: revdig)@((+^:~: revdig)^:500)@(+revdig)"0
L=:I.lychrel500 i.10001
Line 1,971:
244
(#~(=revdig))L
4994 8778 9999</langsyntaxhighlight>
 
T is the "seed" or "true" lychrel numbers, and #T is how many of them we found. L is all of the candidates (both seeds and relateds), so the difference in the lengths of L and T is the number of related.
Line 1,978:
Translation of [[Lychrel_numbers#Python|Python]] via [[Lychrel_numbers#D|D]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.math.BigInteger;
import java.util.*;
 
Line 2,059:
System.out.printf("%d Lychrel palindromes: %s%n", palin.size(), palin);
}
}</langsyntaxhighlight>
 
<pre>5 Lychrel seeds: [196, 879, 1997, 7059, 9999]
Line 2,066:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq"># This workhorse function assumes its arguments are
# non-negative integers represented as decimal strings:
def add(num1;num2):
Line 2,129:
end
end ) ;
</syntaxhighlight>
</lang>
'''The task'''
<langsyntaxhighlight lang="jq">lychrel_dictionary(10001; 500)
| {seed, palindromic_seed, related: (.related | length) }</langsyntaxhighlight>
 
'''Output'''
Line 2,155:
{{works with|Julia|1.3}}
 
<langsyntaxhighlight lang="julia">const cache = Dict{BigInt, Tuple{Bool, BigInt}}()
 
Base.reverse(n::Integer) = parse(BigInt, string(n) |> reverse)
Line 2,208:
println(length(seeds), " lychrel seeds: ", join(seeds, ", "))
println(length(related), " lychrel related")
println(length(palin), " lychrel palindromes: ", join(palin, ", "))</langsyntaxhighlight>
 
{{out}}
Line 2,216:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.math.BigInteger
Line 2,277:
println("\nThere are ${palindromes.size} palindromic Lychrel numbers, namely")
println(palindromes)
}</langsyntaxhighlight>
 
{{out}}
Line 2,295:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
A few convenient functions:
<langsyntaxhighlight Mathematicalang="mathematica">palindromeQ[n_Integer] :=
Block[{digits = IntegerDigits[n]}, digits == Reverse@digits]
 
Line 2,303:
palindromeQ@
Catch[Nest[If[palindromeQ[#], Throw[#], nextNumber[#]] &,
nextNumber[n], 500]]</langsyntaxhighlight>
 
A list of all integers less 10,000 that do not evolve to a palindrome in 500 iterations
<langsyntaxhighlight Mathematicalang="mathematica">candidates = Cases[Range[10000], _?(lychrelQ@# &)];</langsyntaxhighlight>
The Lychrel seeds can be obtained from the list of candidates as follows:
<langsyntaxhighlight Mathematicalang="mathematica">seeds = {};
NestWhile[(seeds = {seeds, First@#};
test = NestList[nextNumber, First@#, 10];
Line 2,314:
Rest[#], _?(IntersectingQ[test,
NestList[nextNumber, #, 10]] &)]) &, candidates,
Length@# > 0 &]; seeds = Flatten@seeds</langsyntaxhighlight>
{{out}}
<pre>{196,879,1997,7059,9999}</pre>
Line 2,322:
 
The number of related Lychrel numbers is:
<syntaxhighlight lang Mathematica="mathematica">Length@candidates - Length@seeds</langsyntaxhighlight>
{{out}}
<pre>244
</pre>
The Lycrel numbers that are also palindromes are:
<langsyntaxhighlight Mathematicalang="mathematica">Cases[candidates, _?(palindromeQ@# &)]</langsyntaxhighlight>
{{out}}
<pre>{4994,8778,9999}
Line 2,337:
The program runs in 20-30 ms and there is probably still some room for optimizations.
 
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strformat, strutils, tables
 
type
Line 2,468:
echo "These candidate seed Lychrel numbers are: ", cand.seeds
echo &"Found {cand.relatedCount(4)} candidate related Lychrel numbers between 1 and 10000."
echo "Palindromic candidate Lychrel numbers between 1 and 10000 are: ", cand.palindromicLychrel(4)</langsyntaxhighlight>
 
{{out}}
Line 2,484:
The main intention was to get below 1min for 100e6 ;-)
 
<langsyntaxhighlight lang="pascal">program p196;
{$IFDEF FPC}
{$R-}
Line 2,780:
user 0m48.579s
sys 0m0.012s
}</langsyntaxhighlight>
 
{{out}}
Line 2,793:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use English;
Line 2,844:
$h{$_}++ for @{$a}, @{$b};
keys %h;
}</langsyntaxhighlight>
{{out}}
<pre> Number of seed Lychrels <= 10000: 5
Line 2,852:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">iterations</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">500</span><span style="color: #0000FF;">,</span>
Line 2,911:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"related lychrel: %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">related</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d lychrel palindromes: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">palin</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">palin</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
Completes in under a second on desktop/Phix, but takes about 10s under pwa/p2js
Line 2,921:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
Limit = 500,
Lychrel = [],
Line 2,967:
Seed = Seed1,
% The check: have we found a Lyncrel number
(OldHit == true ; Found == false).</langsyntaxhighlight>
 
{{out}}
Line 2,977:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de pali? (N)
(let N (chop N)
(= N (reverse N)) ) )
Line 3,031:
(lychrel 10000)
 
(bye)</langsyntaxhighlight>
 
{{out}}
Line 3,045:
=={{header|PowerShell}}==
{{works with|PowerShell|4}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Test-Palindrome ( [String]$String )
{
Line 3,148:
return $LychrelNumbers
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$Upto = 10000
$IterationLimit = 500
Line 3,160:
"Number of palindrome Lychrel numbers: " + $LychrelNumbers.Palindromes.Count
" Palindrome Lychrel numbers: " + ( $LychrelNumbers.Palindromes -join ", " )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,174:
{{trans|D}}
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">reverse_number(Number, Rev):-
reverse_number(Number, 0, Rev).
 
Line 3,230:
 
main:-
lychrel(10000).</langsyntaxhighlight>
 
{{out}}
Line 3,241:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from __future__ import print_function
 
def add_reverse(num, max_iter=1000):
Line 3,288:
pals = [x for x in lychrel + l_related if x == reverse_int(x)]
print(' Number of Lychrel palindromes:', len(pals))
print(' Lychrel palindromes:', ', '.join(str(n) for n in pals))</langsyntaxhighlight>
 
{{out}}
Line 3,299:
 
If we test the numbers in ascending order, many of them would have been encountered when checking smaller numbers (i.e. 10 is seen when testing 5), caching them causes a large performance boost, more so with larger ranges.
<langsyntaxhighlight lang="python">from __future__ import print_function
 
def rev(n): return int(str(n)[::-1])
Line 3,332:
print("%d Lychel seeds:"%len(seeds), seeds)
print("%d Lychel related" % len(related))
print("%d Lychel palindromes:" % len(palin), palin)</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
library(gmp)
library(magrittr)
Line 3,407:
cat("Number of palindromic Lychrel numbers:",length(palindrome_lychrel),"\n")
cat("Palindromic Lychrel numbers:",palindrome_lychrel,"\n")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,421:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(require racket/splicing)
 
Line 3,480:
Palindromic Lychrel numbers: ~a~%
EOS
(reverse seeds/r) (length seeds/r) n-relateds (reverse palindromes/r)))</langsyntaxhighlight>
 
{{out}}
Line 3,491:
{{works with|Rakudo|2018.03}}
 
<syntaxhighlight lang="raku" perl6line>my %lychrels;
my @seeds;
my @palindromes;
Line 3,536:
say "Number of Lychrel related numbers < $limit: ", +$count - @seeds;
say " Number of Lychrel palindromes < $limit: ", +@palindromes;
say " Lychrel palindromes < $limit: ", join ", ", @palindromes;</langsyntaxhighlight>
 
{{out}}
Line 3,546:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays Lychrel numbers, related numbers, and palindromes. */
parse arg high limit . /*obtain optional argument from the CL.*/
if high='' | high=="," then high= 10000 /*Not specified? Then use the default.*/
Line 3,581:
T._= 1 /*mark number as "related".*/
end /*a*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 3,593:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">require 'set'
 
def add_reverse(num, max_iter=1000)
Line 3,642:
pals = (lychrel + l_related).select{|x| palindrome?(x)}.sort
puts " Number of Lychrel palindromes: #{pals.length}"
puts " Lychrel palindromes: #{pals}"</langsyntaxhighlight>
 
{{out}}
Line 3,667:
 
'''src/main.rs'''
<langsyntaxhighlight lang="rust">extern crate num;
use num::FromPrimitive;
use num::bigint::BigInt;
Line 3,784:
println!("Number of Lychrel palindromes: {}", palindrome_lychrels.len());
print_nums("Lychrel palindromes: ", &palindrome_lychrels);
}</langsyntaxhighlight>
 
{{out}}
Line 3,796:
=={{header|Scala}}==
Using a '''Map''' to prevent duplicate values in the Lychrel sequence when determining seeds.
<langsyntaxhighlight lang="scala">import scala.collection.mutable.LinkedHashMap
 
val range = 1 to 10000
Line 3,855:
println( s"\tRelated Count: ${related.size}" )
println( s"\t Palindromes: (${lychrelPals.mkString(", ")})")
}</langsyntaxhighlight>
 
{{out}}
Line 3,868:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var (
lychrels = [],
palindromes = [],
Line 3,913:
say ("Number of Lychrel related numbers < 10_000: ", lychrels.len - seeds.len)
say (" Number of Lychrel palindromes < 10_000: ", palindromes.len)
say (" Lychrel palindromes < 10_000: ", palindromes.join(', '))</langsyntaxhighlight>
{{out}}
<pre>
Line 3,927:
{{libheader|AttaSwift's BigInt}}
 
<langsyntaxhighlight lang="swift">import BigInt
 
public struct Lychrel<T: ReversibleNumeric & CustomStringConvertible>: Sequence, IteratorProtocol {
Line 4,025:
print("Lychrel seeds found: \(seeds.sorted())")
print("Number of related Lychrel nums found: \(related.count)")
print("Lychrel palindromes found: \(seeds.union(related).filter(isPalindrome).sorted())")</langsyntaxhighlight>
 
{{out}}
Line 4,041:
As we collect Lychrel numbers, we can save some time by storing all the members of their orbits in a hash table so that related numbers can be quickly identified. This is what the <tt>$visited</tt> variable is for: using the keys of a dictionary (or on earlier Tcl versions, an array) for a hash table is a common trick in Tcl. Native bignums mean we don't have to worry about overflow
 
<langsyntaxhighlight Tcllang="tcl">proc pal? {n} {
expr {$n eq [string reverse $n]}
}
Line 4,093:
}
 
lychrels</langsyntaxhighlight>
 
{{out}}
Line 4,107:
{{libheader|Wren-big}}
{{libheader|Wren-set}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/set" for Set
 
Line 4,169:
}
System.print("\nThere are %(palindromes.count) palindromic Lychrel numbers, namely:")
System.print(palindromes)</langsyntaxhighlight>
 
{{out}}
Line 4,189:
 
The Lychrel number chain is stored as a long string with '\0' separating each number, which we can then search to find relateds.
<langsyntaxhighlight lang="zkl">var BN=Import("zklBigNum");
 
// 192-->"887\01675\07436\013783\0..." ~60k bytes
Line 4,214:
}
seeds.apply("get",0)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">lychrels:=[1..0d10_000].filter(lychrel).apply(fcn(n){ T(n,lychrel(n,True)) });
println("[1..10,000] contains ",lychrels.len()," Lychrel numbers.");
 
Line 4,223:
(n:=findSeeds(lychrels))
.println("<-- Lychrel seeds (%d) ==> %d related"
.fmt(n.len(),lychrels.len() - n.len()));</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits