+ 2

integer division without *or/?

how can i do integer division without using * or/?

18th Sep 2018, 3:22 PM
sama baluom
sama baluom - avatar
12 Réponses
+ 2
You are welcome, sama baluom! Here's the code for positive numbers: ------------------- #finding the quotient of the division a/b a = 100 b = 7 quotient = 0 while a >= b: a -= b quotient += 1 print(quotient) ------------------- Let me know if anything looks confusing :)
18th Sep 2018, 4:08 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
I'm assuming you don't want to use // or divmod() or any other sneaky method... Use the idea that division is repeated subtraction. For positive numbers, use a loop to keep subtracting the divisor from the dividend until the remainder is smaller than the divisor. Use a counter to keep track of the number of times you had to subtract. The final value of the counter is the quotient. Hope that helps :)
18th Sep 2018, 3:41 PM
Kishalaya Saha
Kishalaya Saha - avatar
18th Sep 2018, 4:47 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 1
thanks!
18th Sep 2018, 3:46 PM
sama baluom
sama baluom - avatar
+ 1
thanks a lot^^ its pretty clear now for me , but was wondering if I can use input so that I can change the value everytime..?
18th Sep 2018, 4:12 PM
sama baluom
sama baluom - avatar
+ 1
why are you trying to do this(´・ω・`) well, there's a method faster than keep subtracting take 12345/111 for example get first 3 digits of 12345, which is 123 keep subtracting 111 and count until it's less than 111, and you will get 1 as first digit of quotion and rest 12 then plus the fourth digits after 12, which becomes 124 keep subtracting and count then get 1...13 by doing this, just like how we do with hand, will be fast if you don't want to use * or //
18th Sep 2018, 4:14 PM
Flandre Scarlet
Flandre Scarlet - avatar
+ 1
sama baluom Sure! Modify it to a = int(input()) b = int(input()) at the beginning.
18th Sep 2018, 4:14 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Flandre Scarlet! thanks for answering me but i couldn't understand how to code that ? , should I use slicing or what?
18th Sep 2018, 4:23 PM
sama baluom
sama baluom - avatar
+ 1
thank you♡ waiting for it
18th Sep 2018, 4:27 PM
sama baluom
sama baluom - avatar
+ 1
thank you;)
18th Sep 2018, 4:54 PM
sama baluom
sama baluom - avatar
0
Flandre Scarlet haha! You adapted the method for integer long division! Nice!
18th Sep 2018, 4:24 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
sama baluom i'll write it at code playground, wait for a while😀
18th Sep 2018, 4:24 PM
Flandre Scarlet
Flandre Scarlet - avatar