X is a new programming language which allows only the following operations –
- You can assign zero to a variable ( as in a = 0 )
- You can assign one variable to another ( a=b )
- You can do the postincrement operation ( a++ )
- Negative numbers dont exist in the language. So negative numbers are taken as 0.
- loop(10){ //code } will execute the code ten times.
- You cant have comparison operators or if conditionals or bitwise operators.
Write a function to implement division in this language.
My solution so far –
Since division is repeated subtraction I’ll first implement subtraction.
function decrement(var a)
{
var x;
loop(a)
{
x = a++;
}
return x;
}
function subtract( var a, var b )
{
//returns a-b
var x;
loop(b)
{
x=decrement(a);
}
return x;
}
Now, how do i implement the division function using this subtraction?
Or any other solution without using this subtraction is also fine.
1 Answer