Math Puzzle (SOLVED by harold aptroot!)
-
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 typomodified on Tuesday, April 20, 2010 9:36 PM
32-bit, eh? I'm assuming an environment in which numbers approach infinity (positive and negative). Please try again. :)
-
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. :)
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.
-
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.
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. :)
-
32-bit, eh? I'm assuming an environment in which numbers approach infinity (positive and negative). Please try again. :)
-
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).
modified on Tuesday, April 20, 2010 10:16 PM
-
((x - 1) / 2 - (x - 1) \ 2) * ((-(x + 1) / 2) - (-(x + 1) \ 2)) * 7 + x
edit: forgot negation - oopsDang, I was just about to tell you that you were incorrect. Alright, let me get the pen and paper out again... please hold...
-
Discontinuous functions are quite possible using the operators I've allowed. In fact, I demonstrated one in my original post.
-
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).
modified on Tuesday, April 20, 2010 10:16 PM
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
-
((x - 1) / 2 - (x - 1) \ 2) * ((-(x + 1) / 2) - (-(x + 1) \ 2)) * 7 + x
edit: forgot negation - oopsThis 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!
-
Discontinuous functions are quite possible using the operators I've allowed. In fact, I demonstrated one in my original post.
-
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
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. :)
-
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. :)
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.
-
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!
-
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.
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. :)
-
Yay :) Usually trying to solve these problems at 4 am doesn't work out so well for me lol
4AM may not be such a bad time for you. You have earned a place on my bookmark list. :)
-
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.
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. :)
-
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. :)
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.
-
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. :)
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
-
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
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.
-
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
Thanks for the info. :)