r/programming • u/Paddy3118 • Aug 29 '15
Lychrel numbers - Rosetta Code
http://rosettacode.org/wiki/Lychrel_numbers1
u/hahainternet Aug 29 '15 edited Aug 29 '15
This is a cute little problem because it can be solved with an infinite good list implementation quite nicely (perl6 with a little help from irc):
sub Lychrel($n) {
$n, -> $a { $a + $a.flip } ... -> $a { $a == $a.flip }
};
say Lychrel((10..99).pick);Example outputs:
56 121
27 99
28 110 121
93 132 363
41 55
91 110 121
94 143 484
14 55
93 132 363
19 110 121
50 55
And my favourite...
89 187 968 1837 9218 17347 91718 173437 907808 1716517 8872688 17735476 85189247 159487405 664272356 1317544822 3602001953 7193004016 13297007933 47267087164 93445163438 176881317877 955594506548 1801200002107 88132000231882
u/Paddy3118 Aug 29 '15
Your favourite is also Bradys': http://www.bradyharanblog.com/postcard/89 (He produces the videos).
1
u/hahainternet Aug 29 '15
Nice. Think this is worth sticking on rosetta code? I could flesh it out a bit to detect lengths or what have you but the concept is relatively obvious, the pointy-blocks-in-a-list-comprehension thing is a bit odd but you can also write it as
$n, {$^a + $^a.flip} ... {$^a == $^a.flip};The 'arity' of the blocks is implicit here, but it's possibly more clear
1
u/Paddy3118 Aug 29 '15
If it's:
Good idiomatic Perl 6 and
It solves the details of the task
Then it would be the kind of thing we encourage on Rosetta Code :-)
2
u/hahainternet Aug 29 '15
I'm not so strong on whether this is particularly idiomatic. I'll ask around on IRC a little more before contributing.
1
u/RodgerTheGreat Aug 30 '15
There are quite a few additional implementations of this algorithm on Code Golf: http://codegolf.stackexchange.com/questions/51839/palindrome-reversal-addition
1
u/Paddy3118 Aug 30 '15
Thanks for that link. I took a look at the description and it is pretty good . Only a small niggle in that the author missed mentioning how where you do your test for a palindrome can either include or exclude counting initial numbers that are palindromes as potential Lychrel candidates/relateds.
The popularity of this recreational maths problem makes sense :-)
1
0
Aug 29 '15
[deleted]
1
u/Paddy3118 Aug 29 '15
Snap. It's a reference on the RC task and is what sparked my interest to start the task. :-)
2
u/monsieursquirrel Aug 29 '15
I'm wearing my rustacean shirt today so I figured I'd have a go. This is a pretty naive implementation.
https://github.com/monsieursquirrel/lychrel
A thought I haven't acted on yet: once two sequences converge (as with related numbers) they don't ever diverge. This could be used for early return. Or, more interestingly, it could be used to rule out previously accepted numbers by skipping to the end of the sequence and spending the checks there.