-1

I'm trying to test a function in Jupyter Notebooks that uses gmpy2 to produce prime numbers but I'm getting the following output.

import gmpy2
def solution(i):
    n = 2
    while True:
        yield n
        n = str(gmpy2.next_prime(n))

for i in solution(1):
    print(i)
    input()
2

3

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-e0db74781d33> in <module>
      6         n = str(gmpy2.next_prime(n))
      7 
----> 8 for i in solution(1):
      9     print(i)
     10     input()

<ipython-input-14-e0db74781d33> in solution(i)
      4     while True:
      5         yield n
----> 6         n = str(gmpy2.next_prime(n))
      7 
      8 for i in solution(1):

TypeError: next_prime() requires 'mpz' argument

Why does the function successfully output the first two numbers in the sequence (2 and 3) but then produce an error? What does mpz mean?

Nasrat Takoor
  • 344
  • 3
  • 8
  • There was an question as same before, I think it will help you - https://stackoverflow.com/questions/13326673/is-there-a-python-library-to-list-primes – Lakshan Costa Nov 19 '20 at 04:04

2 Answers2

1

In n = str(gmpy2.next_prime(n)) you are changing n to string. That's why it yields 2 and 3, where 2 is an integer, 3 is a string, but gmpy2.next_prime(n) requires an integer.

cigien
  • 57,834
  • 11
  • 73
  • 112
heressy
  • 11
  • 1
-2
import gmpy2
def solution():
    n = 2
    while True:
        yield n
        n = str(gmpy2.next_prime(n))

while True:
    print(solution())

try this way