Search code examples
pythonfactors

How to ask for a remainder, or a whole number in python?


Im using pYcharm, Im stuck trying to make a program that requests a whole number greater than 1 as inut factors and then returns the prime numbers of the given int. Here is what i have so far:

number = int(input("Enter a positive whole number (<1):"))
f = 2
range = (1, number)
while number > 1:
    while f / number ## Does f divide by number without remainders? as in        
                           does in divide evenly?
        print(f)
        number /= f
    else:
        f += 1

The part I am stuck on, is how do I ask if f / number has a remainder or does it divide evenly?


Solution

  • You want the modulo operator %:

    >>> 5 % 3
    2
    >>> 15 % 5
    0
    

    Specifically a % b == 0 iff b divides a without remainder. Otherwise the remainder is returned.