Skip to content

Instantly share code, notes, and snippets.

@pH200
Last active December 20, 2015 05:39
Show Gist options
  • Save pH200/6080552 to your computer and use it in GitHub Desktop.
Save pH200/6080552 to your computer and use it in GitHub Desktop.
factorial generators
let fTable =
let rec factorial prev pos = seq {
let current = prev * pos
yield current
yield! factorial current (pos + 1)
}
factorial 1 1
let fTable = Seq.initInfinite(fun index -> Seq.reduce (*) [1..(index+1)])
function fTable() {
var prev = 1;
var pos = 1;
for (;;) {
var current = prev * pos;
prev = current;
pos++;
if (yield current) {
prev = 1;
pos = 1;
}
}
}
def factorial(prev: BigInt, pos: Int): Stream[BigInt] = {
val current = prev * pos
current #:: factorial(current, pos + 1)
}
val fTable = factorial(1, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment