Python Forum

Full Version: Creating a program to look for the largest prime number of a number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I'm creating a code that will take on number look for all the factors of that number and then tell me what factors are prime and which ones are not
num = (random number) <==This a preinputed number.

def print_factors(x):
    print("factors of", x,"are:")
    for i in range(1,x + 1):
        if x % i ==0:
            print(i)
print_factors(num)
I think the way to do it would be to have the program put all the factors into a list and then run a prime number program through it but I don't know how to do that and would love the help/criticism or whatever you have.
Thanks for the help, Wikki14
Please use python tags when posting code, see the BBCode link in my signature below for instructions. I did it for you this time.

I would both sets of factors at the same time. Start with two and go up. The first factor you find is guaranteed to be prime. Put that in the prime factors list. Then every factor you find is either evenly divisible by one of the prime factors, or another prime factor.
it would seem to me that a generator that yields prime numbers in increasing sequence could be useful here.
(Sep-02-2018, 11:53 PM)Wikki14 Wrote: [ -> ]put all the factors into a list and then run a prime number program
The smallest factor (after 1) is necessarily a prime number. If you find it, you can divide x by this factor.
(Sep-07-2018, 04:57 AM)Gribouillis Wrote: [ -> ]
(Sep-02-2018, 11:53 PM)Wikki14 Wrote: [ -> ]put all the factors into a list and then run a prime number program
The smallest factor (after 1) is necessarily a prime number. If you find it, you can divide x by this factor.
then after you divide x by this (smallest) factor, apply all this to the quotient you get. its smallest factor is also prime ...