Lychrel numbers: Difference between revisions
m
→{{header|Perl}}: modernize code & speed up with Math::AnyNum
(Added Wren) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: modernize code & speed up with Math::AnyNum) |
||
Line 2,455:
=={{header|Perl}}==
<lang perl>use strict;
use warnings;
use English;
use Const::Fast;
use Math::AnyNum qw(:overload);
const my $n_max => 10_000;
const my $iter_cutoff => 500;
for (my $n=1; $n<=$n_max; $n++) {
my @seq = lychrel_sequence($n);
if (
if (has_overlap(\@seq, \@seq_dump)) { push @related_lychrels, $n }
else { push @
@seq_dump = set_union(\@seq_dump, \@seq);
}
}
join ', ', sort {$a <=> $b} grep { is_palindrome($ARG) } @seed_lychrels, @related_lychrels;
▲ grep {is_palindrome($ARG)} (@seed_lychrels, @related_lychrels)),
sub lychrel_sequence {
my $n = shift;
my @seq
for (1 .. $iter_cutoff) {
return if is_palindrome($n = next_n($n));
}
}
sub next_n { my $n = shift; $n + reverse($n) }
sub is_palindrome
sub has_overlap {
my ($a, $b) = @ARG;
my %h;
}
Line 2,519 ⟶ 2,503:
my ($a, $b) = @ARG;
my %h;
keys %h;
}</lang>
{{out}}
Palindromes among seed and related <= 10000: 4994, 8778, 9999</pre>
▲Number of related Lychrels <= 10000: 244
▲Palindromes among seed and related <= 10000: 4994, 8778, 9999
=={{header|Phix}}==
|