chickadee » chibi-math

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
modular-inverse a bprocedure

Returns the multiplicative inverse of a modulo b.

modular-expt
modular-expt a e mprocedure

Returns (remainder (expt a e) m).

coprime?
coprime? n mprocedure

Returns true iff n and m are coprime.

Exact prime testing

provable-prime?
provable-prime? nprocedure

Returns true iff n is definitely prime. May take an impossibly long time for large values.

Probable primes.

miller-rabin-composite?
miller-rabin-composite? nprocedure

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?
probable-prime? nprocedure

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?
prime? nprocedure

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
nth-prime iprocedure

Returns the nth prime, with 2 being the 0th prime.

prime-below
prime-below nprocedure

Returns the first prime less than or equal to n, or #f if there are no such primes.

prime-above
prime-above n #!optional (limit #f)procedure

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
factor-alistconstant

Returns 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
factorconstant

Returns the factorization of n as a monotonically increasing list of primes.

totient
totientconstant

The Euler totient φ(n) is the number of positive integers less than or equal to n that are relatively prime to n.

aliquot
aliquotconstant

The aliquot sum s(n) is the sum of proper divisors of a positive integer n.

perfect?
perfect? nprocedure

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
random-prime lo hiprocedure

Returns a random prime between lo, inclusive, and hi, exclusive.

random-prime-distinct-from
random-prime-distinct-from lo hi pprocedure

Variant of random-prime which ensures the result is distinct from p.

random-coprime
random-coprime nprocedure

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. 

Contents »