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 / PascalsTriangle.java
Last active August 29, 2015 14:12
Java code for Pascal's Triangle.
public class PascalsTriangle {
private int[][] triangle;
/* Creates a new PascalsTriangle representation with n rows. */
public PascalsTriangle(int n) {
triangle = new int[n][n];
// The first column is all 1's.
for (int i = 0; i < n; i++) {
triangle[i][0] = 1;
@RussellAndrewEdson
RussellAndrewEdson / PascalsTriangle.scala
Last active August 29, 2015 14:12
Scala code for Pascal's Triangle.
def nextRow(row: List[Int]): List[Int] = (row, (0 :: row)).zipped.map(_ + _)
/* Generating 5 rows of Pascal's triangle. */
var pascal = List[List[Int]]( List(1,0,0,0,0) )
for (i <- 1 to 4) {
pascal = nextRow(pascal.head) :: pascal
}
pascal = pascal.reverse
for (row <- pascal) {
@RussellAndrewEdson
RussellAndrewEdson / pascals-triangle.clj
Last active August 29, 2015 14:12
Clojure code for Pascal's Triangle.
(defn pascal
"Returns the number in the given row and column of
Pascal's triangle."
[row column]
(cond (= column 1) 1
(= column row) 1
(> column row) 0
:else (+ (pascal (- row 1) (- column 1))
(pascal (- row 1) column)))))
@RussellAndrewEdson
RussellAndrewEdson / fibonacci_recursive.clj
Last active August 29, 2015 14:12
Clojure code to generate the nth Fibonacci number recursively.
(defn fibonacci
"Returns the nth Fibonacci number using a recursive process."
[n]
(cond (= n 0) 0
(= n 1) 1
:else (+ (fibonacci (- n 1))
(fibonacci (- n 2)))))
@RussellAndrewEdson
RussellAndrewEdson / fibonacci_iterative.py
Created January 4, 2015 03:31
Python code to generate the nth Fibonacci number iteratively.
# Calculates the nth Fibonacci number using an iterative process.
def fibonacci(n):
(current, next) = (0, 1)
index = 0
while (index < n):
(current, next) = (next, current + next)
index += 1
return current
@RussellAndrewEdson
RussellAndrewEdson / fibonacci_iterative.clj
Created January 4, 2015 03:58
Clojure code to generate the nth Fibonacci number iteratively.
(defn fibonacci
"Returns the nth Fibonacci number using an iterative process."
[n]
(loop [current 0N next 1N index 0]
(if (= index n)
current
(recur next (+ current next) (inc index)))))
@RussellAndrewEdson
RussellAndrewEdson / fibonacci_logarithmic.clj
Last active August 29, 2015 14:12
Clojure code to generate the nth Fibonacci number in logarithmic time.
(defn fibonacci
"Returns the nth Fibonacci number using a logarithmic squaring algorithm."
[n]
(let [square (fn [x] (* x x))]
(loop [current 0N next 1N p 0N q 1N count n]
(cond (= count 0) current
(even? count) (recur current
next
(+ (square p) (square q))
(+ (square q) (* 2 p q))
@RussellAndrewEdson
RussellAndrewEdson / fibonacci_logarithmic.py
Created January 4, 2015 06:05
Python code to generate the nth Fibonacci number in logarithmic time.
# Calculates the nth Fibonacci number using a logarithmic algorithm.
def fibonacci(n):
# Helper function: returns True if n is an even number.
even = lambda n: (n % 2 == 0)
(current, next, p, q) = (0, 1, 0, 1)
while (n > 0):
if (even(n)):
(p, q) = (p**2 + q**2, q**2 + 2*p*q)
@RussellAndrewEdson
RussellAndrewEdson / fibonacci_formula.clj
Last active August 29, 2015 14:12
Clojure code to generate the nth Fibonacci number using a formula.
(require '[clojure.math.numeric-tower :as math])
(defn fibonacci
"Returns the nth Fibonacci number by using the Golden Ratio formula."
[n]
(let [root-five (math/sqrt 5M)
golden-ratio (/ (+ 1 root-five) 2)]
(math/round (bigdec (/ (math/expt golden-ratio n)
root-five)))))
@RussellAndrewEdson
RussellAndrewEdson / PrimalityTest.java
Last active August 29, 2015 14:12
Common interface for our primality tests.
import java.math.BigInteger;
/** A common interface for the primality tests we'll look at. */
public abstract class PrimalityTest {
public abstract boolean isPrime(BigInteger n);
public void timedPrimeTest(BigInteger n) {
System.out.println("Running test: " + getClass().getName());
System.out.println("n: " + n);
long startTime = System.currentTimeMillis();