rbandrews: (Default)
rbandrews ([personal profile] rbandrews) wrote2007-12-28 06:15 pm

A very strange monkey

The rarest present I got this year would have to be "Consul, the Educated Monkey".

It's a small tin plate with a monkey on it, over a chart of numbers. You move the feet of the monkey to point at two numbers, and, through scissor action, his hands point at those numbers' product in the chart. Here's another picture, and another.

So aside from a very odd gift, this would make a nifty programming puzzle. So, here one is: generate the chart printed behind the monkey. Make a program that generates it for any upper limit of numbers.

For reference, here's the chart (printed from my program to do this):

1
12 4
11 24 9
10 22 36 16
9 20 33 48 25
8 18 30 44 60 36
7 16 27 40 55 72 49
6 14 24 36 50 66 84 64
5 12 21 32 45 60 77 96 81
4 10 18 28 40 54 70 88 108 100
3 8 15 24 35 48 63 80 99 120 121
2 6 12 20 30 42 56 72 90 110 132 144

The numbers on the bottom go from 1 to 12, and the little square on the far right means, obviously, "square the number". For bonus points, make the program print the chart for any arbitrary operation on two numbers, like print an addition chart like this, or a modulus chart. In this case, the square should just act as if it is whatever the other foot is pointing at. Don't worry about spacing, I had to resort to HTML to get it to line up. Just generating the right numbers is enough.

I'll make another post in a few days with code and explanation.

[identity profile] desfido.livejournal.com 2007-12-29 01:35 pm (UTC)(link)
Should I just post another comment with my code solution or what?

[identity profile] desfido.livejournal.com 2007-12-29 05:30 pm (UTC)(link)
Meh, I just went ahead and posted it in my journal, here.

[identity profile] st3v3.livejournal.com 2008-01-09 10:50 am (UTC)(link)
Hi... found you via reddit, and couldn't resist writing it.

Here's the ruby function which creates the array of numbers in line(i)
    # eg line(1) = [1], line(3) = [11,24,9]
    def line(y) (1..y-1).map{ |x| (13-y+x)*(x)}.push(y*y)  end

And here's the code which prints out your centred pyramid...
    # print the pyramid, centred.
    print (1..12).
      map { |y|  line y }.                  # num array
      map { |ln| ln.join(" ").center(72) }. # centred string arr
      join("\n")                            # single multiline string

It's like a rag to a bull, it really is... ;)