Anyone know the solution to this? Happened to see it while browsing through an old HDD.
int result = 0;
while(b > 0)
{
result += a;
b--;
}
return result;
int product(int a, int b) {
int res = 0;
while (b--)
res += a;
return res;
}
>>60815603
Yeh this is what I thought of too, I just remember some anon in a previous thread using division in their solution
public int product(int a, int b) {
return Math.multiplyExact(a, b);
}
>>60815671
https://en.wikipedia.org/wiki/Two%27s_complement#Multiplication
>>60815545public int Product(int a, int b) {
return product(a, b);
}
private int product(int a, int b) {
return a * b;
}
>>60815671
Wouldn't that just be a/(1/b) then?
>>60815743
kek
import Data.Bits
add :: Integer -> Integer -> Integer
add a 0 = a
add a b = add x y
where x = a `xor` b
y = (a .&. b) `shiftL` 1
[Code] for I in range(b):
A=+1 [/code]
Print("A")
>not just using a switch statementpublic int Product(int a, int b) {
switch (b) {
case 1:
return a;
case 2:
return a+a;
case 3:
return a+a+a;
case 4:
return a+a+a+a;
case 5:
return a+a+a+a+a;
case 6:
return a+a+a+a+a+a;
case 7:
return a+a+a+a+a+a+a;
case 8:
return a+a+a+a+a+a+a+a;
case 9:
return a+a+a+a+a+a+a+a+a;
case 10:
return a+a+a+a+a+a+a+a+a+a;
default:
throw new IllegalArgumentException("The multiplier is too large.");
}
}
>>60815655
Wait, wouldn't that loop forever or does b-- automagically stop when b reaches zero?
>>60816450
There are no booleans in C. 0 is FALSE, and anything not 0 is TRUE. So,while(b--)would decrement b until it was 0.
>>60816479
I mean, they're just as much booleans as C++/Java when you look under the hood. Even in other languages with dedicated bool types, they're still going to be byte aligned, so you still have at least a single byte integer, with some free compile time error checking
>>60815743
Yeh that was it I think, thanks
>>60815545int mult(int x, int y){
int result = 0;
do{
result += (y & 1)? x : 0;
x <<= 1;
}while((y >>= 1) != 0);
return result;
}
>>60815733
ho shit, had to do this shit all the time in uni
>>60816801
Sorry you went to a shit uni anon.
Looks like a lot of code here isn't handling integer overflow.
>>60816933
>/g/
>good code
Pick only one.
>>60816450
Jesus.
>>60815655
> int b
> b = -1