Python Forum
Pairs of multiplied prime number--->N - 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: Pairs of multiplied prime number--->N (/thread-36078.html)

Pages: 1 2


RE: Pairs of multiplied prime number--->N - deanhystad - Jan-15-2022

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))



RE: Pairs of multiplied prime number--->N - Frankduc - Jan-15-2022

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.


RE: Pairs of multiplied prime number--->N - ibreeden - Jan-16-2022

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


RE: Pairs of multiplied prime number--->N - Frankduc - Jan-16-2022

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