Welcome to the Monastery PerlMonks

### Re: Coming soon: Algorithm::Loops

by Abigail-II (Bishop)
 on Apr 11, 2003 at 22:12 UTC ( #249986=note: print w/replies, xml ) Need Help??

in reply to Coming soon: Algorithm::Loops

I found 301 years with a unique solution in the range 2000 till 3000. I used a different, IMO, simpler program than the one you describe. I think you are referring to 2001 and 2005 as candidates in the counter puzzle.

Anyway, my program is after the readmore tag.

Abigail

```#!/usr/bin/perl

use strict;
use warnings;

my \$from = 2000;
my \$till = 3000;

my @nums;
for my \$x (0 .. 9) {
for my \$y (\$x .. 9) {
for my \$z (\$y .. 9) {
my %seen;
push @nums => [grep {!\$seen {\$_} ++}
map  {s/^0{1,2}//; \$_} "\$x\$y\$z", "\$x\$z\$y",
+"\$y\$x\$z",
"\$y\$z\$x", "\$z\$x\$y",
+"\$z\$y\$x"]
}
}
}

sub all_sub_sets;
sub all_sub_sets {
return unless @_;

my \$f = shift;
my @r = all_sub_sets @_;

@r => [\$f] => map {[\$f => @\$_]} @r
}

foreach my \$num (@nums) {
my @sets = all_sub_sets @\$num;

foreach my \$set (@sets) {
local \$" = " + ";
my \$sum  = eval "@\$set";
if (\$sum >= \$from && \$sum < \$till) {
}
}
}

for my \$sum (\$from .. \$till - 1) {
local \$" = " + ";
}

Replies are listed 'Best First'.
Re^2: Coming soon: Algorithm::Loops (puzzle2)
by tye (Sage) on Apr 12, 2003 at 07:06 UTC

I think you missed the hint in the code:

```    ## \$expr= "()"   if  @_ < 6;
I noticed that there were only two numbers with unique solutions involving 6 terms. So we agreed that a number with only one solution would make the problem harder. I felt that more terms in that solution would also make it harder. But my statement of the puzzle was pretty vague.

[ And sure you can simplify the code if you hard-code three loops and hard-code the permutations. It runs fast enough that it'd be interesting to look at 4-digit terms next... ;) ]

- tye
Sorry, I hadn't studied the code well enough that you intended to find years with a unique solution, and the solution requiring six terms. A modified version of my program finds 5106 and 5328.

Also, my modified versions no longer has three digits hardcoded. You can now give the number of digits on the command line - you can also give the minimum number of terms in the unique solution, and the range of years to investigate. Unfortunally, doing it for 4 digits takes a long time. There are about 12 billion sums to consider, compared to about 14 thousand for 3 digits.

Abigail

Create A New User
Node Status?
node history
Node Type: note [id://249986]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-04-13 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?