multiply integer a with integer b without using the * operator
language of choice
hardmode: decimals
faggots cant do this
duh just add b to the product a times or vice versa.
decimal point needs to be shifted until you get integers, then shifted back.
>>61696072
write it faggot
>>61696094
not your code monkey.
>>61696005
if a = 0 or b = 0 then return 0 fi;
sign = +1;
if (a < 0) then sign = -sign; a=-a fi;
if (b < 0) then sign = -sign; b=-b fi;
product = exp(ln(a) + ln(b));
if sign < 0 then product = -product fi;
return product;
kek OP it's literally >>61696072 and would take a solid 30 seconds to write
do your own homework faggot
program asd;
var
a,b,p,i:integer;
begin
readln(a);
readln(b);
p:=0;
for i:=1 to b do
p:=p+a;
writeln(p);
end.
>>61696005
easymode (C):
int mul(int a, int b) {
int res;
while (b > 0) {
a += res;
}
}
>>61696630
This is what the compiler can do with it:int mul(int a, int b) { asm volatile ("ud2"); }
The loop never finishes, b will always be > 0.
That aside, the compiler can still nuke your entire routine because integer overflow is undefined and a can overflow.
>>61696630
OMGWTFBBQROTFL
>>61696005
>>61696005(define (* a b)
(cond ((= b 0) 0)
((even? b) (double (* a (/ b 2))))
(else (+ a (* a (- b 1))))))
>>61696005
a/(1/b) :^)
>>61696005
> a / (b/a)
>>61696005
This is in C (Only the relevant code):int vrResult;
int vrNumber1;
int vrNumber2;
int vrTemporary;
int vrCounter;
scanf("%d %d", &vrNumber1, &vrNumber2);
if(vrNumber1 == 0 || vrNumber2 == 0){
vrResult = 0;
printf("%d\n", vrResult);
exit(0);
}
if(vrNumber2 > vrNumber1){
vrTemporary = vrNumber1;
vrNumber1 = vrNumber2;
vrNumber2 = vrTemporary;
}
vrResult = 0;
vrCounter = vrNumber2;
while(vrCounter > 0){
vrResult = vrResult + vrNumber1;
vrCounter = vrCounter - 1;
}
printf("%d", vrResult);
input a, b
c = floor (log_2 (a))
v = 0
for i = c, c >= 0, --d
if a && (1 << c)
v += b << c
return v
>>61697081
oops... i meant i, not c in that loop...
I don't know if this actually works desu, it feels like it should
>>61696890
I've noticed that I havent covered both signs so I'll address that here:char vrSign;
if((vrNumber1 < 0 || vrNumber2 < 0) && !(vrNumber1 < 0 && vrNumber2 < 0)){
vrSign = '-';
}
else{
vrSign = '+';
}
printf("%c%d\n", vrSign, vrResult); // This is to replace the printf() in the previous code
>>61696005
haskell easy mode
>iterate (+a) 0 !! b
>>61697525
forgot negatives desu
>iterate (+a') 0 !! abs(b)
>where a' = if b < 0 then -a else a
>>61696630
We're still waiting for your program to terminate
int mult(int a, int b) {
int c;
asm{
movl $a,%eax
movl $b,%ebx
mul %eax,%ebx
movl %eax, $c
}
return c;
}
>>61696094
pajeets are getting worse by the day
what's your job title? Senior Dev and expert for advanced algorithms?
>>61696072
def kek(a, b):
return a.__mul__(b)
>>61696005
int mult(int a, int b)
{
return a / (1.0/ b) ;
}
int mul(int a, int b) {
int f = (b < 0);
if (f) {
b = -b;
}
int res = 0;
while (b) {
if (b & 1) {
res += a;
}
a <<= 1;
b >>= 1;
}
if (f) {
return -res;
} else {
return res;
}
}
Without the workaround you get an infinite loop, because any right shifts on -1 return -1.
Was about to post the shift-add solution but I'm late.
>>61696663
>undefined instruction for an infinite loop
Yeah, no sane compiler is going to do that. GCC never emits invalid instructions, and Clang only does it as a way of guarding against somehow returning from functions specified as noreturn. It should appear as a regular old infinite loop.
>>61698172
You know, if you want to force a logical instead of arithmetic shift, you could always cast b to unsigned temporarily.
>>61698203
Could always implement it in another language.
>>61696829
impressive
>>61698554
>logical instead of arithmetic shift
I have thought about that, but dealing with the sign only twice instead of every shift seems easier.