Lychrel numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: give some indication of progress) |
Thundergnat (talk | contribs) (→{{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| |
{{works with|Rakudo|2018.03}} |
||
<lang perl6>my |
<lang perl6>my %lychrels; |
||
⚫ | |||
my @seeds; |
my @seeds; |
||
⚫ | |||
my $count; |
|||
my $max = 500; |
my $max = 500; |
||
my $limit = 10_000; |
|||
for 1 .. |
for 1 .. $limit -> $int { |
||
my @test; |
my @test; |
||
my $ |
my $index = 0; |
||
if lychrel($int) { |
|||
print "\b" x 20, "Found Lychrel: $int"; |
|||
%lychrels.push: ($int => [@test]).invert; |
|||
⚫ | |||
$count++; |
|||
} |
|||
print "\b" x 20; |
|||
sub lychrel (Int $l) { |
sub lychrel (Int $l) { |
||
return True if $ |
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: | ||
} |
} |
||
for %lychrels{*}»[0].unique.sort -> $ly { |
|||
my $next = False; |
|||
for |
for %lychrels -> $l { |
||
⚫ | |||
for $l.value[1..*] -> $lt { |
|||
$next = True and last if $ly == $lt; |
|||
⚫ | |||
last if |
last if $ly < $lt; |
||
} |
|||
last if $next; |
|||
} |
} |
||
next if $next; |
|||
⚫ | |||
} |
} |
||
say " Number of Lychrel seed numbers < |
say " Number of Lychrel seed numbers < $limit: ", +@seeds; |
||
say " Lychrel seed numbers < |
say " Lychrel seed numbers < $limit: ", join ", ", @seeds; |
||
say "Number of Lychrel related numbers < |
say "Number of Lychrel related numbers < $limit: ", +$count - @seeds; |
||
say " Number of Lychrel palindromes < |
say " Number of Lychrel palindromes < $limit: ", +@palindromes; |
||
say " Lychrel palindromes < |
say " Lychrel palindromes < $limit: ", join ", ", @palindromes;</lang> |
||
{{out}} |
{{out}} |
||
<pre> Number of Lychrel seed numbers < 10000: 5 |
|||
<pre> |
|||
Lychrel seed numbers < 10000: 196, 879, 1997, 7059, 9999 |
|||
Number of Lychrel related numbers < 10000: 244 |
|||
Number of Lychrel |
Number of Lychrel palindromes < 10000: 3 |
||
Lychrel palindromes < 10000: 4994, 8778, 9999</pre> |
|||
Lychrel palindromes < 10_000: 4994, 8778, 9999 |
|||
</pre> |
|||
=={{Header|PicoLisp}}== |
=={{Header|PicoLisp}}== |