rbandrews: (Default)
[personal profile] rbandrews

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.

Date: 2008-01-09 10:50 am (UTC)
From: [identity profile] st3v3.livejournal.com
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... ;)

Profile

rbandrews: (Default)
rbandrews

July 2024

S M T W T F S
 123456
78910111213
14151617181920
212223242526 27
28293031   

Style Credit

Page generated Jun. 24th, 2025 06:29 pm
Powered by Dreamwidth Studios

Expand Cut Tags

No cut tags