View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m.
Examples:
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4
Method 1: Naive approach
The naive approach is to find the quotient using the double division [//] operator and remainder using the modulus [%] operator.
Example:
Python3
def
find[n, m]:
q
=
n
/
/
m
print
[
"Quotient: "
, q]
r
=
n
%
m
print
[
"Remainder"
, r]
find[
10
,
3
]
find[
99
,
5
]
Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
Time Complexity: O[1]
Auxiliary Space: O[1]
Method 2: Using divmod[] method
Divmod[] method takes two numbers as parameters and returns the tuple containing both quotient and remainder.
Example:
Python3
q, r
=
divmod
[
10
,
3
]
print
[
"Quotient: "
, q]
print
[
"Remainder: "
, r]
q, r
=
divmod
[
99
,
5
]
print
[
"Quotient: "
, q]
print
[
"Remainder: "
, r]
Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
Time Complexity: O[1]
Auxiliary Space: O[1]
In Python, you can calculate the quotient with //
and the remainder with %
.
q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
The built-in function divmod[]
is useful when you want both the quotient and remainder.
- Built-in Functions - divmod[] — Python 3.7.4 documentation
divmod[a, b]
returns a tuple [a // b, a % b]
.
You can unpack and assign to each variable.
- Unpack a tuple and list in Python
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
Of course, you can receive it as a tuple.
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
Modulo is performed in the integer context, not fractional [remainders are integers]. Therefore:
1 % 1 = 0 [1 times 1 plus 0]
1 % 2 = 1 [2 times 0 plus 1]
1 % 3 = 1 [3 times 0 plus 1]
6 % 3 = 0 [3 times 2 plus 0]
7 % 3 = 1 [3 times 2 plus 1]
8 % 3 = 2 [3 times 2 plus 2]
etc
How do I get the actual remainder of x / y?
By that I presume you mean doing a regular floating point division?
for i in range[2, 11]:
print 1.0 / i
answered Feb 4, 2009 at 1:10
codelogiccodelogic
70k9 gold badges58 silver badges54 bronze badges
2
I think you can get the result you want by doing something like this:
for i in range[2, 11]:
print 1.0*[1 % i] / i
This computes the [integer] remainder as explained by others. Then you divide by the denominator again, to produce the fractional part of the quotient.
Note that I multiply the result of the modulo operation by 1.0 to ensure that a floating point division operation is done [rather than integer division, which will result in 0].
answered Feb 4, 2009 at 1:37
Greg HewgillGreg Hewgill
909k177 gold badges1131 silver badges1267 bronze badges
You've confused division and modulus.
"0.5, 0.333333, 0.25" etc. as I expected [1/2 = 0.5, etc]."
That's the result of division.
Not modulus.
Modulus [%
] is the remainder left over after integer division.
Your sample values are simple division, which is the /
operator. Not the %
operator.
answered Feb 4, 2009 at 2:08
S.LottS.Lott
376k78 gold badges503 silver badges771 bronze badges
Wouldn't dividing 1 by an number larger than it result in 0 with remainder 1?
The number theorists in the crowd may correct me, but I think modulus/remainder is defined only on integers.
answered Feb 4, 2009 at 1:11
DanaDana
30.9k17 gold badges62 silver badges72 bronze badges
3
We can have 2 types of division, that we can define through the return types:
Float: a/b. For example: 3/2=1.5
def division[a,b]:
return a/b
Int: a//b and a%b. For example: 3//2=1 and 3%2=1
def quotient[a,b]:
return a//b
def remainder[a,b]:
return a%b
answered Jun 13, 2017 at 11:00