NOTE: **use Perl;** is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

Ovid

(email not shown publicly)

http://publius-ovidius.livejournal.com/

**AOL IM:** ovidperl (**Add Buddy, Send Message**)

(email not shown publicly)

http://publius-ovidius.livejournal.com/

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Sunday June 08, 2008

11:47 AM

Every week, New Scientist has an "Enigma" puzzle. I've always thought they should be easy to solve with programming, so this week I decided to try it. Here's this week's puzzle:

Using each of the digits 1 to 9, find a 3-digit positive integer divisible by 7 whose reverse is an integer also divisible by 7, a 3-digit positive integer divisible by 9 whose reverse is an integer also divisible by 9, and a 3-digit positive integer divisible by 11 whose reverse is an integer also divisible by 11.

What are the smallest and largest of your six integers?

So basically you want all three digit numbers *not* containing zero. When you are examining candidates, the numbers should use "each" of the digits, not just "any". My program is still getting too many results. Either I'm missing something fundamental or their is an ambiguity in the spec.

Full

Abbreviated

Hidden

Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.

## Maybe there is an ambiguity... (Score:1)

## Re: (Score:2)

I get more than that. They draw the prize next Wednesday, so I don't think it's fair to post code right now, but that's OK because mine is obviously buggy :)

## Re: (Score:2)

Er, now that I stop to think about it, perhaps it's not fair for me to count a number

andits reverse. D'oh!## Re: (Score:1)

## Re: (Score:2)

I think you're seeing something I don't, then, because I have no idea what your first sentence meant :)

## Re: (Score:2)

OK, now I get your results :(

## Is this it? (Score:2)

## Re: (Score:2)

Nope, not even close :) That tiny spec bears close rereading. For example, "using each of the digits 1 to 9" for 3-digit numbers means your first grep is off.

I also used

&List::MoreUtils::uniqto pre-trim that list (since duplicate numbers are not allowed).Basically, you'll need a three stage process (I think). First, generate your candidate list. Second, find all three digit numbers which satisfy the reverse divisor requirement. Then construct your

## Re: (Score:1)

## Re: (Score:1)

rjbs

## Re: (Score:2)

That appears to be correct. You have to read the spec

carefully. They ask you to useeachof those numbers, notany. I got that wrong the first time.## Re: (Score:2)

## Maybe there isn't an ambiguity ... (Score:1)

you can check my code at: http://www.depesz.com/ovid.pl [depesz.com]

of course i could have made mistake, but the numbers "look" right to me.

## Re: (Score:2)

I get the same sets of numbers, but what does "six integers" mean in this context?

## Re: (Score:1)

## Re: six integers (Score:1)

## Re: (Score:2)

You have five three-digit numbers. One for 7, one for 11, but three for 9. If there was only one result for each of the three (which is what I was initially expecting), then six would make sense.

## Re: (Score:1)

The request was:

"What are the smallest and largest of your six integers?"

even it we'll change it to "10 integers" the answer stays the same. i.e. the numbers i got for div/9 are neither min nor max.

## Spec (Score:1)

`(a * 10**2 + b * 10**1 + c * 10**0) % 7 == 0`

`(c * 10**2 + b * 10**1 + a * 10**0) % 7 == 0`

`(d * 10**2 + e * 10**1 + f * 10**0) % 9 == 0`

`(f * 10**2 + e * 10**1 + d * 10**0) % 9 == 0`

`(g * 10**2 + h * 10**1 + i * 10**0) % 11 == 0`

`(i * 10**2 + h * 10**1 + g * 10**0) % 11 == 0`

and where there exists an invertible function between the variables a..i to the numbers 1..9.

## I bet New Scientist publishes a correction (Score:1)

## Re: (Score:1)

## Re: (Score:2)

They're not usually this unclear. I think it's just a fluke, but I'd have to work through some others to be sure.

## My bit of clever (Score:2)

`next if $nine =~ qr/[$seven]/;`

A three digit number makes a fine character class inside of a regex when making sure that you're not reusing a digit.--

xoa

## Re: (Score:2)

Hey, it's nice to see how different languages tackle the problem. Thanks!

## Re: (Score:2)

Unsurprisingly, we used a lot of the same elements, including

/(.).?\1/. I decided to recurse. Since these are all three-digit numbers, a simple(sort @nums)[0,-1]gives min and max, so as soon as we get a hit in 11 we have all the info we need. So yours could be shortened quite a bit further: