foreach my $n (1..1000) { $x=.5*$n*($n+1); print $x; } #### #!/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; }