How to resolve the algorithm Lychrel numbers step by step in the Perl programming language
How to resolve the algorithm Lychrel numbers step by step in the Perl programming language
Table of Contents
Problem Statement
The above recurrence relation when applied to most starting numbers n = 1, 2, ... terminates in a palindrome quite quickly.
If n0 = 12 we get And if n0 = 55 we get Notice that the check for a palindrome happens after an addition.
Some starting numbers seem to go on forever; the recurrence relation for 196 has been calculated for millions of repetitions forming numbers with millions of digits, without forming a palindrome. These numbers that do not end in a palindrome are called Lychrel numbers. For the purposes of this task a Lychrel number is any starting number that does not form a palindrome within 500 (or more) iterations.
Any integer produced in the sequence of a Lychrel number is also a Lychrel number. In general, any sequence from one Lychrel number might converge to join the sequence from a prior Lychrel number candidate; for example the sequences for the numbers 196 and then 689 begin: So we see that the sequence starting with 689 converges to, and continues with the same numbers as that for 196. Because of this we can further split the Lychrel numbers into true Seed Lychrel number candidates, and Related numbers that produce no palindromes but have integers in their sequence seen as part of the sequence generated from a lower Lychrel number.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Lychrel numbers step by step in the Perl programming language
Source code in the perl programming language
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;
my(@seq_dump, @seed_lychrels, @related_lychrels);
for (my $n=1; $n<=$n_max; $n++) {
my @seq = lychrel_sequence($n);
if ($iter_cutoff == scalar @seq) {
if (has_overlap(\@seq, \@seq_dump)) { push @related_lychrels, $n }
else { push @seed_lychrels, $n }
@seq_dump = set_union(\@seq_dump, \@seq);
}
}
printf "%45s %s\n", "Number of seed Lychrels <= $n_max:", scalar @seed_lychrels;
printf "%45s %s\n", "Seed Lychrels <= $n_max:", join ', ', @seed_lychrels;
printf "%45s %s\n", "Number of related Lychrels <= $n_max:", scalar @related_lychrels;
printf "%45s %s\n", "Palindromes among seed and related <= $n_max:",
join ', ', sort {$a <=> $b} 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));
push @seq, $n;
}
@seq;
}
sub next_n { my $n = shift; $n + reverse($n) }
sub is_palindrome { my $n = shift; $n eq reverse($n) }
sub has_overlap {
my ($a, $b) = @ARG;
my %h;
$h{$_}++ for @{$a};
exists $h{$_} and return 1 for @{$b};
0;
}
sub set_union {
my ($a, $b) = @ARG;
my %h;
$h{$_}++ for @{$a}, @{$b};
keys %h;
}
You may also check:How to resolve the algorithm Execute SNUSP step by step in the Nim programming language
You may also check:How to resolve the algorithm Factorial step by step in the AsciiDots programming language
You may also check:How to resolve the algorithm Stack step by step in the Swift programming language
You may also check:How to resolve the algorithm Compiler/code generator step by step in the AWK programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the TUSCRIPT programming language