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
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; |
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
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) { |
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
(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))))) |
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
(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))))) |
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
# 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 |
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
(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))))) |
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
(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)) |
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
# 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) |
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
(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))))) |
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; | |
/** 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(); |
OlderNewer