This question already has answers here:
Find the division remainder of a number
(13 answers)
Closed 2 years ago.
In python, after I have divided to variables (k and h) How do I then find the remainder of the equation? This is the code I have:
h = int(input())
print(k / h)
Simple math does the trick:
h = int(input())
print(k % h)
Related
This question already has answers here:
How to calculate a mod b in Python?
(7 answers)
Closed 1 year ago.
I want to make the number begin from zero if it is more than twenty-five. For example,
x = 24 + 10 must be 8. How can I do that?
You can use the modulus operator, or %. a % b finds the remainder when you divide a by b. If you want to keep the number at 25 or lower, you should take the mod with 26.
def add_and_wrap(n1, n2, wrap_at):
return (n1 + n2) % (wrap_at + 1)
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
Why isn't q equal to b as the formula for modulo is: a % b = a - (a/b)*b ?
x = (int(time.time())*100)
q = x % 360
b = x - (x/360)*360
print(x)
print(q)
print(b)
This question already has answers here:
Exponentials in python: x**y vs math.pow(x, y)
(7 answers)
Closed 3 years ago.
I've been trying to solve this problem:
The cube at the bottom will have a volume of n^3, the cube above will
have volume of (n-1)^3 and so on until the top which will have a
volume of 1^3.
You are given the total volume m of the building. Being given m can
you find the number n of cubes you will have to build?
And this is my code
import math
def find_nb(m):
nb = 1
nb_vol = 0
while True:
nb_vol += math.pow(nb, 3)
if (nb_vol == m):
return nb
elif (nb_vol > m):
return -1
nb += 1
Now, when I try to solve for find_nb(2521115597681328384) it returns -1 when it should in fact return 56352. If I change
nb_vol += math.pow(nb, 3)
to
nb_vol += nb ** 3
Everything works correctly. Why?
math.pow always converts it’s arguments to floats first, which are only approximations.
The ** operator uses integers that can never overflow, which means that it always gives a correct result.
This question already has answers here:
Exponentials in python: x**y vs math.pow(x, y)
(7 answers)
Closed 3 years ago.
I've been trying to solve this problem:
The cube at the bottom will have a volume of n^3, the cube above will
have volume of (n-1)^3 and so on until the top which will have a
volume of 1^3.
You are given the total volume m of the building. Being given m can
you find the number n of cubes you will have to build?
And this is my code
import math
def find_nb(m):
nb = 1
nb_vol = 0
while True:
nb_vol += math.pow(nb, 3)
if (nb_vol == m):
return nb
elif (nb_vol > m):
return -1
nb += 1
Now, when I try to solve for find_nb(2521115597681328384) it returns -1 when it should in fact return 56352. If I change
nb_vol += math.pow(nb, 3)
to
nb_vol += nb ** 3
Everything works correctly. Why?
math.pow always converts it’s arguments to floats first, which are only approximations.
The ** operator uses integers that can never overflow, which means that it always gives a correct result.
This question already has answers here:
What is the best way to get all the divisors of a number?
(18 answers)
Prime factorization - list
(17 answers)
Closed 5 years ago.
I just started to open for myself programming in python. I know how to find factors for example of 30. I will get [1,2,3,5,6,10,15,30].
But I need this output 30 = 2 * 3 * 5. Thank you in advance!
Since you're trying to find all prime, unique factors, I'd use the following function:
def factor(numberToFactor, arr=list()):
i = 2
maximum = numberToFactor / 2 + 1
while i < maximum:
if numberToFactor % i == 0:
return factor(numberToFactor/i,arr + [i])
i += 1
return list(set(arr + [numberToFactor]))
print(factor(59511555)) # [3, 5, 1747, 757]