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
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
You put the return in the wrong function
Reply
#5
(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 >
Reply
#6
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)
Reply
#7
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
Reply
#8
(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.
Reply
#9
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?
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pairs Trading Simulation Kiitoos 0 322 Feb-19-2024, 08:27 PM
Last Post: Kiitoos
  Prime number detector Mark17 5 969 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Sample random, unique string pairs from a list without repetitions walterwhite 1 577 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Extracting unique pairs from a data set based on another value rybina 2 2,419 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 3,038 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  check if the number is a prime integer atlass218 5 3,072 Sep-26-2019, 07:58 AM
Last Post: atlass218
  Key value pairs assistance UtiliseIT 2 2,745 May-09-2019, 09:26 AM
Last Post: UtiliseIT
  Creating a program to look for the largest prime number of a number Wikki14 4 4,031 Sep-08-2018, 12:30 AM
Last Post: Skaperen
  Create random pairs Dennisp44 3 8,216 Jun-02-2018, 05:51 AM
Last Post: buran
  Unexpected result in simple prime number example jackhj 2 3,102 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