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


Pairs of multiplied prime number--->N - Frankduc - Jan-14-2022

Hello,

I cant fix this bug. I am trying to find pairs of prime numbers for a series of prime till N.

I get message errors:

Generate prime numbers up to what number? : 10
Traceback (most recent call last):
File "<string>", line 22, in <module>
File "<string>", line 17, in printPairs
TypeError: 'NoneType' object cannot be interpreted as an integer
>


lim = int(input("Generate prime numbers up to what number? : "))
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)
                #print(j)



def printPairs(lim):
    primes = prime_eratosthenes(lim)
    for i in range(0, len(primes)):
        for j in range(i+1, len(primes)):
            if (i*j)<=n:
                print(primes[i], primes[j])

printPairs(lim)
With 10 it should generate output of (2,5)

Thank you


RE: Pairs of multiplied prime number--->N - Yoriz - Jan-14-2022

you have a call to prime_eratosthenes(lim) which has a reference to the return value in primes
because the function prime_eratosthenes has no return it defaults to returning None
Try adding return prime_list to the end of the function.


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

I am not sure i am following you but i did some updates:

lim = int(input("Generate prime numbers up to what number? : "))
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)
                #print(j)



def printPairs(lim):
    primes = prime_eratosthenes(lim)
    list = []
    for i in range(0, lim):
        if (primes[i]):
           for j in range(i+1, n/i):
              if (primes[j]):
                 list.append(i)
                 list.append(j)
                 
    return list

printPairs(lim)
Now i get there errors:

Error:
Traceback (most recent call last): File "<string>", line 27, in <module> File "<string>", line 19, in printPairs TypeError: 'NoneType' object is not subscriptable



RE: Pairs of multiplied prime number--->N - Yoriz - Jan-14-2022

You put the return in the wrong function


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

(Jan-14-2022, 07:47 PM)Yoriz Wrote: You put the return in the wrong function

What am i doing wrong. Angry

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)
                #print(j)
    return prime_list

def printPairs(lim):
    primes = prime_eratosthenes(lim)
    list = []
    for i in range(2, lim):
        if (primes[i]):
           for j in range(2, lim/i):
              if (primes[j]):
                 list.append(i)
                 list.append(j)
    return list  
    

printPairs(lim)
Error:
Traceback (most recent call last): File "<string>", line 26, in <module> File "<string>", line 19, in printPairs TypeError: 'float' object cannot be interpreted as an integer >



RE: Pairs of multiplied prime number--->N - Yoriz - Jan-14-2022

On the original code in your first post for the particular error
Error:
TypeError: 'NoneType' object cannot be interpreted as an integer
was because your function prime_eratosthenes had no return
to fix that error you would add return prime_list to the end of it
def prime_eratosthenes(lim):
    ...
    return prime_list
(Note I replace the rest of the code with ... just to show the important part not for you to do this to your code)


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

You are right about this part. But it wont return the right answer.

lim = int(input("Generate prime numbers up to what number? : "))
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)
                #print(j)
 
    return prime_list
 
def printPairs(lim):
    primes = prime_eratosthenes(lim)
    for i in range(0, len(primes)):
        for j in range(i+1, len(primes)):
            if (i*j)<=lim:
                print(primes[i], primes[j])
 
printPairs(lim)
Output:
Generate prime numbers up to what number? : 10 2 3 5 7 4 6 4 8 4 10 4 9 6 8 6 10 6 9 8 10 8 9 >
4 by 8 dont return 10


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

(Jan-14-2022, 09:06 PM)Frankduc Wrote: 4 by 8 dont return 10
You should test your program bit by bit. The first function should return a list of primes. Does it do so?
def prime_eratosthenes(lim):
    prime_list = []
    for i in range(2, lim + 1):
        if i not in prime_list:
            print(i)
            for j in range(i * i, lim + 1, i):
                prime_list.append(j)
                # print(j)
    return prime_list

lim = int(input("Generate prime numbers up to what number? : "))
print(prime_eratosthenes(lim))
Output:
Generate prime numbers up to what number? : 10 2 3 5 7 [4, 6, 8, 10, 9]
You can see the print() statement on line 5 does print the primes. But the returned list are not primes. Correct this first.


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

This part is faulty:

def printPairs(lim):
    primes = prime_eratosthenes(lim)
    for i in range(0, len(primes)):
        print("pris",primes[i])
        for j in range(i+1, len(primes)):
            if (i*j)<=lim:
                print('')
                print(primes[i], primes[j])
  
printPairs(lim)
As you noticed it return: number that are not primes. But i dont understand why primes = prime_eratosthenes(lim) return those who are not primes when actually prime_eratosthenes(lim) return primes.

Output:
Generate prime numbers up to what number? : 10 2 3 5 7 pris 4 4 6 4 8 4 10 4 9 pris 6 6 8 6 10 6 9 pris 8 8 10 8 9 pris 10 pris 9
The way to solve it would be to insert an if after the first loop :

def printPairs(lim):
    primes = prime_eratosthenes(lim)
    for i in range(0, len(primes)):
        if (primes[i] != primes):
            for j in range(i+1, len(primes)):
                if (i*j)<=lim:
                    print('')
                    print(primes[i], primes[j])
  
printPairs(lim)
but i get :

Error:
File "<string>", line 18 if (!primes): ^ SyntaxError: invalid syntax >
Can you use ! in Python or it is (is not)? How to use it?


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

(Jan-15-2022, 01:10 PM)Frankduc Wrote: Can you use ! in Python or it is (is not)? How to use it?
Look at Python Comparison Operators. The exclamation mark is used in combination with the equals sign: " != " meaning: not equal. But the operands at both sides of the operator must be comparable. You cannot compare an integer with a string. So this is nonsense:
if (primes[i] != primes):
... you cannot compare an integer with a list. But you can check that an integer is not in a list:
if i not in primes:
(Jan-15-2022, 01:10 PM)Frankduc Wrote: But i dont understand why primes = prime_eratosthenes(lim) return those who are not primes when actually prime_eratosthenes(lim) return primes.
It seems you do not know the difference between the returned value of a function and the data printed in a function. The rest of your program knows nothing about what is printed in the function. The rest of your program can only use what is returned by the function.
My advice is again: first make your prime_eratosthenes() function correct.