Core Functions in Python SymPy

Python Methods and Functions

Library functions always make our code easier, so here we are going to discuss some library functions in python for working with prime numbers.  SymPy — it is a python module that contains some really interesting library functions related to simple numbers . Below is a list of these functions:

  1. isprime (n):
  2. It checks if it is whether n is prime (True) or not (False).

  3. primerange (a, b): generates a list of all primes in the range [a , b).
  4. randprime (a, b): Returns a random prime in the range [a, b).
  5. primepi (n): Returns the number of primes less than or equal to n.
  6. prime (nth): returns the nth prime, with primes indexed as prime (1) = 2.nth prime is approximately n * log (n) and can never be greater than 2 ** n.
  7. prevprime (n): returns the previous prime less than n.
  8. nextprime (n): returns the next higher prime than n.
  9. sieve.primerange (a, b): generates all primes in the range [a, b), implemented as a dynamically growing sieve of eratosthenes.
  10. # Library functions for plain

    import sympy

      
    # Output: True

    print sympy.isprime ( 5

     
    # Output: [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]

    print list (sympy.primerange ( 0 , 100 )) 

      

    print sympy.randprime ( 0 , 100 # Output: 83

    print sympy.randprime ( 0 , 100 # Output: 41

    print sympy. prime ( 3 # Output: 5

    print sympy.prevprime ( 50 # Output: 47

    print sympy.nextprime ( 50 # Output: 53

      
    # Output: [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 ]

    print list (sympy.sieve .primerange ( 0 , 100 )) 

    Link:
    https://stackoverflow.com/questions/13326673 / is-there-a-python-library-to-list-primes

    This article courtesy of Shashank Mishra (Gullu) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.