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 AspDotNetDev

    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 Offline
    P Offline
    Phil Martin
    wrote on last edited by
    #22

    aspdotnetdev wrote:

    PIEBALDconsult wrote: it's just integer division Not sure I agree (mod is the remainder from the division),

    Sure it is, since your division rounds: a % b = a - b * (a / b) e.g. 12 % 5 = 12 - 5 * (12 / 5) 2 = 12 - 5 * 2 2 = 2

    L A P 3 Replies Last reply
    0
    • P Phil Martin

      aspdotnetdev wrote:

      PIEBALDconsult wrote: it's just integer division Not sure I agree (mod is the remainder from the division),

      Sure it is, since your division rounds: a % b = a - b * (a / b) e.g. 12 % 5 = 12 - 5 * (12 / 5) 2 = 12 - 5 * 2 2 = 2

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

      absolutely, and furthermore PIEBALD will claim the use of the C/C++ pre-processor to introduce and define new operators. :)

      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 P P 3 Replies Last reply
      0
      • P Phil Martin

        aspdotnetdev wrote:

        PIEBALDconsult wrote: it's just integer division Not sure I agree (mod is the remainder from the division),

        Sure it is, since your division rounds: a % b = a - b * (a / b) e.g. 12 % 5 = 12 - 5 * (12 / 5) 2 = 12 - 5 * 2 2 = 2

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

        Thanks for the info. :)

        [Forum Guidelines]

        1 Reply Last reply
        0
        • L Luc Pattyn

          absolutely, and furthermore PIEBALD will claim the use of the C/C++ pre-processor to introduce and define new operators. :)

          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
          #25

          Just so long as he shows his work. :)

          [Forum Guidelines]

          1 Reply Last reply
          0
          • L Luc Pattyn

            absolutely, and furthermore PIEBALD will claim the use of the C/C++ pre-processor to introduce and define new operators. :)

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


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


            P Offline
            P Offline
            Phil Martin
            wrote on last edited by
            #26

            C++ Preprocessor? That's so kitsch. Prolog operators all the way!

            1 Reply Last reply
            0
            • L Luc Pattyn

              it is only / one needs, since that one has a singularity at zero, hence it allows to construct C-like functions (resulting in 0 or 1) like x>0 and x<0, x>=0 and x<=0, and therefore also x==0. :)

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


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


              P Offline
              P Offline
              Phil Martin
              wrote on last edited by
              #27

              Man, all that money wasted on finding the Higgs Boson, when it was hiding in the divide by zero all along!

              L 1 Reply Last reply
              0
              • P Phil Martin

                Man, all that money wasted on finding the Higgs Boson, when it was hiding in the divide by zero all along!

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

                it is not only at zero, it is everywhere, we are just too blind to see it. :)

                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
                • L Luc Pattyn

                  it is not only at zero, it is everywhere, we are just too blind to see it. :)

                  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
                  #29

                  Luc Pattyn wrote:

                  we are just too blind to see it

                  That'll teach you to look directly into a singularity. Those accretion discs will get you every time.

                  [Forum Guidelines]

                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    absolutely, and furthermore PIEBALD will claim the use of the C/C++ pre-processor to introduce and define new operators. :)

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


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


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

                    :-D I was this close to using C rather than C#... Besides, he invented the \ operator. X|

                    L A 2 Replies Last reply
                    0
                    • P PIEBALDconsult

                      :-D I was this close to using C rather than C#... Besides, he invented the \ operator. X|

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

                      PIEBALDconsult wrote:

                      Besides, he invented the \ operator

                      I only know \ from several Basic systems including VB. And I don't really like it, so I did not use it. :)

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


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


                      P 1 Reply Last reply
                      0
                      • P Phil Martin

                        aspdotnetdev wrote:

                        PIEBALDconsult wrote: it's just integer division Not sure I agree (mod is the remainder from the division),

                        Sure it is, since your division rounds: a % b = a - b * (a / b) e.g. 12 % 5 = 12 - 5 * (12 / 5) 2 = 12 - 5 * 2 2 = 2

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

                        Actually, I would likely define it using repeated subtraction; so it should still be allowed. :-D

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          PIEBALDconsult wrote:

                          Besides, he invented the \ operator

                          I only know \ from several Basic systems including VB. And I don't really like it, so I did not use it. :)

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


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


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

                          Huh... I've never heard of it. Turbo BASIC has it. Dartmouth BASIC doesn't seem to. BASIC-Plus doesn't. Seems to be an abomination.

                          1 Reply Last reply
                          0
                          • A AspDotNetDev

                            Luc Pattyn wrote:

                            we are just too blind to see it

                            That'll teach you to look directly into a singularity. Those accretion discs will get you every time.

                            [Forum Guidelines]

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

                            She was only a particle physicist's daughter, but she had big accretion discs. :cool:

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              :-D I was this close to using C rather than C#... Besides, he invented the \ operator. X|

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

                              Different languages have different uses for backslash. I did arbitrarily assign each slash to a particular type of integer division (was not based on any programming language). Gotta keep you on your toes. :)

                              [Forum Guidelines]

                              1 Reply Last reply
                              0
                              • A AspDotNetDev

                                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 Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #36

                                aspdotnetdev wrote:

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

                                Well, you should give a prize then. :)

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                A 1 Reply Last reply
                                0
                                • C CPallini

                                  aspdotnetdev wrote:

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

                                  Well, you should give a prize then. :)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

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

                                  I did, I gave him a nice shiny 5 vote. What better prize is there?

                                  [Forum Guidelines]

                                  C 1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    I did, I gave him a nice shiny 5 vote. What better prize is there?

                                    [Forum Guidelines]

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #38

                                    :beer: :pizza: for instance. :-D

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    A 1 Reply Last reply
                                    0
                                    • C CPallini

                                      :beer: :pizza: for instance. :-D

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                      [My articles]

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

                                      *searches freezer for pizza*

                                      [Forum Guidelines]

                                      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

                                        S Offline
                                        S Offline
                                        supercat9
                                        wrote on last edited by
                                        #40

                                        Here's a smaller approach:

                                                y = x + 7 \* (1 / (x \* x + 1))
                                        

                                        Assuming integer division, and assuming numbers don't overflow, this should work nicely. The expression x*x is never negative, so x*x+1 is always positive and never zero. The expression 1/q is one when q=1, and zero if q>1; I don't think things can get any simpler than that.

                                        A 1 Reply Last reply
                                        0
                                        • S supercat9

                                          Here's a smaller approach:

                                                  y = x + 7 \* (1 / (x \* x + 1))
                                          

                                          Assuming integer division, and assuming numbers don't overflow, this should work nicely. The expression x*x is never negative, so x*x+1 is always positive and never zero. The expression 1/q is one when q=1, and zero if q>1; I don't think things can get any simpler than that.

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

                                          Very nice. And easily verifiable. I like. :thumbsup:

                                          [Forum Guidelines]

                                          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