### comment on

 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):

```foreach my \$n (1..1000) {
\$x=.5*\$n*(\$n+1);
print \$x;
}

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

Update

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 enigma@newscientist.com (include your postal address)

```#!/usr/bin/perl
use strict;

my @words=('ONE', 'THREE', 'SIX', 'TEN');
my %letters;
foreach (@words) {
my @a=split //, \$_;
foreach (@a) {
\$letters{\$_}=0;
}
}

#Construct an array of the letters (you'll see why
#in a few lines)
my @letters=(keys %letters);

my \$count;
my \$oldtime;
do {
\$letters{\$letters[\$#letters]}++;
foreach( reverse(0..\$#letters)) {
if (\$count++>10000) {
print time-\$oldtime,"\n";
\$oldtime=time;
\$count=0;
}
#Increment the number for the letter at the end
#of the array, and don't forget to carry
if (\$letters{\$letters[\$_]} > 9) {
\$letters{\$letters[\$_]}=0;
\$letters{\$letters[\$_-1]}++;
}
#Turn the letters into numbers
prep(%letters);
}
} until (\$letters{\$letters[0]} > 9);

sub prep {
my %letters=@_;
my \$match=0;
my %match;
foreach my \$a (@words) {
my \$test= \$a;
#Change the letters into numbers
foreach (keys %letters) {
\$test=~ s/\$_/\$letters{\$_}/g;
}
\$match{"\$a=\$test is triangular\n"}=1 and \$match ++ if is_triangular
+(\$test);
}
if (\$match>0) {
# print "-----------\n";
if (\$match>(@words-1)) {
print \$_ foreach keys %match;
print "Found it!\n";
#exit;
}
}

}

#This should be memoised, but I'm afraid of the
#memory blowout
sub is_triangular {
my \$num=shift;
my (\$n,\$x)=(1,0);
do {
\$x=.5*\$n*(++\$n);
return 1 if \$x==\$num;
} until \$x > \$num;
return 0;
}

Update II

Since tachyon has indeed beaten me to the answer I shall enter PerlMonks in the competetion, winnings go in the plate. (More likely they will go into an envelope and off to vroom).

Update III

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.

____________________
Jeremy
I didn't believe in evil until I dated it.

In reply to (Efficiency Golf) Triangular numbers by jepri

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-12-09 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
R or B?

Results (36 votes). Check out past polls.

Notices?