MillerRabin

com.phasmidsoftware.number.core.numerical.MillerRabin
object MillerRabin

This code is attributed to: https://www.literateprograms.org/miller-rabin_primality_test__scala_.html

It's not idiomatic Scala but I will clean it up as we go forward.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def isProbablePrime(n: BigInt): Boolean

Method (originally called miller_rabin) from "literateprograms" with slight improvements for elegance.

Method (originally called miller_rabin) from "literateprograms" with slight improvements for elegance.

NOTE: this is equivalent (I believe) to n.isProbablePrime(40), i.e. there's a one-in-a-trillion chance that we get false positive.

Value parameters

n

a BigInt.

Attributes

Returns

true if n is a probable prime with certainty approximately 2.pow(-40)

def millerRabinTester(action: String, number: String): String

Executes a Miller-Rabin primality test or generates a probable prime number based on the specified action. If the action is "test", it determines whether the provided number is a prime. If the action is "genprime", it generates a probable prime number with the specified bit length.

Executes a Miller-Rabin primality test or generates a probable prime number based on the specified action. If the action is "test", it determines whether the provided number is a prime. If the action is "genprime", it generates a probable prime number with the specified bit length.

Value parameters

action

A string specifying the operation to perform. Valid values are "test" for performing a primality test or "genprime" for prime number generation.

number

A string representation of the number to test or the bit length for prime generation. For the "test" action, this should be the number to be tested for primality. For the "genprime" action, this should specify the number of bits for the generated prime.

Attributes

Returns

A string result. If the action is "test", returns "PRIME" if the number is a probable prime or "COMPOSITE" otherwise. If the action is "genprime", returns the generated probable prime as a string. If the action is invalid, returns a message indicating the invalid action.