Prime numbers python library

Is there a Python library to list primes?

Is there a library function that can enumerate the prime numbers (in sequence) in Python? I found this question Fastest way to list all primes below N but I’d rather use someone else’s reliable library than roll my own. I’d be happy to do import math; for n in math.primes:

you are always going to have to put some upper limit N . and for big N value it may take a long time .

this may be what your are looking for stackoverflow.com/questions/567222/… . see first answer (which actually links to code.activestate.com/recipes/117119 )

6 Answers 6

SymPy is another choice. It is a Python library for symbolic mathematics. It provides several functions for prime.

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. 
>>> 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] 

Источник

Читайте также:  Java вывести время выполнения

primelibpy 2.2

This library can be used in cryptoanalysis and some compatitive exams. It cover many different types of prime number and three factorization algorithems. This library also helps to generate random spacific type of prime number with desire digits. To use this library you mast have python 3.x

Ссылки проекта

Статистика

Метаданные

Лицензия: MIT License

Требует: Python >=3.0

Сопровождающие

Классификаторы

Описание проекта

Python Prime Library

This is the official documentation of the python prime library.

  • Generate Specific types of Prime numbers between the given range
  • Generate Random Prime number
  • Factorization of the given number

Installation!

Use pip3 if you are using python version 3+ else follow the same steps as mentioned below:

  1. If you don’t have pip then follow the below procedure else go to step 2.
  • For window Users
    • Download get-pip to a folder on your computer.
    • Put that file on Desktop
    • Open cmd and run the following commands:
    • Run the following commands for python(version > 2.0): $ sudo apt-get install python-pip $ sudo pacman -S python2-pip $ sudo yum upgrade python-setuptools $ sudo yum install python-pip python-wheel $ sudo dnf upgrade python-setuptools $ sudo dnf install python-pip python-wheel $ sudo zypper install python-pip python-setuptools python-wheel
    • Run the following commands for python(version > 3.0): $ sudo apt-get install python3-pip $ sudo pacman -S python-pip $ sudo yum install python3 python3-wheel $ sudo dnf install python3 python3-wheel $ sudo zypper install python3-pip python3-setuptools python3-wheel
    • Run the following commands for python(version > 2.0): $ sudo apt-get install python-pip
    • Run the following commands for python(version > 2.0): $ sudo apt-get install python3-pip
    1. Import gmpy2 file
      • This package is required to install primelibpy library
      • Run the following command $ pip install gmpy2==2.1.0a2
    2. Now, install the prime python library using the below command.
      • Run the following command $ pip install primelibpy
    3. How to use Library
      • Inside of your python IDE(Python file) from primelibpy import Prime as p
      • Now, using p all functions can be used in code e.g. balancedNumberList = p.getBalancedPrime(2,100,2)

    Functions Description

    • Prime Functions In all the prime numbers Start_Limit and End_Limit are the range of prime numbers the user wants to print inclusively.

    Balanced Prime

    Syntex: getBalancedPrime(startLimit,endLimit,balancedMode)

    Return Type: list

    Description: Balanced_Mode is how number which decide a balanced limit for prime.

    Circular Prime

    Syntex: getCircularPrime(startLimit,endLimit)

    Return Type: list

    Cousin Prime

    Syntex: getCousinPrime(startLimit,endLimit)

    Return Type: list

    Description: Cousin prime are in pair so return list is have list inside it e.g.[ [1,2], [2,3] ]

    Double Mersenne Prime

    Syntex: getDoubleMersennePrime(startLimit,endLimit)

    Return Type: list

    Factorial Prime

    Syntex: getFactorialPrime(startLimit,endLimit)

    Return Type: list

    Good Prime

    Syntex: getGoodPrime(startLimit,endLimit)

    Return Type: list

    Mersenne Prime

    Syntex: getMersennePrime(startLimit,endLimit)

    Return Type: list

    Palindromic Prime

    Syntex: getPalindromicPrime(startLimit,endLimit)

    Return Type: list

    Permutable Prime

    Syntex: getPermutablePrime(startLimit,endLimit)

    Return Type: list

    Primorial Prime

    Syntex: getPrimorialPrime(startLimit,endLimit)

    Return Type: list

    Fermat Pseudo Prime

    Syntex: getFermatPseudoPrime(startLimit,endLimit,baseNumber)

    Return Type: list

    Description: Base_number helps to generate a composite the number and the second argument is the Total number of Pseudo primes

    Pythagorean Prime

    Syntex: getPythagoreanPrime(startLimit,endLimit)

    Return Type: list

    Reversible Prime

    Syntex: getReversiblePrime(startLimit,endLimit)

    Return Type: list

    Semi Prime

    Syntex: getSemiPrime(startLimit,endLimit)

    Return Type: list

    Sophie Germain Prime

    Syntex: getSophieGermainPrime(startLimit,endLimit)

    Return Type: list

    Twin Prime

    Syntex: getTwinPrime(startLimit,endLimit)

    Return Type: list

    Description: Twin prime are in pair so return list is have list inside it e.g.[ [1,2], [2,3] ]

    Wagstaff Prime

    Syntex: getWagstaffPrime(startLimit,endLimit)

    Return Type: list

    Wieferich Prime

    Syntex: getWieferichPrime(startLimit,endLimit)

    Return Type: list

    Wilson Prime

    Syntex: getWilsonPrime(startLimit,endLimit)

    Return Type: list

    Left Truncatable Prime

    Syntex: getLeftTruncatablePrime(startLimit,endLimit)

    Return Type: list

    Right Truncatable Prime

    Syntex: getRightTruncatablePrime(startLimit,endLimit)

    Return Type: list

    Truncatable Prime

    Syntex: getTruncatablePrime(startLimit,endLimit)

    Return Type: list

    Gaussian Prime

    Syntex: checkGaussianPrime(realPart,imaginaryPart)

    Return Type: None

    Note: This will print whether the number is gaussian or not

    Random Prime Generation

    Random Prime generation function has three arguments and it requires little knowledge of above all functions.

    The first argument is the type of prime number. Note that name of prime followed by Prime. The name of the prime type has to be in the capital.

    The second argument is the number of digits of random numbers.

    The third argument is only mandatory for balanced prime numbers.

    Syntex: getRandomPrime(primeType,totalDigits,mode=0)

    Return Type: integer

    import Prime as p

    randomPrimorial = p.getRandomPrime(«PrimorialPrime»,3)

    Factorization

    This Section is related to Number factorization. Use Semi-Prime where it is indicated.

    Traditional Way for Factorization

    Syntex: getFactorTraditional(semiPrimeNumber)

    Return Type: list

    Note: This function will work all kinds of numbers but I’ll suggest using Semi prime number because This function returns the Factor of input number except 1 and the number itself.

    Fermat Theorem for Factorization

    Syntex: getFactorFermatTheorem(semiPrimeNumber)

    Return Type: tuple

    Note: This is only for composite numbers that have only two prime factors except for the number itself e.g. 33 have two prime factors 3 and 11.

    Pollard Rho Factorization

    Syntex: getFactorPollardRho(semiPrimeNumber)

    Return Type: integer

    Note: This will return any one factor of the given number because this algorithm works on random numbers.

    General Factorization

    Syntex: getAllFactors(compositeNumber)

    Return Type: list

    Note: This will return a list of all prime factors.

    License

    Free Software, Hell Yeah!

    Источник

    Saved searches

    Use saved searches to filter your results more quickly

    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

    Prime number library for Python 3

    License

    liam-m/primes.py

    This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

    Name already in use

    A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

    Sign In Required

    Please sign in to use Codespaces.

    Launching GitHub Desktop

    If nothing happens, download GitHub Desktop and try again.

    Launching GitHub Desktop

    If nothing happens, download GitHub Desktop and try again.

    Launching Xcode

    If nothing happens, download Xcode and try again.

    Launching Visual Studio Code

    Your codespace will open once ready.

    There was a problem preparing your codespace, please try again.

    Latest commit

    Git stats

    Files

    Failed to load latest commit information.

    README.md

    Prime number library for Python 3

    If you want to help develop, open an issue or fork the repo, make your changes and submit a pull request.

    A list-like object that automatically generates additional prime numbers as required. Supports membership testing and slicing for sequences of prime numbers

    >>> primes = Primes() >>> 10 in primes False >>> 11 in primes True >>> primes[10:20] [31, 37, 41, 43, 47, 53, 59, 61, 67, 71] >>> primes[15:10:-2] [53, 43, 37] >>> primes[100] 547 >>> primes.index(23) 8

    There are also a number of functions for prime generation and primality testing:

    Implementation of Sieve of Eratosthenes

    Returns all primes up to (and including) x

    Can pass in primes to decrease execution time

    >>> primes_up_to(10) [2, 3, 5, 7] >>> primes_up_to(20, [2, 3, 5, 7]) [2, 3, 5, 7, 11, 13, 17, 19]

    Returns True if x is a prime number, False if it is not

    Can pass in known primes to decrease execution time

    >>> is_prime(191) True >>> is_prime(192) False

    Returns the first n primes

    Can pass in known primes to decrease execution time

    >>> n_primes(5) [2, 3, 5, 7, 11] >>> n_primes(10, [2, 3, 5, 7, 11]) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    Returns the n th prime (i.e. the 3 rd prime, the 6 th prime)

    Can pass in known primes to decrease execution time

    >>> nth_prime(1000) 7919 >>> nth_prime(4, [2, 3, 5, 7, 11]) 7

    Returns all composite (non-prime greater than 1) numbers up to (and including) x

    Can pass in known primes to decrease execution time

    >>> composites_up_to(10) [4, 6, 8, 9, 10] >>> composites_up_to(20, [2, 3, 5, 7, 11, 13, 17, 19, 23]) [4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20]

    Given primes, returns the next prime

    Uses method of trial division

    This is much less efficient than primes_up_to for generating ranges of primes

    >>> next_prime([2, 3, 5, 7, 11]) 13 >>> next_prime(primesUpTo(1000000)) 1000003

    About

    Prime number library for Python 3

    Источник

Оцените статью