Announcement

Collapse
No announcement yet.

Formatting just for fun

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Formatting just for fun

    I ran across a little puzzle today and couldn't figure it out (What else is new?). It seemed a logical thing for a computer to do. And I still couldn't get the answer. (Was a little trick to the answer, one of the numbers had a leading zero.)

    And you can't easily format a leading zero in a 2 digit number.

    Just for fun, here's the puzzle:

    '
    Code:
    #Compile Exe  
    #Include "WIN32API.INC"
     
     
     
    Function PBMain
    'Need to open the safe.  
    'The combination is three 2-digit numbers which 
    'can be expressed like this:
     
    'The total of the three numbers is 39.
    'The second number Is half Of the third number.
    'The first number Is the third number minus 1.
     
      Local m0$, m2$, i1&, i2&, i3& 
     
     m0$ = "No Answer"               
    ' For I3 = 20 To 99 '<< No answer here so:
     For I3 = 1 To 99 'try all combos
       i1 = i3 - 1 'The first number Is the third number minus 1.
       i2 = i3 / 2 'The second number Is half Of the third number.
     
       If I3 + I2 + I1 = 39 Then 'The total of the three numbers is 39.
          m0$ = Using$("##  ##  ## is the wrong answer ", i1, i2, i3) & $CrLf & _
                Using$("##  *0#  ## is the wrong answer ", i1, i2, i3) 
     
          m2$ = "00 00 00 is the right answer "
            Mid$(m2$, 1, 2) = Right$("0" & Trim$(Str$(i1)), 2)
            Mid$(m2$, 4, 2) = Right$("0" & Trim$(Str$(i2)), 2)
            Mid$(m2$, 7, 2) = Right$("0" & Trim$(Str$(i3)), 2)
          Exit For
       End If
     
     Next ctr
     ? m0$,, m2$
     
    End Function
    '
    ===========================================
    "Half this game is ninety percent mental."
    Yogi Berra
    ===========================================
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    Call the numbers X, Y and Z.

    X=Z-1 'The first number Is the third number minus 1.

    Y=Z/2 'The second number Is half Of the third number.


    substitute those values into the original equation and you get:
    X + Y + Z = 39 'The total of the three numbers is 39.
    (Z-1) + (Z/2) + Z = 39

    2.5Z - 1 = 39
    2.5Z = 40
    Z=40/2.5= 16

    Then put Z=16 into the equations for X and Y
    X = Z-1 = 16-1 = 15
    Y = Z/2 = 16/2 = 8

    So the answer is:
    First number is X= 15
    Second number is Y= 8
    Third number is Z= 16

    It's easier to do without a computer.

    Paul.

    Comment


    • #3
      >It's easier to do without a computer.

      You think so? Let's give that puzzle to 100 randomly-selected high-school seniors and see how many can solve it without resorting to trial and error.

      I think the over/under on that might be forty or so.
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Very nice classic puzzle,

        it can be represented using system of linear equations.
        Lets call first number X, second Y, third Z.

        'The total of the three numbers is 39.
        x + y + z = 39

        'The second number Is half Of the third number.
        y = 0.5z

        'The first number Is the third number minus 1.
        x = z - 1

        So you can transform it to shape:
        Code:
        1.0x + 1.0y + 1.0z = 39
        0.0x + 1.0y - 0.5z =  0
        1.0x + 0.0y - 1.0z = -1
        This can be solved ( with a bit of luck on determinant :P ) using Cramers rule, see following code:
        Code:
        #COMPILE EXE
        #INCLUDE "WIN32API.INC"
        
        MACRO DisplayMatrix3x3( matrix, title )
          MSGBOX FORMAT$(matrix(1, 1), "0.00") + $TAB + FORMAT$(matrix(1, 2), "0.00")  + $TAB + FORMAT$(matrix(1, 3), "0.00") +$CRLF+ _
                 FORMAT$(matrix(2, 1), "0.00") + $TAB + FORMAT$(matrix(2, 2), "0.00")  + $TAB + FORMAT$(matrix(2, 3), "0.00") +$CRLF+ _
                 FORMAT$(matrix(3, 1), "0.00") + $TAB + FORMAT$(matrix(3, 2), "0.00")  + $TAB + FORMAT$(matrix(3, 3), "0.00"), , title
        
        END MACRO
        
        MACRO DisplayMatrix3x1( matrix, title )
          MSGBOX FORMAT$(matrix(1, 1), "0.00") + $CRLF + FORMAT$(matrix(2, 1), "0.00")  + $CRLF + FORMAT$(matrix(3, 1), "0.00"), , title
        END MACRO
        
        FUNCTION PBMAIN()
        
          ' Coefficients
          DIM MatrixLeft(1 TO 3, 1 TO 3) AS DOUBLE
        
          ' Values
          DIM MatrixRight(1 TO 3, 1 TO 1) AS DOUBLE
        
          ' Results buffer
          DIM MatrixResult(1 TO 3, 1 TO 1) AS DOUBLE
          
          ' Fill coefficients
          MatrixLeft(1, 1) = 1
          MatrixLeft(1, 2) = 1
          MatrixLeft(1, 3) = 1
        
          MatrixLeft(2, 1) =  0
          MatrixLeft(2, 2) =  1
          MatrixLeft(2, 3) = -0.5
        
          MatrixLeft(3, 1) =  1
          MatrixLeft(3, 2) =  0
          MatrixLeft(3, 3) = -1
          
          ' Fill values
          ARRAY ASSIGN MatrixRight() =  39, _
                                         0, _
                                        -1
          ' Use the Cramer, Luke
          CALL Solve_Cramer(MatrixLeft(), MatrixRight(), MatrixResult())
          
          DisplayMatrix3x3(MatrixLeft, "Left matrix = coefficients")
          DisplayMatrix3x1(MatrixRight, "Right matrix = values")
          DisplayMatrix3x1(MatrixResult, "Results")
          
        
        END FUNCTION
        
        ' Calculates determinant of 3x3 matrix
        FUNCTION Get_Determinant(matrix3x3() AS DOUBLE) AS DOUBLE
          FUNCTION = matrix3x3(1, 1) * matrix3x3(2, 2) * matrix3x3(3, 3) + _
                     matrix3x3(1, 2) * matrix3x3(2, 3) * matrix3x3(3, 1) + _
                     matrix3x3(1, 3) * matrix3x3(2, 1) * matrix3x3(3, 2) - _
                     matrix3x3(1, 3) * matrix3x3(2, 2) * matrix3x3(3, 1) - _
                     matrix3x3(1, 2) * matrix3x3(2, 1) * matrix3x3(3, 3) - _
                     matrix3x3(1, 1) * matrix3x3(2, 3) * matrix3x3(3, 2)
        END FUNCTION
        
        ' Inserts column to 3x3 matrix
        FUNCTION Insert_Column(matrix3x3() AS DOUBLE, matrix3x1() AS DOUBLE, columnNum AS LONG) AS DOUBLE
        
          matrix3x3(1, columnNum) = matrix3x1(1, 1)
          matrix3x3(2, columnNum) = matrix3x1(2, 1)
          matrix3x3(3, columnNum) = matrix3x1(3, 1)
        
        END FUNCTION
        
        ' Allmighty Cramer
        SUB Solve_Cramer( matrix3x3() AS DOUBLE, matrix3x1() AS DOUBLE, matrix3x1Result() AS DOUBLE )
          REGISTER c AS LONG
        
          LOCAL determinantBase, determinantTemp AS DOUBLE
          DIM MatrixCopy3x3(1 TO 3, 1 TO 3) AS DOUBLE
          DIM MatrixCopy3x1(1 TO 3, 1 TO 1) AS DOUBLE
        
          ' Determinant of original matrix
          determinantBase = Get_Determinant(matrix3x3())
          IF determinantBase = 0 THEN
            MSGBOX "Well, cramer won't help :("
            EXIT SUB
          END IF
        
          ' Determinant of modified matrices
          MAT MatrixCopy3x1() = matrix3x1()
          FOR c = 1 TO 3
            MAT MatrixCopy3x3() = matrix3x3()
            Insert_Column(MatrixCopy3x3(), MatrixCopy3x1(), c)
            determinantTemp = Get_Determinant(MatrixCopy3x3())
            
            matrix3x1Result(c) = determinantTemp/determinantBase
          NEXT
        
        END SUB
        I do not say this is optimized, I do not say this is the best way to do it, but from matrix point of view it seems quite straightforward to me
        Thanks for interesting little challenge, it took me a while to remind me of Cramers rule.


        Bye,
        Petr
        Last edited by Petr Schreiber jr; 22 Aug 2008, 12:34 PM.
        [email protected]

        Comment


        • #5
          Gosta was having fun with the formatting, not the math.

          If he was having fun with the math he'd have never run his FOR/NEXT loop past 39, if that far. He would have started it at 0 as well.

          Somehow, I think he's having more fun now.
          Rod
          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

          Comment


          • #6
            Personally, I prefer a simpler, more direct method ;
            Code:
            FUNCTION PBMAIN
            'Need to open the safe.
            'The combination is three 2-digit numbers which
            'can be expressed like this:
            
            'The total of the three numbers is 39.
            'The second number Is half Of the third number.
            'The first number Is the third number minus 1.
            
                LOCAL x,y,z AS LONG
                LOCAL test1, test2, test3 AS SINGLE
                f$ = "##  "
                COLOR 14,1
                CLS
            
                FOR x = 1 TO 39
                FOR y = 1 TO 39
                FOR z = 1 TO 39
            
                test1 = x + y + z
                test2 = z / 2
                test3 = z - 1
            
                IF test1 = 39 AND _
                   test2 = y  AND _
                   test3 = x  THEN
            
                PRINT;test1,USING$(f$,x) + USING$(f$,y) + USING$(f$,z)
                END IF
            
                NEXT : NEXT : NEXT
            
            WAITKEY$
            END FUNCTION
            There are no atheists in a fox hole or the morning of a math test.
            If my flag offends you, I'll help you pack.

            Comment


            • #7
              Originally posted by Rodney Hicks View Post
              Gosta was having fun with the formatting, not the math.

              If he was having fun with the math he'd have never run his FOR/NEXT loop past 39, if that far. He would have started it at 0 as well.

              Somehow, I think he's having more fun now.
              {grin}


              =======================================
              Waste not fresh tears over old griefs.
              Euripides
              =======================================
              It's a pretty day. I hope you enjoy it.

              Gösta

              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

              Comment


              • #8
                Gosta was having fun with the formatting, not the math.
                Ooops

                And you can't easily format a leading zero in a 2 digit number.
                What about following?:
                Code:
                FORMAT$( i1, "00" )+" "+FORMAT$( i2, "00" )+" "+FORMAT$( i3, "00" )
                It ... works.
                Hope this time I solved what I was asked for, if not ... I am tired, very tired


                Petr
                [email protected]

                Comment


                • #9
                  Originally posted by Petr Schreiber jr View Post
                  Ooops



                  What about following?:
                  Code:
                  FORMAT$( i1, "00" )+" "+FORMAT$( i2, "00" )+" "+FORMAT$( i3, "00" )
                  It ... works.
                  Hope this time I solved what I was asked for, if not ... I am tired, very tired


                  Petr
                  Not too tired to get the right answer though. Works a charm.
                  It's a pretty day. I hope you enjoy it.

                  Gösta

                  JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                  LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                  Comment


                  • #10
                    However loathsome it is to me personally, as an epigone of the MCM school of programming thought, I must reluctantly point out the incorrect answers:

                    Originally posted by Paul Dixon View Post
                    Call the numbers X, Y and Z.

                    ..Then put Z=16 into the equations for X and Y
                    X = Z-1 = 16-1 = 15
                    Y = Z/2 = 16/2 = 8

                    So the answer is:
                    First number is X= 15
                    Second number is Y= 8
                    Third number is Z= 16

                    It's easier to do without a computer.

                    Paul.
                    Easier maybe but not the correct answer (no leading zero for y)

                    Mel said ...Personally, I prefer a simpler, more direct method ...
                    Simpler maybe but not the correct answer (no leading zero either)

                    Petr said:I do not say this is optimized, I do not say this is the best way to do it, but from matrix point of view it seems quite straightforward to me
                    Thanks for interesting little challenge, it took me a while to remind me of Cramers rule.
                    Straightforward? Maybe, but incorrect. NLZ plus all the answers had too many characters ("15.00", etc.) . And here I thought all along Cramer's Rule was to "Sponge off everybody." {USA inside joke}

                    ====================================
                    "If everything seems under control,
                    you're just not going fast enough."
                    Mario Andretti
                    ====================================
                    It's a pretty day. I hope you enjoy it.

                    Gösta

                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                    Comment

                    Working...
                    X