Long years and failed palindromes
Weekly challenge 137 — 1 November 2021
Week 137: 1 Nov 2021
You are given a number, 10 <= $n <= 1000
.
Write a script to find out if the given number is a Lychrel Number.
To keep the task simple, we impose the following rules: a. Stop if the number of iterations reached 500. b. Stop if you end up with number >= 10_000_000.
Example 1 Input: $n = 56 Output: 0 After 1 iteration, we found palindrome number. 56 + 65 = 121 Example 2 Input: $n = 57 Output: 0 After 2 iterations, we found palindrome number. 57 + 75 = 132 132 + 231 = 363 Example 3 Input: $n = 59 Output: 0 After 3 iterations, we found palindrome number. 59 + 95 = 154 154 + 451 = 605 605 + 506 = 1111
This is a rather futile challenge, as no number has yet been proved to be a Lychrel Number. However ...
My non-solution does what is required, and by increasing Muhammad's limit of 10**7 to 10**13 I can show that 89 does and 196 doesn't converge to a palindrome.
According to Wikipedia, 1000206827388999999095750 takes 293 iterations to reach a 132 digit palindrome, and that's well into BigInt territory.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2021-11-01 use utf8; # Week 137 - task 2 - Lychrel number use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; my $i; for $i (80 .. 89) { lychrel_number($i); } lychrel_number(196); sub lychrel_number { my ($input, $i, $r, $test); $input = shift; say qq[\nInput: $input]; $test = $input; # iterate up to 500 times for $i (0 .. 499) { $r = reverse($test); # found a palindrome if ($r == $test) { print qq[Output: $input is not a Lychrel Number - ]; say qq[$r reached after $i iterations]; return; } # not a palindrome so add and repeat $test += $r; # reached a very large number so give up if ($test >= 10_000_000_000_000) { say qq[Output: 1 - reached $test after $i iterations]; return; } } # like it says if ($r != $test) { say qq[Output: still not palindromic after 500 iterations]; # but no number has yet been shown to be one } else { say qq[Output: $input is a Lychrel Number]; } }
Input: 80 Output: 80 is not a Lychrel Number - 88 reached after 1 iterations Input: 81 Output: 81 is not a Lychrel Number - 99 reached after 1 iterations Input: 82 Output: 82 is not a Lychrel Number - 121 reached after 2 iterations Input: 83 Output: 83 is not a Lychrel Number - 121 reached after 1 iterations Input: 84 Output: 84 is not a Lychrel Number - 363 reached after 2 iterations Input: 85 Output: 85 is not a Lychrel Number - 484 reached after 2 iterations Input: 86 Output: 86 is not a Lychrel Number - 1111 reached after 3 iterations Input: 87 Output: 87 is not a Lychrel Number - 4884 reached after 4 iterations Input: 88 Output: 88 is not a Lychrel Number - 88 reached after 0 iterations Input: 89 Output: 89 is not a Lychrel Number - 8813200023188 reached after 24 iterations Input: 196 Output: 1 - reached 17602285712176 after 25 iterations
Any content of this website which has been created by Peter Campbell Smith is in the public domain