Math Puzzle (SOLVED by harold aptroot!)
-
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
Actually, I would likely define it using repeated subtraction; so it should still be allowed. :-D
-
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.
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.
-
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.
She was only a particle physicist's daughter, but she had big accretion discs. :cool:
-
:-D I was this close to using C rather than C#... Besides, he invented the
\
operator. X|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. :)
-
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. :)
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] -
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]I did, I gave him a nice shiny 5 vote. What better prize is there?
-
I did, I gave him a nice shiny 5 vote. What better prize is there?
: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] -
: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]*searches freezer for pizza*
-
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
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.
-
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.
Very nice. And easily verifiable. I like. :thumbsup: