# A Mind Teaser – good one..!

To improve our programming skills and as a part of self-improvement exercise we are supposed to spend sometime every week as a part of Company Policy. Last fortnight my team has put their mind on test with one puzzle (passed on to us by other programming department) and it went quite well for everyone.

It was more about basic math rather than programming and it was good to see everyone refreshing their mind with roots of math/programming. I am sharing this puzzle/question on my blog and want my reader to put their mind and some of their time to crack this and put down their results as comments on this post.

Here we go:

Assume that you have a abstract computer, so forget everything you know about computers, this one only does what I am about to tell you it does.

- You can use as many variables as you need,
- There are no negative numbers,
- All numbers are integers.
- You do not know the size of the integers, they could be infinitely large, so you cannot count on truncating at any point.
- There are NO comparisons allowed, no if statements or anything like that.
There are only four operations you can do on a variable.

- You can set a variable to 0.
- You can set a variable = another variable.
- You can increment a variable (only by 1), and it is a post increment.
- You can loop. So, if you were to say loop(v1) and v1 = 10, your loop would execute 10 times, but the value in v1 would not change so the first line in the loop can change value of v1 without hanging the number of times you loop.

You need to perform 3 operations:

- Write a function that decrements by 1.
- Write a function that subtracts one variable from another.
- Write a function that divides one variable by another.

(See if you can implement all 3 using at most 4 variables.)Meaning, you are not making function calls now, you are making macros. And at most you can have 4 variables.

The restriction really only applies to divide, the other 2 are easy to do with 4 variables or less. Division on the other hand is dependent on the other 2 functions, so, if subtract requires 3 variables, then divide only has 1 variable left unchanged after a call to subtract.

Basically, just make your function calls to decrement and subtract so you pass your variables in by reference, and you can not declare any new variables in a function, what you pass in is all it gets.

Although one can search through Internet to get the answer to this Question but it would be fun to see how long it takes one to crack it and how much of 3 operations.

Readers if you try this, please write down your scores (Time and no of Operations) as a comment. I am sure I don't need to put on the answer here.

## 14 Responses