chibi-math
Chibi Scheme's math library
Module: (chibi math prime)
Prime and number theoretic utilities.
Original documentation here: https://synthcode.com/scheme/chibi/lib/chibi/math/prime.html
modular-inverse
[procedure] (modular-inverse a b)Returns the multiplicative inverse of a modulo b.
modular-expt
[procedure] (modular-expt a e m)Returns (remainder (expt a e) m).
coprime?
[procedure] (coprime? n m)Returns true iff n and m are coprime.
Exact prime testing
provable-prime?
[procedure] (provable-prime? n)Returns true iff n is definitely prime. May take an impossibly long time for large values.
Probable primes.
miller-rabin-composite?
[procedure] (miller-rabin-composite? n)Returns true if we can show n to be composite using the Miller-Rabin test (i.e., finding a witness a where a^(n-1)≠1 mod n or a reveals the existence of a 3rd square root of 1 in Z/(n))
probable-prime?
[procedure] (probable-prime? n)Returns true if n has a very high probability (enough that you can assume a false positive will never occur in your lifetime) of being prime.
prime?
[procedure] (prime? n)Returns true iff n is prime. Uses provable-prime? for small n, falling back on probable-prime? for large values.
Prime iteration and factorization
nth-prime
[procedure] (nth-prime i)Returns the nth prime, with 2 being the 0th prime.
prime-below
[procedure] (prime-below n)Returns the first prime less than or equal to n, or #f if there are no such primes.
prime-above
[procedure] (prime-above n #!optional (limit #f))Returns the first prime greater than or equal to n. If the optional limit is given and not false, returns #f if no such primes exist below limit.
factor-alist
[constant] factor-alistReturns the factorization of n as a list of elements of the form (p . k)}, where {{p is a prime factor and k is its multiplicity.
factor
[constant] factorReturns the factorization of n as a monotonically increasing list of primes.
totient
[constant] totientThe Euler totient φ(n) is the number of positive integers less than or equal to n that are relatively prime to n.
aliquot
[constant] aliquotThe aliquot sum s(n) is the sum of proper divisors of a positive integer n.
perfect?
[procedure] (perfect? n)Returns true iff n is a perfect number, i.e. the sum of its divisors other than itself equals itself.
Random prime generation
random-prime
[procedure] (random-prime lo hi)Returns a random prime between lo, inclusive, and hi, exclusive.
random-prime-distinct-from
[procedure] (random-prime-distinct-from lo hi p)Variant of random-prime which ensures the result is distinct from p.
random-coprime
[procedure] (random-coprime n)Returns a random integer less than n relatively prime to n.
Maintainer
Diego A. Mundo
Author
Alex Shinn
Version History
- 0.1.0
License
BSD
Copyright (c) 2009-2021 Alex Shinn All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.