Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. IQ / Programming Quiz (Cannon-Ball Stacks)

IQ / Programming Quiz (Cannon-Ball Stacks)

Scheduled Pinned Locked Moved The Lounge
helpcomquestionlearning
85 Posts 19 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Stephen Dycus

    but not all of them: The smallest number of cannon-balls he can have if the two pyramids are the same size is twenty (assuming he uses every cannon-ball in both pyramids). That's an assumption, not a requirement :)

    P Offline
    P Offline
    PhilLenoir
    wrote on last edited by
    #51

    Using that logic I think that your answer might be one :cool:

    Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

    S 1 Reply Last reply
    0
    • P PhilLenoir

      Using that logic I think that your answer might be one :cool:

      Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

      S Offline
      S Offline
      Stephen Dycus
      wrote on last edited by
      #52

      Otherwise you would have to form two different sized pyramids who could form one larger pyramid without any left overs. That would require a Calculus 1 concept called optimization, which isn't too mard but more work than I want to do.

      1 Reply Last reply
      0
      • S Stephen Dycus

        Wow such complicated answers.... the answer is 4. The question is If the two smaller pyramids are different sizes, however, what would be the minimum number of cannon-balls he could use to make one large tetrahedral pyramid? ... but the thing is, there are still only 20 cannonballs to use to build the pyramid and he never said you had to use all the cannonballs. It asks the MINIMUM... which is 3 on the bottom + 1 on top.

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #53

        You missed this bit from the problem: "assuming he uses every cannon-ball in both pyramids". But nice try. :)

        Thou mewling ill-breeding pignut!

        S 1 Reply Last reply
        0
        • P PhilLenoir

          Maybe not the most elegant solution but might possibly be the fastest way to come up with the answer!: 1 Using Excel I created 3 columns: column 1 being a linear sequence (1,2,3 ....100); column 2 representing each layer (B1=1,B2=A1+A2); the third representing the pyramid (C1=1,C2=B1+B2) 2 I pasted these into an Access table 3 I cross joined the table to itself and added the two columns 3 4 I inner joined the query to the table on sum = column 3 Answer 120 (8 layers) + 560 (14 layers) = 680 (15 layers)

          Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #54

          PhilLenoir wrote:

          Using Excel I created 3 columns: column 1 being a linear sequence (1,2,3 ....100); column 2 representing each layer (B1=1,B2=A1+A2); the third representing the pyramid (C1=1,C2=B1+B2)

          That's how I started out on paper, then converted that to C#.

          PhilLenoir wrote:

          I inner joined the query to the table on sum = column 3

          I hadn't considered using a database. Points for innovation. :thumbsup:

          PhilLenoir wrote:

          680

          That's the answer I got. :)

          Thou mewling ill-breeding pignut!

          1 Reply Last reply
          0
          • Y YvesDaoust

            Even bruter force:

            def T(n):
            return n * (n + 1) * (n + 2) / 6

            for r in range (1, 1000):
            for q in range(1, r):
            for p in range(1, q):
            if T(p) + T(q) == T(r):
            print T(p), '+', T(q), '=', T(r)

            Yields:

            120 + 560 = 680
            1540 + 27720 = 29260
            4960 + 29260 = 34220
            10660 + 59640 = 70300
            39711 + 182104 = 221815
            102340 + 125580 = 227920
            7140 + 280840 = 287980
            19600 + 447580 = 467180
            ...

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #55

            Points for compact code. :thumbsup:

            Thou mewling ill-breeding pignut!

            Y 1 Reply Last reply
            0
            • E el_vez

              Haskell answer:

              layers = scanl1 (+) [1..]
              sizes = scanl1 (+) layers
              combinedSizes = [(s1, s2, s1 + s2) | s1 <- sizes, s2 <- takeWhile (< s1) sizes]
              newPyramids = filter combinesToPyramid combinedSizes
              where combinesToPyramid (s1, s2, sum) = sum `elem` (takeWhile (sum >=) sizes)

              answer = head newPyramids

              -------- SPOILER ------------ this gives the answer as "(560, 120, 680)", (if you load it up in GHCI and type "answer"). If you want the n first solutions, just type "take n newPyramids":

              take 3 newPyramids
              [(560,120,680),(27720,1540,29260),(29260,4960,34220)]

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #56

              I don't know Haskell, but that looks pretty elegant. Points awarded. :)

              Thou mewling ill-breeding pignut!

              1 Reply Last reply
              0
              • A AspDotNetDev

                You missed this bit from the problem: "assuming he uses every cannon-ball in both pyramids". But nice try. :)

                Thou mewling ill-breeding pignut!

                S Offline
                S Offline
                Stephen Dycus
                wrote on last edited by
                #57

                That's an assumption though, not a requirement. A requirement would be worded something like: (as he must use every cannonball from both pyramids) The fact that the problem states exactly how many cannon balls there are should tell you something. With 20 cannonballs the best you could do with 2 pyramids is a 3+1 pyramid and a 6+3+1 pyramid with 6 left over. The question asks if the pyramids are not the same size, what would be the minimum. This doesn't magically give you permission to *add* more cannonballs to the equation :P So if it *is* a poorly worded requirement then the answer is 20. Otherwise, it's 4.

                A 1 Reply Last reply
                0
                • Y YvesDaoust
                  1. looked up the tetrahedral numbers in Wikipedia and found this list: 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969... (10 seconds of work) 2) opened an Excel sheet and created the table of sums 2 5 11 21 36 57... 5 8 14 24 39 60... 11 14 20 30 45 66... 21 24 30 40 55 76... 36 39 45 55 70 91... ... (just needed to recall the appropriate syntax to form the cell expression; 1 minute of work) 3) sorted all sums in increasing order 2, 5, 5, 8, 11, 11, 14, 14, 20, 21, 21, 24, 24, 30, 30, 36, 36, 39, 39, 40, 45, 45, 55, 55, 57, 57, 60, 60, 66, 66, 70, 76, 76, 85, 85, 88, 88, 91, 91, ... (moved to Word to flatten the table structure; 1 minute of work) 4) spotted by eye in the list the first tetrahedral number larger than 20: 680, the sum of 120 and 560. (Lucky the Wikipedia list was long enough :)) (2 extra minutes) 5) explained the answer in CodeProject (half an hour)
                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #58

                  Points awarded for getting as close as possible to cheating without fully qualifying as cheating. ;P

                  Thou mewling ill-breeding pignut!

                  Y 1 Reply Last reply
                  0
                  • J jsc42

                    I did this using an old technique: pencil and paper. It is more complicated to explain than it was to do. Basically, I wrote 3 columns of numbers: Col 1: 1, 2, 3, 4, etc [represents the no of levels in the pyramid) Col 2: 1, (corresponding no in col 1 + prev number in col 2), ... [represents the no of new balls in the new level of the pyramid] Col 3: 1, (corresponding no in col 2 + prev number in col 3), ... [represents the total no of balls in the pyramid] and then looked for a number in col 2 that was the same as a number in col 3 which is a pyramid's worth of balls that is also a level's worth of balls [Actually, I didn't bother with col 1 - I could work that one out without writing it down, but it is easier to explain with it there] My solution is ... The first one found was 120, so the solution is 120 + 560 (no in col 3 before the 120 in col 2) = 680 (no in col 3 next to 120 in col 2). (select the text in the gap above, e.g. by dragging the mouse, to read it) Of course, this discounts the possibility that a pyramid might split across multiple levels in a combined pyramid. Just in case I had made a simple arithmetic error, I checked in MS-Excel: Cell A1 = 1, B1 = 1, C1 = 1 Cell A2 = =A1+1, B2 = =A2+B1, C2 = =B2+C1 Cells A3 through C17 = Copy and paste A2 through C2 [I've had to edit this entry twice - suffering from lysdexia (!) today].

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #59

                    Good call on confirming you didn't make an error. I started out on paper and indeed made an error, which I happened to spot when I made the C# program.

                    Thou mewling ill-breeding pignut!

                    1 Reply Last reply
                    0
                    • J jsc42

                      I did this using an old technique: pencil and paper. It is more complicated to explain than it was to do. Basically, I wrote 3 columns of numbers: Col 1: 1, 2, 3, 4, etc [represents the no of levels in the pyramid) Col 2: 1, (corresponding no in col 1 + prev number in col 2), ... [represents the no of new balls in the new level of the pyramid] Col 3: 1, (corresponding no in col 2 + prev number in col 3), ... [represents the total no of balls in the pyramid] and then looked for a number in col 2 that was the same as a number in col 3 which is a pyramid's worth of balls that is also a level's worth of balls [Actually, I didn't bother with col 1 - I could work that one out without writing it down, but it is easier to explain with it there] My solution is ... The first one found was 120, so the solution is 120 + 560 (no in col 3 before the 120 in col 2) = 680 (no in col 3 next to 120 in col 2). (select the text in the gap above, e.g. by dragging the mouse, to read it) Of course, this discounts the possibility that a pyramid might split across multiple levels in a combined pyramid. Just in case I had made a simple arithmetic error, I checked in MS-Excel: Cell A1 = 1, B1 = 1, C1 = 1 Cell A2 = =A1+1, B2 = =A2+B1, C2 = =B2+C1 Cells A3 through C17 = Copy and paste A2 through C2 [I've had to edit this entry twice - suffering from lysdexia (!) today].

                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #60

                      jsc42 wrote:

                      lysdexia

                      :laugh: I thought I was the only one who used that word. Actually, I saw it on some nature show when I was younger and have been using it ever since. :thumbsup:

                      Thou mewling ill-breeding pignut!

                      J 1 Reply Last reply
                      0
                      • S Stephen Dycus

                        That's an assumption though, not a requirement. A requirement would be worded something like: (as he must use every cannonball from both pyramids) The fact that the problem states exactly how many cannon balls there are should tell you something. With 20 cannonballs the best you could do with 2 pyramids is a 3+1 pyramid and a 6+3+1 pyramid with 6 left over. The question asks if the pyramids are not the same size, what would be the minimum. This doesn't magically give you permission to *add* more cannonballs to the equation :P So if it *is* a poorly worded requirement then the answer is 20. Otherwise, it's 4.

                        A Offline
                        A Offline
                        AspDotNetDev
                        wrote on last edited by
                        #61

                        Stephen Dycus wrote:

                        the answer is 20. Otherwise, it's 4

                        Nope and nope. :)

                        Rafiki said:

                        Look harder.

                        Thou mewling ill-breeding pignut!

                        S 1 Reply Last reply
                        0
                        • A AspDotNetDev

                          Stephen Dycus wrote:

                          the answer is 20. Otherwise, it's 4

                          Nope and nope. :)

                          Rafiki said:

                          Look harder.

                          Thou mewling ill-breeding pignut!

                          S Offline
                          S Offline
                          Stephen Dycus
                          wrote on last edited by
                          #62

                          "I will post tomorrow how I arrived at my solution (which may be incorrect, as the book doesn't list what the correct answer is)" You can't say "nope" when you don't even know if you are correct XD

                          A 1 Reply Last reply
                          0
                          • S Stephen Dycus

                            "I will post tomorrow how I arrived at my solution (which may be incorrect, as the book doesn't list what the correct answer is)" You can't say "nope" when you don't even know if you are correct XD

                            A Offline
                            A Offline
                            AspDotNetDev
                            wrote on last edited by
                            #63

                            My not know if I'm correct does not preclude my knowing you are incorrect. :)

                            Thou mewling ill-breeding pignut!

                            S 1 Reply Last reply
                            0
                            • A AspDotNetDev

                              Background

                              I'm reading "The Mammoth Book of IQ Puzzles". It contains, as you may have guessed, a bunch of IQ puzzles. I just solved one of them and thought I'd write the problem down here so you would all have a chance to solve it too. It is called "Cannon-Ball Stacks". In finding the answer, reference materials, books, calculators, and computers are allowed. Since computers are allowed, programs can be created to get the solution.

                              Cannon-Ball Stacks

                              A park ranger has stacked cannon-balls in two tetrahedral pyramids for display at Gettysburg. He later decides to combine the cannon-balls in both of the pyramids in order to create one large pyramid. The smallest number of cannon-balls he can have if the two pyramids are the same size is twenty (assuming he uses every cannon-ball in both pyramids).

                              [10 cannon-ball pyramid] + [10 cannon-ball pyramid] = [20 cannon-ball pyramid]

                              If the two smaller pyramids are different sizes, however, what would be the minimum number of cannon-balls he could use to make one large tetrahedral pyramid? Difficulty: 4 out of 5.

                              AspDotNetDev's Extra Rules

                              Explain how you arrived at the solution. If you create a program to help you solve the problem, paste that in your message. I will reply to this message with the answer in a hidden <span> tag. Don't cheat by looking first though! I will post tomorrow how I arrived at my solution (which may be incorrect, as the book doesn't list what the correct answer is). Points will be awarded for: elegance, quickness, humor, and correcting others (in no particular order). Good luck!

                              Thou mewling ill-breeding pignut!

                              Y Offline
                              Y Offline
                              YvesDaoust
                              wrote on last edited by
                              #64

                              Not-so-brute brute-force: This version is based on a simple test for tetrahedrality: if N = n.(n+1).(n+2), n is the integer cube root of N, so that N gives n gives back the same N. (As one can check, n^3 <= N < (n+1)^3 so that n <= N^1/3 < n+1.) Now all tetrahedral number pairs are tried and their sum tested for tetrahedrality.

                              def T(n):
                              return n * (n + 1) * (n + 2)

                              def IsT(N):
                              return N == T(int(pow(N, 1. / 3.)))

                              for q in range(1, 1000):
                              for p in range(1, q):
                              if IsT(T(p) + T(q)):
                              print T(p) / 6, '+', T(q) / 6, '=', (T(p) + T(q)) / 6

                              1 Reply Last reply
                              0
                              • A AspDotNetDev

                                My not know if I'm correct does not preclude my knowing you are incorrect. :)

                                Thou mewling ill-breeding pignut!

                                S Offline
                                S Offline
                                Stephen Dycus
                                wrote on last edited by
                                #65

                                Yes it does, if you are incorrect then you do not know the correct answer. With 20 cannonballs there are only 3 possible answers: 3+1 = 4 aka MIN 6+3+1 = 10 10+6+3+1 = 20 aka MAX Since my solution IS a tetrahedral pyramid you cannot deduce that my answer is incorrect without knowing the correct answer.

                                A 1 Reply Last reply
                                0
                                • A AspDotNetDev

                                  Points awarded for getting as close as possible to cheating without fully qualifying as cheating. ;P

                                  Thou mewling ill-breeding pignut!

                                  Y Offline
                                  Y Offline
                                  YvesDaoust
                                  wrote on last edited by
                                  #66

                                  ;)

                                  1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    Points for compact code. :thumbsup:

                                    Thou mewling ill-breeding pignut!

                                    Y Offline
                                    Y Offline
                                    YvesDaoust
                                    wrote on last edited by
                                    #67

                                    Thanks. I just released a more efficient compact one.

                                    1 Reply Last reply
                                    0
                                    • R RolfReden

                                      got one, just by accident :) 560 + 120 = 680, which are an 8-layered and a 14-layered pyramid combined to a 15-layered pyramid. still thinking of an smart search or algebraic solution tho [edit for some code] clear clc lines(0) n = 30 m(1) = 1 k(1) = 1 for i = 1:n m(i+1) = m(i) + 1 + (i) k(i+1) = k(i) + m(i+1) end [/edit]

                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #68

                                      RolfReden wrote:

                                      still thinking of an smart search or algebraic solution tho

                                      You could start with this equation:

                                      a(a^2 + 3a + 2) + (a + b)((a + b)^2 + 3(a + b) + 2) = (a + b + c)((a + b + c)^2 + 3(a + b + c) + 2)

                                      The variable (a) is the Nth pyramid for the smallest pyramid. (a + b) is the Nth pyramid for the medium pyramid. (a + b + c) is the Nth pyramid for the large pyramid. Once you have solved for those values, you can plug them into this equation:

                                      n(n + 1)(n + 2)/6

                                      That converts the Nth pyramid into the number of cannon-balls in that pyramid. The first equation will have multiple solutions (infinite solutions?), and some values may be negative. Find the solution where a, b, and c are all positive with the smallest value of (a + b + c), and you have found the solution. :)

                                      Thou mewling ill-breeding pignut!

                                      1 Reply Last reply
                                      0
                                      • S Stephen Dycus

                                        Yes it does, if you are incorrect then you do not know the correct answer. With 20 cannonballs there are only 3 possible answers: 3+1 = 4 aka MIN 6+3+1 = 10 10+6+3+1 = 20 aka MAX Since my solution IS a tetrahedral pyramid you cannot deduce that my answer is incorrect without knowing the correct answer.

                                        A Offline
                                        A Offline
                                        AspDotNetDev
                                        wrote on last edited by
                                        #69

                                        Stephen Dycus wrote:

                                        you cannot deduce that my answer is incorrect without knowing the correct answer

                                        I can, actually. :) Take this equation, for example:

                                        a^2 + b^2 = c^2

                                        Find a solution to this equation where a, b, and c are all positive integers. You might give the solution: a = 1, b = 2, c = 3. Though I may not know a correct answer, I can show that 5 does not equal 9, and so I can show that you are incorrect. Just because I don't know the solution does not mean I can't spot an incorrect solution. :)

                                        Thou mewling ill-breeding pignut!

                                        S 1 Reply Last reply
                                        0
                                        • A AspDotNetDev

                                          Nope, 20 + 54 is not 55.

                                          Thou mewling ill-breeding pignut!

                                          T Offline
                                          T Offline
                                          Thor Sigurdsson
                                          wrote on last edited by
                                          #70

                                          You are quite correct. My brainfarts are legendary. When I said 20 and 54, I did of course mean 20 levels and 54 levels...... 20 levels are 1540 balls and 54 levels are 27720 balls. 55 levels are 29260 balls. Simplified, it can be done:

                                          def onelev(x):
                                          '''The x defines number of balls in a single level given the formula f(x)=(x^2)/2+(x/2)'''
                                          return int((float(x)*float(x))/2.0)+(float(x)/2.0))

                                          def tetra(x):
                                          '''The x defines number of levels of balls in the tetrahedron'''
                                          return sum([onelev(y) for y in range(1,x+1)])

                                          tetra(20)
                                          1540
                                          tetra(54)
                                          27720
                                          tetra(55)-tetra(20)
                                          27720

                                          So I guess my answer was in part correct although not very accurate :) But, looking above, there is a smaller answer ( which I haphazardly ignored since I started to work up from 10 :p ) >>> searchtetra(2,18) 560 120 680 14 8 15 My bad there (yeah, don't solve puzzles while working - you don't have time to verify your mess haha :) )

                                          I=I.am()?Code(I):0/0;

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups