multiply integer a with integer b without using the * operator
language of choice
hardmode: decimals
faggots cant do this
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.
write it faggot
not your code monkey.
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 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.
easymode (C):
int mul(int a, int b) {
int res;
while (b > 0) {
a += res;
}
}
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.
OMGWTFBBQROTFL
...
(define (* a b)
(cond ((= b 0) 0)
((even? b) (double (* a (/ b 2))))
(else (+ a (* a (- b 1))))))
a/(1/b) :^)
> a / (b/a)
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
I already told you, I'm not going to tell you again
oops... i meant i, not c in that loop...
I don't know if this actually works desu, it feels like it should
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
haskell easy mode
>iterate (+a) 0 !! b
forgot negatives desu
>iterate (+a') 0 !! abs(b)
>where a' = if b < 0 then -a else a
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;
}
pajeets are getting worse by the day
what's your job title? Senior Dev and expert for advanced algorithms?
def kek(a, b):
return a.__mul__(b)
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;
}
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.
>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.
You know, if you want to force a logical instead of arithmetic shift, you could always cast b to unsigned temporarily.
Could always implement it in another language.
impressive
>logical instead of arithmetic shift
I have thought about that, but dealing with the sign only twice instead of every shift seems easier.