Python Forum
Creating a program to look for the largest prime number of a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a program to look for the largest prime number of a number
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
it would seem to me that a generator that yields prime numbers in increasing sequence could be useful here.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(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.
Reply
#5
(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 ...
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 1 215 Mar-19-2024, 08:31 PM
Last Post: deanhystad
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 281 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 735 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 870 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 461 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  capturing multiline output for number of parameters jss 3 766 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR
  doubling a number Skaperen 8 1,144 Jul-25-2023, 10:20 PM
Last Post: Skaperen
Question Extracting Version Number from a String britesc 2 1,030 May-31-2023, 10:20 AM
Last Post: britesc
  Formatting float number output barryjo 2 878 May-04-2023, 02:04 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020