user
:   :   1. Take an integer n, greater than zero.
        2. Form the next n of its series by reversing the digits of the current n and adding the result to the current n.
        3. Stop when n becomes palindromic - i.e. the digits of n in reverse order == n.

The above recurrence relation when applied to most starting numbers n = 1, 2, ... terminates in a palindrome quite quickly.

Example

If n0 = 12 we get

```
       12
       12 +  21 =  33,   a palindrome!

```

And if n0 = 55 we get

```
       55
       55 +  55 = 110
      110 + 011 = 121,   a palindrome!

```

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.

Seed and related Lychrel numbers

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:

```
        196
        196 +    691 =   887
        887 +    788 =  1675
       1675 +   5761 =  7436
       7436 +   6347 = 13783
      13783 +  38731 = 52514
      52514 +  41525 = 94039
       ...


        689
        689 +    986 =  1675
       1675 +   5761 =  7436
       ...

```

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.

Task

* Find the number of seed Lychrel number candidates and related numbers for n in the range 1..10000 inclusive. (With that iteration limit of 500).
* Print the number of seed Lychrels found; the actual seed Lychrels; and just the *number* of relateds found.
* Print any seed Lychrel or related number that is itself a palindrome.

Show all output here.

References

* What's special about 196? Numberphile video.
* A023108 Positive integers which apparently never result in a palindrome under repeated applications of the function f(x) = x + (x with digits reversed).
* Status of the 196 conjecture? Mathoverflow.
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2018.03

```
my %lychrels;
my @seeds;
my @palindromes;
my $count;
my $max = 500;
my $limit = '10_000';
my %seen;

for 1 .. $limit -> $int {
    my @test;
    my $index = 0;
    if $int.&is-lychrel {
        %lychrels.push: ($int => @test).invert;
        @palindromes.push: $int if $int == $int.flip;
        $count++;
    }

    sub is-lychrel (Int $l) {
        if  %seen{$l} or $index++ > $max {
            %seen{$_} = True for @test;
            return True;
        }
        @test.push: my $m = $l + $l.flip;
        return False if $m == $m.flip;
        $m.&is-lychrel;
    }
}

for %lychrels{*}»[0].unique.sort -> $ly {
    my $next = False;
    for %lychrels -> $l {
        for $l.value[1..*] -> $lt {
            $next = True and last if $ly == $lt;
            last if $ly < $lt;
        }
        last if $next;
    }
    next if $next;
    @seeds.push: $ly;
}

say "   Number of Lychrel seed numbers < $limit: ", +@seeds;
say "             Lychrel seed numbers < $limit: ", join ", ", @seeds;
say "Number of Lychrel related numbers < $limit: ", +$count - @seeds;
say "    Number of Lychrel palindromes < $limit: ", +@palindromes;
say "              Lychrel palindromes < $limit: ", join ", ", @palindromes;

```

Output:

```
   Number of Lychrel seed numbers < 10_000: 5
             Lychrel seed numbers < 10_000: 196, 879, 1997, 7059, 9999
Number of Lychrel related numbers < 10_000: 244
    Number of Lychrel palindromes < 10_000: 3
              Lychrel palindromes < 10_000: 4994, 8778, 9999
```