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. Math Puzzle (SOLVED by harold aptroot!)

Math Puzzle (SOLVED by harold aptroot!)

Scheduled Pinned Locked Moved The Lounge
csscomquestion
41 Posts 8 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.
  • A Offline
    A Offline
    AspDotNetDev
    wrote on last edited by
    #1

    Sorry folks, harold aptroot beat you. Better luck next time. :) So a coworker of mine was just converting between different representations for the day of the week. In one system 0 means Sunday and in the other 7 means Sunday (all other days are equal between both systems). He was able to solve it using a simple if statement, but that got me thinking about a mathematical way of solving that (just for fun). I came up with:

    newDay = ((oldDay + 2) / (oldDay + 1) - 1) * 7 + oldDay

    That makes new day equal to the old day, except the 0 gets turned into a 7. However, it only works for non-negative integers. Any value less than -1 will be incorrect and -1 will cause a division by 0, which for the sake of the puzzle I am disallowing. Your goal is to create an equation that will handle negatives correctly too. So, the input and output would look like this:

    Old Value

    New Value

    -∞

    -∞

    ...

    ...

    -2

    -2

    -1

    -1

    0

    7

    1

    1

    2

    2

    ...

    ...

    ∞

    ∞

    You are only allowed multiplication, addition, subtraction, negation, integer division, and parentheses for grouping. Assume "/" truncates the result (so, -2.5 becomes -2 and 2.5 becomes 2) and "\" rounds toward negative infinity (so, -2.5 becomes -3 and 2.5 becomes 2). Division by 0 is not allowed. The first one to get a correct solution gets 5 kudos points. Have fun. :) P.S. This is not a homework question. I graduated years ago. Though, teachers, feel free to snag this little puzzle for your math students (or programming students, as this is more a logic puzzle that just happens to employ very basic math). If nobody has solved this by the time I get home in a couple hours, I'll go ahead and give it a go myself then post the answer here (assuming I can figure it out).

    [Forum Guidelines]

    modified on Tuesday, April 20, 2010 10:16 PM

    L P G L S 5 Replies Last reply
    0
    • A AspDotNetDev

      Sorry folks, harold aptroot beat you. Better luck next time. :) So a coworker of mine was just converting between different representations for the day of the week. In one system 0 means Sunday and in the other 7 means Sunday (all other days are equal between both systems). He was able to solve it using a simple if statement, but that got me thinking about a mathematical way of solving that (just for fun). I came up with:

      newDay = ((oldDay + 2) / (oldDay + 1) - 1) * 7 + oldDay

      That makes new day equal to the old day, except the 0 gets turned into a 7. However, it only works for non-negative integers. Any value less than -1 will be incorrect and -1 will cause a division by 0, which for the sake of the puzzle I am disallowing. Your goal is to create an equation that will handle negatives correctly too. So, the input and output would look like this:

      Old Value

      New Value

      -∞

      -∞

      ...

      ...

      -2

      -2

      -1

      -1

      0

      7

      1

      1

      2

      2

      ...

      ...

      ∞

      ∞

      You are only allowed multiplication, addition, subtraction, negation, integer division, and parentheses for grouping. Assume "/" truncates the result (so, -2.5 becomes -2 and 2.5 becomes 2) and "\" rounds toward negative infinity (so, -2.5 becomes -3 and 2.5 becomes 2). Division by 0 is not allowed. The first one to get a correct solution gets 5 kudos points. Have fun. :) P.S. This is not a homework question. I graduated years ago. Though, teachers, feel free to snag this little puzzle for your math students (or programming students, as this is more a logic puzzle that just happens to employ very basic math). If nobody has solved this by the time I get home in a couple hours, I'll go ahead and give it a go myself then post the answer here (assuming I can figure it out).

      [Forum Guidelines]

      modified on Tuesday, April 20, 2010 10:16 PM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Well.. I have an incorrect 'solution' It uses "banned" operations.. ((((x - 1) >> 31) | ((1 - x) >> 31)) & 7) | (((~((x - 1) >> 31)) | (x >> 31)) & x) edit: aah typo

      modified on Tuesday, April 20, 2010 9:36 PM

      A 1 Reply Last reply
      0
      • A AspDotNetDev

        Sorry folks, harold aptroot beat you. Better luck next time. :) So a coworker of mine was just converting between different representations for the day of the week. In one system 0 means Sunday and in the other 7 means Sunday (all other days are equal between both systems). He was able to solve it using a simple if statement, but that got me thinking about a mathematical way of solving that (just for fun). I came up with:

        newDay = ((oldDay + 2) / (oldDay + 1) - 1) * 7 + oldDay

        That makes new day equal to the old day, except the 0 gets turned into a 7. However, it only works for non-negative integers. Any value less than -1 will be incorrect and -1 will cause a division by 0, which for the sake of the puzzle I am disallowing. Your goal is to create an equation that will handle negatives correctly too. So, the input and output would look like this:

        Old Value

        New Value

        -∞

        -∞

        ...

        ...

        -2

        -2

        -1

        -1

        0

        7

        1

        1

        2

        2

        ...

        ...

        ∞

        ∞

        You are only allowed multiplication, addition, subtraction, negation, integer division, and parentheses for grouping. Assume "/" truncates the result (so, -2.5 becomes -2 and 2.5 becomes 2) and "\" rounds toward negative infinity (so, -2.5 becomes -3 and 2.5 becomes 2). Division by 0 is not allowed. The first one to get a correct solution gets 5 kudos points. Have fun. :) P.S. This is not a homework question. I graduated years ago. Though, teachers, feel free to snag this little puzzle for your math students (or programming students, as this is more a logic puzzle that just happens to employ very basic math). If nobody has solved this by the time I get home in a couple hours, I'll go ahead and give it a go myself then post the answer here (assuming I can figure it out).

        [Forum Guidelines]

        modified on Tuesday, April 20, 2010 10:16 PM

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I wouldn't bother with values outside 0 -- 7, so:

        private static int
        F
        (
        int X
        )
        {
        return ( ( X - 7 ) % 7 + 7 ) ;
        }

        A 1 Reply Last reply
        0
        • P PIEBALDconsult

          I wouldn't bother with values outside 0 -- 7, so:

          private static int
          F
          (
          int X
          )
          {
          return ( ( X - 7 ) % 7 + 7 ) ;
          }

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

          PIEBALDconsult wrote:

          %

          Sorry, not allowed. Not on the list of approved operators.

          PIEBALDconsult wrote:

          I wouldn't bother with values outside 0 -- 7

          Sorry, negative to positive infinity. Please try again. :)

          [Forum Guidelines]

          P 1 Reply Last reply
          0
          • L Lost User

            Well.. I have an incorrect 'solution' It uses "banned" operations.. ((((x - 1) >> 31) | ((1 - x) >> 31)) & 7) | (((~((x - 1) >> 31)) | (x >> 31)) & x) edit: aah typo

            modified on Tuesday, April 20, 2010 9:36 PM

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

            32-bit, eh? I'm assuming an environment in which numbers approach infinity (positive and negative). Please try again. :)

            [Forum Guidelines]

            L 1 Reply Last reply
            0
            • A AspDotNetDev

              PIEBALDconsult wrote:

              %

              Sorry, not allowed. Not on the list of approved operators.

              PIEBALDconsult wrote:

              I wouldn't bother with values outside 0 -- 7

              Sorry, negative to positive infinity. Please try again. :)

              [Forum Guidelines]

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              aspdotnetdev wrote:

              not allowed

              Sure it is, it's just integer division.

              aspdotnetdev wrote:

              negative to positive infinity

              Tough crap, it's a ridiculous requirement, not needed for your stated usage.

              A 1 Reply Last reply
              0
              • P PIEBALDconsult

                aspdotnetdev wrote:

                not allowed

                Sure it is, it's just integer division.

                aspdotnetdev wrote:

                negative to positive infinity

                Tough crap, it's a ridiculous requirement, not needed for your stated usage.

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

                Calm down there cowboy. My "stated usage" was just how I thought up this idea. This is just a fun math puzzle to occupy your time should you choose to let it.

                PIEBALDconsult wrote:

                it's just integer division

                Not sure I agree (mod is the remainder from the division), but no shorthand allowed... show your work please. :)

                [Forum Guidelines]

                P 1 Reply Last reply
                0
                • A AspDotNetDev

                  32-bit, eh? I'm assuming an environment in which numbers approach infinity (positive and negative). Please try again. :)

                  [Forum Guidelines]

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  ((x - 1) / 2 - (x - 1) \ 2) * ((-(x + 1) / 2) - (-(x + 1) \ 2)) * 7 + x edit: forgot negation - oops

                  A 2 Replies Last reply
                  0
                  • A AspDotNetDev

                    Sorry folks, harold aptroot beat you. Better luck next time. :) So a coworker of mine was just converting between different representations for the day of the week. In one system 0 means Sunday and in the other 7 means Sunday (all other days are equal between both systems). He was able to solve it using a simple if statement, but that got me thinking about a mathematical way of solving that (just for fun). I came up with:

                    newDay = ((oldDay + 2) / (oldDay + 1) - 1) * 7 + oldDay

                    That makes new day equal to the old day, except the 0 gets turned into a 7. However, it only works for non-negative integers. Any value less than -1 will be incorrect and -1 will cause a division by 0, which for the sake of the puzzle I am disallowing. Your goal is to create an equation that will handle negatives correctly too. So, the input and output would look like this:

                    Old Value

                    New Value

                    -∞

                    -∞

                    ...

                    ...

                    -2

                    -2

                    -1

                    -1

                    0

                    7

                    1

                    1

                    2

                    2

                    ...

                    ...

                    ∞

                    ∞

                    You are only allowed multiplication, addition, subtraction, negation, integer division, and parentheses for grouping. Assume "/" truncates the result (so, -2.5 becomes -2 and 2.5 becomes 2) and "\" rounds toward negative infinity (so, -2.5 becomes -3 and 2.5 becomes 2). Division by 0 is not allowed. The first one to get a correct solution gets 5 kudos points. Have fun. :) P.S. This is not a homework question. I graduated years ago. Though, teachers, feel free to snag this little puzzle for your math students (or programming students, as this is more a logic puzzle that just happens to employ very basic math). If nobody has solved this by the time I get home in a couple hours, I'll go ahead and give it a go myself then post the answer here (assuming I can figure it out).

                    [Forum Guidelines]

                    modified on Tuesday, April 20, 2010 10:16 PM

                    G Offline
                    G Offline
                    Gwenio
                    wrote on last edited by
                    #9

                    It cannot be done within the given specifications, as the function is discontinuous.

                    A 1 Reply Last reply
                    0
                    • L Lost User

                      ((x - 1) / 2 - (x - 1) \ 2) * ((-(x + 1) / 2) - (-(x + 1) \ 2)) * 7 + x edit: forgot negation - oops

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

                      Dang, I was just about to tell you that you were incorrect. Alright, let me get the pen and paper out again... please hold...

                      [Forum Guidelines]

                      1 Reply Last reply
                      0
                      • G Gwenio

                        It cannot be done within the given specifications, as the function is discontinuous.

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

                        Discontinuous functions are quite possible using the operators I've allowed. In fact, I demonstrated one in my original post.

                        [Forum Guidelines]

                        G 1 Reply Last reply
                        0
                        • A AspDotNetDev

                          Sorry folks, harold aptroot beat you. Better luck next time. :) So a coworker of mine was just converting between different representations for the day of the week. In one system 0 means Sunday and in the other 7 means Sunday (all other days are equal between both systems). He was able to solve it using a simple if statement, but that got me thinking about a mathematical way of solving that (just for fun). I came up with:

                          newDay = ((oldDay + 2) / (oldDay + 1) - 1) * 7 + oldDay

                          That makes new day equal to the old day, except the 0 gets turned into a 7. However, it only works for non-negative integers. Any value less than -1 will be incorrect and -1 will cause a division by 0, which for the sake of the puzzle I am disallowing. Your goal is to create an equation that will handle negatives correctly too. So, the input and output would look like this:

                          Old Value

                          New Value

                          -∞

                          -∞

                          ...

                          ...

                          -2

                          -2

                          -1

                          -1

                          0

                          7

                          1

                          1

                          2

                          2

                          ...

                          ...

                          ∞

                          ∞

                          You are only allowed multiplication, addition, subtraction, negation, integer division, and parentheses for grouping. Assume "/" truncates the result (so, -2.5 becomes -2 and 2.5 becomes 2) and "\" rounds toward negative infinity (so, -2.5 becomes -3 and 2.5 becomes 2). Division by 0 is not allowed. The first one to get a correct solution gets 5 kudos points. Have fun. :) P.S. This is not a homework question. I graduated years ago. Though, teachers, feel free to snag this little puzzle for your math students (or programming students, as this is more a logic puzzle that just happens to employ very basic math). If nobody has solved this by the time I get home in a couple hours, I'll go ahead and give it a go myself then post the answer here (assuming I can figure it out).

                          [Forum Guidelines]

                          modified on Tuesday, April 20, 2010 10:16 PM

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          y=x+7*(1+x-(x+x+1)/2)*(1-x+(x+x-1)/2)

                          :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          Prolific encyclopedia fixture proof-reader browser patron addict?
                          We all depend on the beast below.


                          modified on Tuesday, April 20, 2010 10:12 PM

                          A 1 Reply Last reply
                          0
                          • L Lost User

                            ((x - 1) / 2 - (x - 1) \ 2) * ((-(x + 1) / 2) - (-(x + 1) \ 2)) * 7 + x edit: forgot negation - oops

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

                            This seems to be correct. Very clever. Looks like you are using the fact that \ and / will only ever differ by 1. Since you flip flop so rounding will occur in the same direction for at least one of the terms to the left and right of the multiply, at least one section of the equation will turn out to be 0, which propogates to make each other terms 0. And you never divide by a variable, so that avoids a divide by 0. A 5 well earned!

                            [Forum Guidelines]

                            L 1 Reply Last reply
                            0
                            • A AspDotNetDev

                              Discontinuous functions are quite possible using the operators I've allowed. In fact, I demonstrated one in my original post.

                              [Forum Guidelines]

                              G Offline
                              G Offline
                              Gwenio
                              wrote on last edited by
                              #14

                              For some reason I was stuck on thinking in terms of normal math. Still, you should really make sure such a problem has an answer before presenting it as a puzzle.

                              A 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                y=x+7*(1+x-(x+x+1)/2)*(1-x+(x+x-1)/2)

                                :)

                                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                Prolific encyclopedia fixture proof-reader browser patron addict?
                                We all depend on the beast below.


                                modified on Tuesday, April 20, 2010 10:12 PM

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

                                Good choice to remove the decimals... I can't recall if I stated that in the puzzle, but that was part of the requirement I had in my head. I don't have the time to check your solution, but it looks like harold aptroot beat you anyway. Yours does look a little less tricky (I too was thinking of using "2 * i + 1"), so kudos for possibly finding a simpler solution. :)

                                [Forum Guidelines]

                                L 1 Reply Last reply
                                0
                                • A AspDotNetDev

                                  Good choice to remove the decimals... I can't recall if I stated that in the puzzle, but that was part of the requirement I had in my head. I don't have the time to check your solution, but it looks like harold aptroot beat you anyway. Yours does look a little less tricky (I too was thinking of using "2 * i + 1"), so kudos for possibly finding a simpler solution. :)

                                  [Forum Guidelines]

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  no need for \! :)

                                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                  Prolific encyclopedia fixture proof-reader browser patron addict?
                                  We all depend on the beast below.


                                  A 1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    This seems to be correct. Very clever. Looks like you are using the fact that \ and / will only ever differ by 1. Since you flip flop so rounding will occur in the same direction for at least one of the terms to the left and right of the multiply, at least one section of the equation will turn out to be 0, which propogates to make each other terms 0. And you never divide by a variable, so that avoids a divide by 0. A 5 well earned!

                                    [Forum Guidelines]

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    Yay :) Usually trying to solve these problems at 4 am doesn't work out so well for me lol

                                    A 1 Reply Last reply
                                    0
                                    • G Gwenio

                                      For some reason I was stuck on thinking in terms of normal math. Still, you should really make sure such a problem has an answer before presenting it as a puzzle.

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

                                      Gwenio wrote:

                                      you should really make sure such a problem has an answer before presenting it as a puzzle

                                      You might want to tell these guys that. ;) Also, I intuited that it was possible. If it wasn't, then a proof of why it wasn't would have been equally valid. :)

                                      [Forum Guidelines]

                                      C 1 Reply Last reply
                                      0
                                      • L Lost User

                                        Yay :) Usually trying to solve these problems at 4 am doesn't work out so well for me lol

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

                                        4AM may not be such a bad time for you. You have earned a place on my bookmark list. :)

                                        [Forum Guidelines]

                                        1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          no need for \! :)

                                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                                          Prolific encyclopedia fixture proof-reader browser patron addict?
                                          We all depend on the beast below.


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

                                          Not bad. I added that operator because I wasn't sure it was possible without it. If you have indeed done it without, then great work. :)

                                          [Forum Guidelines]

                                          L 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