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. I give up

I give up

Scheduled Pinned Locked Moved The Lounge
helpsalesquestion
36 Posts 23 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.
  • M Matias Szulman

    Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

    L Offline
    L Offline
    lucy 0
    wrote on last edited by
    #3

    don't think the clerk gave the correct change. I did this and it returned no answer: bool bAnswer = false; for ( nApple = 0; !bAnswer && nApple < 19.99/0.29; nApple ++ ) { nOrange = (19.99 - nApple * 0.29) / 0.39; if ( (nOrange * 0.39 + nApple * 0.29) == 19.99 ) bAnswer = true; } return bAnswer;

    S 1 Reply Last reply
    0
    • M Matias Szulman

      Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

      S Offline
      S Offline
      Steve Mayfield
      wrote on last edited by
      #4

      There isn't a single equation - but you do have constraints The maximum number of apples is 68 (if no oranges are bought) The maximum number of oranges is 51 (if no apples are bought) so,

      for(a = 0; a <= 68; a++)
      {
      if((1999 - a * 29) % 39 == 0)
      printf("apples = %d, oranges = %d\n", a, (1999 - a * 29) / 39)
      }

      There is only one match a = 38 and o = 23 Steve

      M G 2 Replies Last reply
      0
      • L lucy 0

        don't think the clerk gave the correct change. I did this and it returned no answer: bool bAnswer = false; for ( nApple = 0; !bAnswer && nApple < 19.99/0.29; nApple ++ ) { nOrange = (19.99 - nApple * 0.29) / 0.39; if ( (nOrange * 0.39 + nApple * 0.29) == 19.99 ) bAnswer = true; } return bAnswer;

        S Offline
        S Offline
        Steve Mayfield
        wrote on last edited by
        #5

        There are problems with floating point rounding errors in your solution which is why you did not get a match. Steve

        L 1 Reply Last reply
        0
        • S Steve Mayfield

          There isn't a single equation - but you do have constraints The maximum number of apples is 68 (if no oranges are bought) The maximum number of oranges is 51 (if no apples are bought) so,

          for(a = 0; a <= 68; a++)
          {
          if((1999 - a * 29) % 39 == 0)
          printf("apples = %d, oranges = %d\n", a, (1999 - a * 29) / 39)
          }

          There is only one match a = 38 and o = 23 Steve

          M Offline
          M Offline
          Matias Szulman
          wrote on last edited by
          #6

          Thanks!

          1 Reply Last reply
          0
          • M Matias Szulman

            Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

            B Offline
            B Offline
            BlackDice
            wrote on last edited by
            #7

            38 apples and 23 oranges 38 * 29 + 23 * 39 = 1999 okay, okay - I cheated. I wrote a quick VB program to figure it out for me.

            Private Sub Command1_Click()
            Dim i As Integer
            Dim j As Integer
            Dim intApples As Integer
            Dim intOranges As Integer
            intApples = 29
            intOranges = 39
            For i = 1 To 40
              For j = 1 To 40
                If intApples * i + intOranges * j = 1999 Then
                MsgBox CStr(i) & " apples and " & CStr(j) & " oranges"
                Exit Sub
                End If
              Next
            Next
            End Sub
            

            My articles www.stillwaterexpress.com BlackDice

            1 Reply Last reply
            0
            • M Matias Szulman

              Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #8

              Read what Michael wrote... Two fors should solve it quickly :) pseudocode:

              // 100 is naive upper estimation for sulution, 100*29 > 1999
              for i= 0 to 100 do for j=0 to 100 do if(29*i+39*j == 1999) then print(i, j);

              There (probably) is logical way how to find solution... but brute force is OK too :) Don't give up math after hour or so... better take a rest, solution will come to your mind suddenly - on toilet, on the way to work or while playing some 3D-action ;) Ahh yeah and I forgot about mirror solutions... (j,i) is same as (i,j) :-O David Never forget: "Stay kul and happy" (I.A.)
              David's thoughts / dnhsoftware.org / MyHTMLTidy

              S 1 Reply Last reply
              0
              • D DavidNohejl

                Read what Michael wrote... Two fors should solve it quickly :) pseudocode:

                // 100 is naive upper estimation for sulution, 100*29 > 1999
                for i= 0 to 100 do for j=0 to 100 do if(29*i+39*j == 1999) then print(i, j);

                There (probably) is logical way how to find solution... but brute force is OK too :) Don't give up math after hour or so... better take a rest, solution will come to your mind suddenly - on toilet, on the way to work or while playing some 3D-action ;) Ahh yeah and I forgot about mirror solutions... (j,i) is same as (i,j) :-O David Never forget: "Stay kul and happy" (I.A.)
                David's thoughts / dnhsoftware.org / MyHTMLTidy

                S Offline
                S Offline
                Steve Mayfield
                wrote on last edited by
                #9

                Mirror solution will not work because the cost is different for apples and oranges. 38*29+23*39=1999 23*29+38*39=2149 Steve

                D 1 Reply Last reply
                0
                • S Steve Mayfield

                  Mirror solution will not work because the cost is different for apples and oranges. 38*29+23*39=1999 23*29+38*39=2149 Steve

                  D Offline
                  D Offline
                  DavidNohejl
                  wrote on last edited by
                  #10

                  :doh: that's true David Never forget: "Stay kul and happy" (I.A.)
                  David's thoughts / dnhsoftware.org / MyHTMLTidy

                  1 Reply Last reply
                  0
                  • M Matias Szulman

                    Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

                    S Offline
                    S Offline
                    Srikanth Remani
                    wrote on last edited by
                    #11

                    a for apple, o for orange 29a + 39o = 1999 29(a + o) + 10o = 1999 lets call a + o = t (total) 29t + 10o = 1999 we know o is a whole number, o = (1999 - 29t)/10, for this to be a whole number 29t would be ***9 (has 9 in units place) so t would be *1 (has 1 in units place) so our probable answer set (for total fruits) are 1, 11, 21, 31, 41, 51, 61, 71 ... for lower limit a=0, so t = o (all fruits are oranges, since oranges are costlier) 39t = 1999, t = 1999/39 = 51.256 (just more than 51) for upper limit o = 0, so t =a (all fruits are apples, since apples are cheaper) 29t = 1999, t = 1999/29 = 68.931 (just less than 70). the only number that ends with *1 in this range is 61, so t = 61. so, 29*61 + 10o = 1999, 10o = 1999 - 1769, o = 230/10 = 23. a = 61- 23 = 38.

                    L G R V Y 5 Replies Last reply
                    0
                    • S Steve Mayfield

                      There isn't a single equation - but you do have constraints The maximum number of apples is 68 (if no oranges are bought) The maximum number of oranges is 51 (if no apples are bought) so,

                      for(a = 0; a <= 68; a++)
                      {
                      if((1999 - a * 29) % 39 == 0)
                      printf("apples = %d, oranges = %d\n", a, (1999 - a * 29) / 39)
                      }

                      There is only one match a = 38 and o = 23 Steve

                      G Offline
                      G Offline
                      Gary Wheeler
                      wrote on last edited by
                      #12

                      Does your solution still work if you run the application on a MacTM? (ducks) :laugh:


                      Software Zen: delete this;

                      S 1 Reply Last reply
                      0
                      • S Srikanth Remani

                        a for apple, o for orange 29a + 39o = 1999 29(a + o) + 10o = 1999 lets call a + o = t (total) 29t + 10o = 1999 we know o is a whole number, o = (1999 - 29t)/10, for this to be a whole number 29t would be ***9 (has 9 in units place) so t would be *1 (has 1 in units place) so our probable answer set (for total fruits) are 1, 11, 21, 31, 41, 51, 61, 71 ... for lower limit a=0, so t = o (all fruits are oranges, since oranges are costlier) 39t = 1999, t = 1999/39 = 51.256 (just more than 51) for upper limit o = 0, so t =a (all fruits are apples, since apples are cheaper) 29t = 1999, t = 1999/29 = 68.931 (just less than 70). the only number that ends with *1 in this range is 61, so t = 61. so, 29*61 + 10o = 1999, 10o = 1999 - 1769, o = 230/10 = 23. a = 61- 23 = 38.

                        L Offline
                        L Offline
                        lucy 0
                        wrote on last edited by
                        #13

                        brilliant!

                        1 Reply Last reply
                        0
                        • S Steve Mayfield

                          There are problems with floating point rounding errors in your solution which is why you did not get a match. Steve

                          L Offline
                          L Offline
                          lucy 0
                          wrote on last edited by
                          #14

                          ouch, :-O

                          1 Reply Last reply
                          0
                          • S Srikanth Remani

                            a for apple, o for orange 29a + 39o = 1999 29(a + o) + 10o = 1999 lets call a + o = t (total) 29t + 10o = 1999 we know o is a whole number, o = (1999 - 29t)/10, for this to be a whole number 29t would be ***9 (has 9 in units place) so t would be *1 (has 1 in units place) so our probable answer set (for total fruits) are 1, 11, 21, 31, 41, 51, 61, 71 ... for lower limit a=0, so t = o (all fruits are oranges, since oranges are costlier) 39t = 1999, t = 1999/39 = 51.256 (just more than 51) for upper limit o = 0, so t =a (all fruits are apples, since apples are cheaper) 29t = 1999, t = 1999/29 = 68.931 (just less than 70). the only number that ends with *1 in this range is 61, so t = 61. so, 29*61 + 10o = 1999, 10o = 1999 - 1769, o = 230/10 = 23. a = 61- 23 = 38.

                            G Offline
                            G Offline
                            Graham Bradshaw
                            wrote on last edited by
                            #15

                            That's a beautifully worked argument! :-D

                            1 Reply Last reply
                            0
                            • M Matias Szulman

                              Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

                              I Offline
                              I Offline
                              igor1960
                              wrote on last edited by
                              #16

                              29a + 39o = 1999 a + o = 61 <= because if we assume that only apples you bought, then we get 1999/29=68. and if we assume that only oranges: 1999/39=51.25 meaning that you got >=52 and <=68 overall. However, the price you paid ends with 9 (1999) -- as each cost ends with 9 (29,39) --> the multiplier must finish with 1. The only value between 52 and 68 with 1 at the end is 61. So, resolve the system 29a + 39o =1999 a+o=61 =========== 29a + 39(61-a)=1999 29a + 39*61-39a=1999 -10a=1999-39*61 -10a=1999-2379 -10a=-380 a=38 o=61-38=23 :laugh: "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

                              1 Reply Last reply
                              0
                              • M Matias Szulman

                                Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

                                G Offline
                                G Offline
                                generic_user_id
                                wrote on last edited by
                                #17

                                I'll let the solution speak for itself: (Using Haskell set logic) solution :: [(Int, Int)] solution = [ (apples, oranges) | apples <- [0..100], oranges <- [0..100], apples * 29 + oranges * 39 == 1999 ] Result:

                                [(38,23)]

                                Hence, the answer must be 38 apples. Haskell really shines sometimes. I don't think solutions really get more elegant than this. :-D

                                J 1 Reply Last reply
                                0
                                • S Srikanth Remani

                                  a for apple, o for orange 29a + 39o = 1999 29(a + o) + 10o = 1999 lets call a + o = t (total) 29t + 10o = 1999 we know o is a whole number, o = (1999 - 29t)/10, for this to be a whole number 29t would be ***9 (has 9 in units place) so t would be *1 (has 1 in units place) so our probable answer set (for total fruits) are 1, 11, 21, 31, 41, 51, 61, 71 ... for lower limit a=0, so t = o (all fruits are oranges, since oranges are costlier) 39t = 1999, t = 1999/39 = 51.256 (just more than 51) for upper limit o = 0, so t =a (all fruits are apples, since apples are cheaper) 29t = 1999, t = 1999/29 = 68.931 (just less than 70). the only number that ends with *1 in this range is 61, so t = 61. so, 29*61 + 10o = 1999, 10o = 1999 - 1769, o = 230/10 = 23. a = 61- 23 = 38.

                                  R Offline
                                  R Offline
                                  Rui A Rebelo
                                  wrote on last edited by
                                  #18

                                  Very nice! :) Rui A. Rebelo Computers are useless, they can only provide answers. Pablo Picasso

                                  1 Reply Last reply
                                  0
                                  • M Matias Szulman

                                    Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

                                    B Offline
                                    B Offline
                                    Brit
                                    wrote on last edited by
                                    #19

                                    Srikanth had the same idea I did, so I thought I'd offer a different solution. How many apples can I get for 1999 or less? 68 (with 27 pennies left over). Knowing that, (a) 1 apple + 10 pennies = 1 orange (b) 4 apples + 1 penny = 3 oranges In order to use up my 27 pennies, I do two "a" exchanges (costing a total of 20 pennies) to get me down to 7 pennies and then do 7 "b" exchanges (costing 7 pennies). 68 apples + 27 pennies --> 66 apples + 2 oranges + 7 pennies --> (66-28) apples + (2+21) oranges + 0 pennies = 38 apples + 23 oranges + 0 pennies left ----------------------------------------------------- Empires Of Steel[^]

                                    V 1 Reply Last reply
                                    0
                                    • M Matias Szulman

                                      Today I realised that math is definitively not for me. For fun I was trying to solve some puzzles, and I've been stuck with this one for about an hour. I know some of you really get it, so could I ask for some help? Thanks! A fruitstand in my neighborhood sells apples for 29 cents apiece, and oranges for 39 cents apiece. One day I went there and filled a basket with a mixture of apples and oranges. When I paid for them I handed the clerk a $20 bill, and she handed me back my change ... one penny. How many apples did I buy? (Assume there was no sales tax charged.) Matias

                                      B Offline
                                      B Offline
                                      Bassam Abdul Baki
                                      wrote on last edited by
                                      #20

                                      29a + 39o = 1999 29a ≤ 1999 ⇒ 0 ≤ a ≤ 68 39o ≤ 1999 ⇒ 0 ≤ o ≤ 51 29a + 39o ≡ 9a + 9o = 9(a + o) ≡ 9 (mod 10) ∴ a + o ≡ 1 (mod 10) Since 29 and 39 do not divide 1999, 29(a + o) < 29a + 39o < 39(a + 0) ⇒ 51 < a + o < 68. ∴ a + o = 61. a + o = 61 and 29a + 39o = 1999 ⇒ a = 38 and o = 23. "For that one fraction of a second, you were open to options you would never have considered. That is the exploration that awaits you. Not mapping stars and studying nebula, but charting the unknown possibilities of existence." - Q (Star Trek: The Next Generation) Web - Blog

                                      S S 2 Replies Last reply
                                      0
                                      • G Gary Wheeler

                                        Does your solution still work if you run the application on a MacTM? (ducks) :laugh:


                                        Software Zen: delete this;

                                        S Offline
                                        S Offline
                                        Steve Mayfield
                                        wrote on last edited by
                                        #21

                                        Only if the case color is orange :laugh: Steve

                                        1 Reply Last reply
                                        0
                                        • B Brit

                                          Srikanth had the same idea I did, so I thought I'd offer a different solution. How many apples can I get for 1999 or less? 68 (with 27 pennies left over). Knowing that, (a) 1 apple + 10 pennies = 1 orange (b) 4 apples + 1 penny = 3 oranges In order to use up my 27 pennies, I do two "a" exchanges (costing a total of 20 pennies) to get me down to 7 pennies and then do 7 "b" exchanges (costing 7 pennies). 68 apples + 27 pennies --> 66 apples + 2 oranges + 7 pennies --> (66-28) apples + (2+21) oranges + 0 pennies = 38 apples + 23 oranges + 0 pennies left ----------------------------------------------------- Empires Of Steel[^]

                                          V Offline
                                          V Offline
                                          Vivi Chellappa
                                          wrote on last edited by
                                          #22

                                          The combination of one apple and one orange costs 68 cents. 10 apples and 10 oranges will cost you $6.80. If you picked 30 apples and 30 oranges, the price comes to $20.40. You remove one orange and the price becomes $20.01, tantalizingly close to $19.99! Now, you need to figure out how many oranges you have to take away and how many apples to add till you get a price difference of 2 cents. 8 apples will cost you $2.32 and 6 oranges will cost you $2.34. Better still, when you round the prices to 30 cents an apple and 40 cents an orange, you see immediately that 8x30 = 6x40 and the difference in count (between 8 and 6) is precisely the 2 cents that you are looking for. Hence 38 apples and 23 (29-6) oranges. No programs. No pencil and paper. No simultaneous equations. Just simple mental arithmetic. Next question please! :cool:

                                          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