Python Forum
Creating a program to look for the largest prime number of a number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Creating a program to look for the largest prime number of a number (/thread-12609.html)



Creating a program to look for the largest prime number of a number - Wikki14 - Sep-02-2018

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


RE: Creating a program to look for the largest prime number of a number - ichabod801 - Sep-03-2018

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.


RE: Creating a program to look for the largest prime number of a number - Skaperen - Sep-07-2018

it would seem to me that a generator that yields prime numbers in increasing sequence could be useful here.


RE: Creating a program to look for the largest prime number of a number - Gribouillis - Sep-07-2018

(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.


RE: Creating a program to look for the largest prime number of a number - Skaperen - Sep-08-2018

(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 ...