This is an interview question. We have only two constructs
loop(a)
means loop for a times.increment(a)
increments a.Thus to implement a+b one could write
loop(a) {inc(b)}
return b;
The question is how to implement a-b.
How about;
a = 10
b = 8
result = 0
loop(b) {
last = 0
times = 0;
loop(a) {
last = times
times = inc(times)
}
result = a = last
}
result is 2
Js eg;
var a = 10;
var b = 8;
var result;
for (var _b = 0; _b < b; _b++) {
var last = 0, times = 0, loopa = 0;
for (var _a = 0; _a < a; _a++) {
last = times;
times = inc(times);
}
result = a = last;
}
function inc(i) {
return i + 1;
}
print(result) // 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With