Python Forum
Pairs of multiplied prime number--->N
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pairs of multiplied prime number--->N
#11
This code was close to working.
def prime_eratosthenes(lim):
    prime_list = []
    for i in range(2, lim+1):
        if i not in prime_list:
            print (i, end = ' ')
            for j in range(i*i, lim+1, i):
                prime_list.append(j)
    return prime_list
  
print(prime_eratosthenes(10))
This prints primes in the correct range, but it does not return these values. Why not? Why are you returning prime_list? This is a list of values that are used to identify primes, not a list of primes. In fact, prime_list is a list of numbers that are not primes as indicated by "if i not in prime_list:" being the test to determine if i is prime.
def prime_eratosthenes(lim):
    primes = []
    not_primes = []
    for i in range(2, lim+1):
        if i not in not_primes:
            primes.append(i)
            for j in range(i*i, lim+1, i):
                not_primes.append(j)
    return primes
  
print(prime_eratosthenes(10))
Frankduc likes this post
Reply
#12
I think i got it.

lim = int(input("Generate prime numbers up to what number? : "))
def prime_eratosthenes(lim):
    prime_list = []
    list = []
    for i in range(2, lim+1):
        if i not in prime_list:
            print (i, end = ' ')
            list.append(i)
            for j in range(i*i, lim+1, i):
                prime_list.append(j)
               # print(j)
  
    return list
  
def printPairs(lim):
    primes = prime_eratosthenes(lim)
    for i in range(0, len(primes)):
        for j in range(i+1, len(primes)):
            if (primes[i]*primes[j])==lim:
                print(' ')
                print(primes[i], primes[j])
  
printPairs(lim)
Output:
Generate prime numbers up to what number? : 10 2 3 5 7 2 5
All prime under 10 with the pairs that = 10. (2 , 5)

Thanks for pointing in the right direction. I need to focus on how to make good use of return.
Reply
#13
Hi @Frankduc ,
I am glad you got your program running. But if you don't mind I have two warnings.
First: you named a variable list. But this name is a built-in! Do not use build-ins as a name of a variable because it will hide this built-in! This practice can cause errors that are difficult to understand!
Second: for making the program clear and understandable: choose the names of your variables carefully. See how Deanhystad chose the names "primes" and "not_primes". It makes very clear what they are used for and will help you when you have to maintain the program. In your version you will later have a hard time to understand what the function does when the function returns "list" instead of "prime_list".
ndc85430 likes this post
Reply
#14
(Jan-16-2022, 11:33 AM)ibreeden Wrote: Hi @Frankduc ,
I am glad you got your program running. But if you don't mind I have two warnings.
First: you named a variable list. But this name is a built-in! Do not use build-ins as a name of a variable because it will hide this built-in! This practice can cause errors that are difficult to understand!
Second: for making the program clear and understandable: choose the names of your variables carefully. See how Deanhystad chose the names "primes" and "not_primes". It makes very clear what they are used for and will help you when you have to maintain the program. In your version you will later have a hard time to understand what the function does when the function returns "list" instead of "prime_list".

I did not know about list obviously, i will keep that in mine. I think also it is clear i have started this code by copying others not working code and try fitting parts of it to make something new. I did not bother using new names. I should be more carefull about that. I am wasting to much time searching answers in others code on the net when i should just trusting myself and be more consistant.

I thought in the first def function by returning prime_eratosthenes(lim) at the end of function it was automatically sending the prime numbers to the other def printPairs(lim): by doing primes = prime_eratosthenes(lim). Strange!

Thank you to both of you.
Now i will try to list all combinations possible as an exercise.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pairs Trading Simulation Kiitoos 0 249 Feb-19-2024, 08:27 PM
Last Post: Kiitoos
  Prime number detector Mark17 5 818 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 465 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Extracting unique pairs from a data set based on another value rybina 2 2,309 Feb-12-2021, 08:36 AM
Last Post: rybina
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,921 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  check if the number is a prime integer atlass218 5 2,964 Sep-26-2019, 07:58 AM
Last Post: atlass218
  Key value pairs assistance UtiliseIT 2 2,642 May-09-2019, 09:26 AM
Last Post: UtiliseIT
  Creating a program to look for the largest prime number of a number Wikki14 4 3,921 Sep-08-2018, 12:30 AM
Last Post: Skaperen
  Create random pairs Dennisp44 3 8,019 Jun-02-2018, 05:51 AM
Last Post: buran
  Unexpected result in simple prime number example jackhj 2 3,020 Apr-20-2018, 01:48 AM
Last Post: jackhj

Forum Jump:

User Panel Messages

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