[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
>find the product of a & b without using the operator
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 25
Thread images: 2
File: ForLoopIllustrated[1].png (13 KB, 479x274) Image search: [Google]
ForLoopIllustrated[1].png
13 KB, 479x274
>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
>>
File: Capture.png (232 KB, 592x493) Image search: [Google]
Capture.png
232 KB, 592x493
>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
Thread replies: 25
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.