Lychrel numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: give some indication of progress)
(→‎{{header|Perl 6}}: rewrote to be much faster; O(n²) or maybe O(n logn) rather than O(n³))
Line 2,374: Line 2,374:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2015-10-10}}
{{works with|Rakudo|2018.03}}


<lang perl6>my @lychrels;
<lang perl6>my %lychrels;
my @palindromes;
my @seeds;
my @seeds;
my @palindromes;
my $count;
my $max = 500;
my $max = 500;
my $limit = 10_000;


for 1 .. 10000 -> $int {
for 1 .. $limit -> $int {
my @test;
my @test;
my $count = 0;
my $index = 0;
print "$int " and @lychrels.push( $int => [@test] ) if lychrel($int);
if lychrel($int) {
print "\b" x 20, "Found Lychrel: $int";
%lychrels.push: ($int => [@test]).invert;
@palindromes.push: $int if $int == $int.flip;
$count++;
}
print "\b" x 20;


sub lychrel (Int $l) {
sub lychrel (Int $l) {
return True if $count++ > $max;
return True if $index++ > $max;
@test.push: my $m = $l + $l.flip;
@test.push: my $m = $l + $l.flip;
return False if $m == $m.flip;
return False if $m == $m.flip;
Line 2,394: Line 2,402:
}
}


@seeds = @lychrels[0];
for %lychrels{*}»[0].unique.sort -> $ly {
my $next = False;
for @lychrels -> $l {
for %lychrels -> $l {
@palindromes.push: $l.key if $l.key == $l.key.flip;
my $trial = 0;
for $l.value[1..*] -> $lt {
$next = True and last if $ly == $lt;
for @seeds -> $s {
last if any($s.value) ~~ any($l.value);
last if $ly < $lt;
$trial++;
}
last if $next;
}
}
@seeds.push: $l if $trial == +@seeds;
next if $next;
@seeds.push: $ly;
}
}


say " Number of Lychrel seed numbers < 10_000: ", +@seeds;
say " Number of Lychrel seed numbers < $limit: ", +@seeds;
say " Lychrel seed numbers < 10_000: ", join ", ", @seeds>>.keys;
say " Lychrel seed numbers < $limit: ", join ", ", @seeds;
say "Number of Lychrel related numbers < 10_000: ", +@lychrels -@seeds;
say "Number of Lychrel related numbers < $limit: ", +$count - @seeds;
say " Number of Lychrel palindromes < 10_000: ", +@palindromes;
say " Number of Lychrel palindromes < $limit: ", +@palindromes;
say " Lychrel palindromes < 10_000: ", join ", ", @palindromes;</lang>
say " Lychrel palindromes < $limit: ", join ", ", @palindromes;</lang>


{{out}}
{{out}}
<pre> Number of Lychrel seed numbers < 10000: 5
<pre>
Number of Lychrel seed numbers < 10_000: 5
Lychrel seed numbers < 10000: 196, 879, 1997, 7059, 9999
Lychrel seed numbers < 10_000: 196, 879, 1997, 7059, 9999
Number of Lychrel related numbers < 10000: 244
Number of Lychrel related numbers < 10_000: 244
Number of Lychrel palindromes < 10000: 3
Number of Lychrel palindromes < 10_000: 3
Lychrel palindromes < 10000: 4994, 8778, 9999</pre>
Lychrel palindromes < 10_000: 4994, 8778, 9999
</pre>


=={{Header|PicoLisp}}==
=={{Header|PicoLisp}}==