Why is this modulo equation wrong when it shouldn't? [duplicate] - python

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)

Related

How to loop some amount of number in python [duplicate]

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)

How to find the remainder of a division in python? [duplicate]

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)

Python3 does not truncate the decimals of automatically like python2 [duplicate]

This question already has answers here:
Integer division in Python 2 and Python 3
(4 answers)
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 2 years ago.
In python2, if you did
n = 12
n /= 10
n would become 1.
In python 2,
the above would cause n to be 1.2, even if it were passed in an integer parameter like
def foo(self, n: int) -> bool:
print (n / 10)
return True
The simple fix is to just cast it to an integer like so:
n = int(n/10)
But this is quite memory/time costly. Are there better alternatives in python3?
// is integer division, so it removes the decimal from the result.

How to find the prime factors of a number in Python? [duplicate]

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]

How to carry out to 3 decimals rather than ints in the list? [duplicate]

This question already has answers here:
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 6 years ago.
So a list from 0 to 100 is created and I apply the formula in line two to make list B. The issue is that it's rounded to an int and I have been trying how to get it out to 3 decimals.
A = range(0,101)
B = [(x-10)/3 for x in A]
Ex:
If A = 11
(11-10)/3 = 0.333 instead of (11-10)/3 = 0.
Thanks!
You can use the function round(B, 3)
this should work
from __future__ import division
A = range(0,101)
B = [(x-10)/3 for x in A]

Categories