I'm trying to solve this. But I'm not figuring the right
solution. Can some one help what the answer is for the
question below?
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 can't 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.
1) You can set a variable to 0.
2) You can set a variable = another variable.
3) You can increment a variable (only by 1), and it's a post
increment.
4) 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
wouldn't change so the first line in the loop can change
value of v1 without changing the number of times you loop.
You need to do 3 things.
1) Write a function that decrements by 1.
2) Write a function that subtracts one variable from another.
3) Write a function that divides one variable by another.
4) See if you can implement all 3 using at most 4 variables.
Meaning, you're not making function calls now, you're 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 vars 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 vars in by reference, and you can't declare any new
variables in a function, what you pass in is all it gets.

Answer Posted / 1337

1. Dec:

var DEC(var x)
{
var y = 0;
loop(x)
{
x = y;
y++;
}
return x;
}

var SUB(var x, var y)
{
loop(y)
{
x = DEC(x);
}
return x;
}

// Too lazy to do the rest - you get the idea...
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Given an array all of whose elements are positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7)

749


I HAVE DONE TESTING TOOLS COURSE,NOW I AM FRESHER,I AM NOT GETTING ANY CALLS,I WANT TO DO THE PROJECT ,WHERE I HAVE TO MEET TO DO THE PROJECT,I AM GOING WITH FAKE EXPERIENCE,SO WHAT I HAVE TO DO.

4271


hi, all this is shoba m.c.a . i have learned abap but no oppurtunities right now as fresher , right now i want to learn any course on demand any one pls suggest me good course and institute in hyderabad

1377


we create a pf with 3 fields.2 is defined as keyfields.we lock it with alcobj command.how we find out whether the file is locked or not?is it dspfd??/

1605


Can any one give an example (Source Code) on virtual function implemetation in Java?

1487






which book we learned this mantis? how many version are realsed this mantis upto now?

1413


Which language they use during interview?

1514


what is difference between object oriented programming structure and object oriented programming system?

1428


how many types of bytes are there???

1970


i want to improve my english vocab for racking campus written exam plz tell me best book for prepration......

1693


How do i find out the number of parameters passed into function?

2196


Explain polymorphism. Provide an example.

607


If u need any fake experience certificate in software side, contact me at: vikramyadhav@gmail.com

7818


Write a shell program to test whether a given year is leap year or not ?

2249


You are given some denominations of coins in an array (int denom[])and infinite supply of all of them. Given an amount (int amount), find the minimum number of coins required to get the exact amount. What is the method called?

590