This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
/** Tests for a prime by checking every single candidate. */ | |
public class ExhaustivePrimeSearch extends PrimalityTest { | |
@Override | |
public boolean isPrime(BigInteger n) { | |
// Only need to check numbers from 2 up to the square root. | |
BigInteger candidate = BigInteger.valueOf(2); | |
while ( (candidate.pow(2)).compareTo(n) == -1 ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
/** Tests for a prime by using the Fermat test. */ | |
public class FermatTest extends PrimalityTest { | |
public java.util.Random rand = new java.util.Random(); | |
public int numberOfTests; | |
/** Sets up the Fermat test procedure to run the given number of tests. */ | |
public FermatTest(int tests) { | |
numberOfTests = tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
/** Tests for a prime by using the Miller-Rabin test. */ | |
public class MillerRabinTest extends PrimalityTest { | |
public java.util.Random rand = new java.util.Random(); | |
public int numberOfTests; | |
// The algorithm works to find a representation (n-1) = 2^s * d. | |
public int s; | |
public BigInteger d; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns the k-term continued fraction approximation. */ | |
def continuedFraction(n: Int => Double, d: Int => Double, k: Int): Double = { | |
var result = 0.0 | |
for (i <- (1 to k).reverse) { | |
result = n(i) / (d(i) + result) | |
} | |
result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns the k-term continued fraction approximation. */ | |
def continuedFraction(n: Int => Double, d: Int => Double, k: Int): Double = | |
(1 to k).reverse.foldLeft(0.0) { | |
(result, i) => n(i) / (d(i) + result) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** We bring in our continued fraction function... */ | |
def continuedFraction(n: Int => Double, d: Int => Double, k: Int): Double = | |
(1 to k).reverse.foldLeft(0.0) { | |
(result, i) => n(i) / (d(i) + result) | |
} | |
/** ...And define our tangent function as follows. */ | |
def tan(x: Double): Double = { | |
def n(i: Int): Double = if (i == 1) x else -(x * x) | |
def d(i: Int): Double = (2 * i) - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns the sine of the given angle (in radians.) */ | |
def sin(x: Double): Double = { | |
if (Math.abs(x) < 0.01) | |
x | |
else | |
((y: Double) => 3*y - 4*Math.pow(y,3))( sin(x/3) ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns an approximation to the fixed-point of the given function f | |
* (using the given firstGuess to get started.) | |
*/ | |
def fixedPoint(f: Double => Double, firstGuess: Double): Double = { | |
val tolerance = 0.0001 | |
val maxIterations = 30 | |
var guess = firstGuess | |
for (i <- 1 to maxIterations) { | |
if (Math.abs(guess - f(guess)) < tolerance) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* We bring in the fixed-point code... */ | |
def fixedPoint(f: Double => Double, firstGuess: Double): Double = { | |
val tolerance = 0.0001 | |
val maxIterations = 30 | |
var guess = firstGuess | |
for (i <- 1 to maxIterations) { | |
if (Math.abs(guess - f(guess)) < tolerance) { | |
return guess | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returns an approximation to the fixed-point of the given function f | |
# (using the given first_guess to get us started.) | |
def fixed_point(f, first_guess) | |
tolerance = 0.0001 | |
max_iterations = 30 | |
guess = first_guess | |
max_iterations.times do | |
return guess if (guess - f.call(guess)).abs < tolerance |