Skip to content

Instantly share code, notes, and snippets.

View RussellAndrewEdson's full-sized avatar

Russell Andrew Edson RussellAndrewEdson

  • Adelaide, Australia
View GitHub Profile
@RussellAndrewEdson
RussellAndrewEdson / ExhaustivePrimeSearch.java
Last active August 29, 2015 14:13
Exhaustive prime search: we check every single candidate.
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 ) {
@RussellAndrewEdson
RussellAndrewEdson / FermatTest.java
Created January 7, 2015 07:27
The Fermat test for probabilistic primality checking.
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;
@RussellAndrewEdson
RussellAndrewEdson / MillerRabinTest.java
Last active August 29, 2015 14:13
The Miller-Rabin test for probabilistic primality checking.
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;
@RussellAndrewEdson
RussellAndrewEdson / ContinuedFraction1.scala
Created January 14, 2015 07:26
A function to represent a k-term continued fraction.
/** 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
}
@RussellAndrewEdson
RussellAndrewEdson / ContinuedFraction2.scala
Created January 14, 2015 07:28
A function to represent a k-term continued fraction. (Scala idiomatic)
/** 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)
}
@RussellAndrewEdson
RussellAndrewEdson / Tangent.scala
Created January 14, 2015 07:33
The tangent function, approximated using a k-term continued fraction.
/** 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
@RussellAndrewEdson
RussellAndrewEdson / Sine.scala
Last active August 29, 2015 14:13
The sine function, approximated using a recursive trigonometric identity.
/** 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) )
}
@RussellAndrewEdson
RussellAndrewEdson / FixedPoint.scala
Last active August 29, 2015 14:13
A function to approximate the fixed-point of a given transformation.
/** 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) {
@RussellAndrewEdson
RussellAndrewEdson / SqrtFixedPoint.scala
Last active August 29, 2015 14:13
An implementation of a square-root function using fixed-point iteration.
/* 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
}
@RussellAndrewEdson
RussellAndrewEdson / newtons_method.rb
Last active August 29, 2015 14:14
Newton's Method for approximating the zeros of functions.
# 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