Simple prime number generator in Python

前端 未结 26 2198
感情败类
感情败类 2020-11-22 07:16

Could someone please tell me what I\'m doing wrong with this code? It is just printing \'count\' anyway. I just want a very simple prime generator (nothing fancy).

26条回答
  •  眼角桃花
    2020-11-22 08:13

    SymPy is a Python library for symbolic mathematics. It provides several functions to generate prime numbers.

    isprime(n)              # Test if n is a prime number (True) or not (False).
    
    primerange(a, b)        # Generate a list of all prime numbers in the range [a, b).
    randprime(a, b)         # Return a random prime number in the range [a, b).
    primepi(n)              # Return the number of prime numbers less than or equal to n.
    
    prime(nth)              # Return the nth prime, with the primes indexed as prime(1) = 2. The nth prime is approximately n*log(n) and can never be larger than 2**n.
    prevprime(n, ith=1)     # Return the largest prime smaller than n
    nextprime(n)            # Return the ith prime greater than n
    
    sieve.primerange(a, b)  # Generate all prime numbers in the range [a, b), implemented as a dynamically growing sieve of Eratosthenes. 
    

    Here are some examples.

    >>> import sympy
    >>> 
    >>> sympy.isprime(5)
    True
    >>> list(sympy.primerange(0, 100))
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    >>> sympy.randprime(0, 100)
    83
    >>> sympy.randprime(0, 100)
    41
    >>> sympy.prime(3)
    5
    >>> sympy.prevprime(50)
    47
    >>> sympy.nextprime(50)
    53
    >>> list(sympy.sieve.primerange(0, 100))
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    

提交回复
热议问题