|Just another Perl shrine|
(Efficiency Golf) Triangular numbersby jepri (Parson)
|on May 30, 2001 at 09:08 UTC||Need Help??|
Every month New Scientist magazine publishes a little mathematical challenge. Normally these things irritate me because they rely on some cute but irrelevant mathematical trick. This month, however, they have tried to obscure the question by replacing numbers with letters, which makes it a Perlish challenge. Here we go:
A triangular number is a term from the sequence generated by this equation (in Perl):
This gives 1, 3, 6, 10 and so on. This months challenge is to find a certain four numbers from this sequence. They don't give us the numbers, but they have substituted letters for digits of these numbers, and made words from them. The words are: ONE, THREE, SIX and TEN.
So for instance, we could have O=1, N=2, E=3, and the first number would be 123.
So I wrote a Perl program to have a go at it. I make a hash of the letters, assign a value (starting at zero) to each letter, then start incrementing. It's been going for the last few hours and I just realised it could take quite a long time to finish.
I am declaring that the challenge is to make it efficient, not write it with the fewest characters. This is not part of the official challenge, if you feel like writing it in one line, go right ahead.
The prize is drawn on the 28 June
jeroenes asked if there were restrictions on the numbers - there are. It's one letter for one digit. The digits may be 0..9 (the article is a little vague on this, but that's my reading). But I forgot to mention that none of the numbers start with a zero. i.e. 003 is not a valid solution for this challenge
The challenge does not mention the order of the numbers, so I guess we can assume 'any'.
If you would also like to apply for the 15-pound prize you can send your solution to email@example.com (include your postal address)
My bad. The challenge for the magazine is to get the answer. The answers are then drawn out of a hat, first right one gets the prize.
My challenge to you is to do it most efficiently.