>find the product of a & b without using the operator *
I'm not the only guy who solved this by not using a for loop and instead dividing a by the reciprocal of b, right?
Am I retarded?
>>54713399
I mean, I guess I wasn't in the context of a class and therefore didn't know that I was supposed to use a for loop.
>>54713399
generally, when a problem tells you not to use multiplication, it's also bad form to use division.
also,
>>>/g/sqt/
>>54713399
It depends if the mean
>don't *explicitly* use *
or
>don't multiply a and b
You multiplied a and b but you didn't explicitly used *. I wouldn't do the same as you tho.
>>54713399
your method is superior anon
for loop would only work with ints
>>54713399
I've done this in binary by using addition and shifting
>tfw cant do this
maximum ironing
>Not knowing that multiplication is just repeated addition
OB BLS.
>>54714417
this
for (i = 0; i < b; i++){
product += a;
}
/thread
>>54714457
Of course you'd have to make this a bit more complicated if you're multiplying non-integers
>>54713399
>Am I retarded?
Yes. Your method doesn't work at all when b is 0. Also, assuming you aren't using arbitrary precision arithmetic, you will either potentially lose information if you do integer division, or potentially get a rounding error if you use IEEE.
>>54714457
Operates in O(b), when it could operate in O(log(b)) if you used shift and add.
>>54714457
-3 * -5 = ???
>>54713399
a / (1 / b) = a * b
>>54714951
LOOK IT'S Ruby !OffRailsfU THANKS GOD HE IS USING HIS AMAZING HACKER TRIPCODE (WOW HOW DID YOU DO THIS?!) SO WE KNOW THE POST MUST BE HIGH DEFINITION QUALITY!!!
>>54715059
Except for b = 0
>>54715077
use a try catch jesus
>>54715086
You flatter me, anon, but I'm not Jesus.
>>54715096
i flattered ur mom last nite m8
Now that I think about things, it should also fail for multipliers of 2 or greater.
If b >= 2, 1 / b = 0 using integer division. Dividing by this always causes an arithmetic exception.
>>54715068
Why are there so many tripfags recently on every damn board?
>>54715198
Reddit invasion, most likely thanks to /tv/ and /co/.
Using Bitwise
#include<stdio.h>
main()
{
int a, b, result;
printf("\nEnter the numbers to be multiplied:");
scanf("%d%d", &a, &b); // a > b
result = 0;
while (b != 0) // Iterate the loop till b == 0
{
if (b & 01) // Bitwise & of the value of b with 01
{
result = result + a; // Add a to result if b is odd .
}
a<<=1; // Left shifting the value contained in 'a' by 1
// Multiplies a by 2 for each loop
b>>=1; // Right shifting the value contained in 'b' by 1.
}
printf("nResult:%d",result);
}
>>54713399
You are suppose to use recursion
>>54713399
>I'm not the only guy who solved this by not using a for loop and instead dividing a by the reciprocal of b, right?
Explain to me, Bobby, how that works with integer types.
Then, for bonus points, explain to me how that works with floating point types.
>not using inline assembly