March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -5,5 +5,5 @@ long long int fibb(int n) {
|
|||
fnow = fnext;
|
||||
fnext = tempf;
|
||||
}
|
||||
return fnow;
|
||||
return fnext;
|
||||
}
|
||||
|
|
|
|||
74
Task/Fibonacci-sequence/C/fibonacci-sequence-5.c
Normal file
74
Task/Fibonacci-sequence/C/fibonacci-sequence-5.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <gmp.h>
|
||||
|
||||
typedef struct node node;
|
||||
struct node {
|
||||
int n;
|
||||
mpz_t v;
|
||||
node *next;
|
||||
};
|
||||
|
||||
#define CSIZE 37
|
||||
node *cache[CSIZE];
|
||||
|
||||
// very primitive linked hash table
|
||||
node * find_cache(int n)
|
||||
{
|
||||
int idx = n % CSIZE;
|
||||
node *p;
|
||||
|
||||
for (p = cache[idx]; p && p->n != n; p = p->next);
|
||||
if (p) return p;
|
||||
|
||||
p = malloc(sizeof(node));
|
||||
p->next = cache[idx];
|
||||
cache[idx] = p;
|
||||
|
||||
if (n < 2) {
|
||||
p->n = n;
|
||||
mpz_init_set_ui(p->v, 1);
|
||||
} else {
|
||||
p->n = -1; // -1: value not computed yet
|
||||
mpz_init(p->v);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
mpz_t tmp1, tmp2;
|
||||
mpz_t *fib(int n)
|
||||
{
|
||||
int x;
|
||||
node *p = find_cache(n);
|
||||
|
||||
if (p->n < 0) {
|
||||
p->n = n;
|
||||
x = n / 2;
|
||||
|
||||
mpz_mul(tmp1, *fib(x-1), *fib(n - x - 1));
|
||||
mpz_mul(tmp2, *fib(x), *fib(n - x));
|
||||
mpz_add(p->v, tmp1, tmp2);
|
||||
}
|
||||
return &p->v;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, n;
|
||||
if (argc < 2) return 1;
|
||||
|
||||
mpz_init(tmp1);
|
||||
mpz_init(tmp2);
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
n = atoi(argv[i]);
|
||||
if (n < 0) {
|
||||
printf("bad input: %s\n", argv[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// about 75% of time is spent in printing
|
||||
gmp_printf("%Zd\n", *fib(n));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import std.stdio, std.bigint;
|
||||
import std.bigint;
|
||||
|
||||
T fibonacciMatrix(T=BigInt)(size_t n) {
|
||||
int[size_t.sizeof * 8] binDigits;
|
||||
|
|
@ -26,5 +26,5 @@ T fibonacciMatrix(T=BigInt)(size_t n) {
|
|||
}
|
||||
|
||||
void main() {
|
||||
writeln(fibonacciMatrix(1_000_000));
|
||||
10_000_000.fibonacciMatrix;
|
||||
}
|
||||
|
|
|
|||
44
Task/Fibonacci-sequence/D/fibonacci-sequence-3.d
Normal file
44
Task/Fibonacci-sequence/D/fibonacci-sequence-3.d
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import std.bigint, std.math;
|
||||
|
||||
// Algorithm from: Takahashi, Daisuke,
|
||||
// "A fast algorithm for computing large Fibonacci numbers".
|
||||
// Information Processing Letters 75.6 (30 November 2000): 243-246.
|
||||
// Implementation from:
|
||||
// pythonista.wordpress.com/2008/07/03/pure-python-fibonacci-numbers
|
||||
BigInt fibonacci(in ulong n)
|
||||
in {
|
||||
assert(n > 0, "fibonacci(n): n must be > 0.");
|
||||
} body {
|
||||
if (n <= 2)
|
||||
return 1.BigInt;
|
||||
BigInt F = 1;
|
||||
BigInt L = 1;
|
||||
int sign = -1;
|
||||
immutable uint n2 = cast(uint)n.log2.floor;
|
||||
auto mask = 2.BigInt ^^ (n2 - 1);
|
||||
foreach (immutable i; 1 .. n2) {
|
||||
auto temp = F ^^ 2;
|
||||
F = (F + L) / 2;
|
||||
F = 2 * F ^^ 2 - 3 * temp - 2 * sign;
|
||||
L = 5 * temp + 2 * sign;
|
||||
sign = 1;
|
||||
if (n & mask) {
|
||||
temp = F;
|
||||
F = (F + L) / 2;
|
||||
L = F + 2 * temp;
|
||||
sign = -1;
|
||||
}
|
||||
mask /= 2;
|
||||
}
|
||||
if ((n & mask) == 0) {
|
||||
F *= L;
|
||||
} else {
|
||||
F = (F + L) / 2;
|
||||
F = F * L - sign;
|
||||
}
|
||||
return F;
|
||||
}
|
||||
|
||||
void main() {
|
||||
10_000_000.fibonacci;
|
||||
}
|
||||
12
Task/Fibonacci-sequence/Frink/fibonacci-sequence.frink
Normal file
12
Task/Fibonacci-sequence/Frink/fibonacci-sequence.frink
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fibonacciN[n] :=
|
||||
{
|
||||
a = 0
|
||||
b = 1
|
||||
count = 0
|
||||
while count < n
|
||||
{
|
||||
[a,b] = [b, a + b]
|
||||
count = count + 1
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
(de fibo (N)
|
||||
(cache '(NIL) (pack (char (hash N)) N) # Use a cache to accelerate
|
||||
(cache '(NIL) N # Use a cache to accelerate
|
||||
(if (>= 2 N)
|
||||
N
|
||||
(+ (fibo (dec N)) (fibo (- N 2))) ) ) )
|
||||
|
|
|
|||
7
Task/Fibonacci-sequence/Python/fibonacci-sequence-8.py
Normal file
7
Task/Fibonacci-sequence/Python/fibonacci-sequence-8.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def fib(n, c={0:1, 1:1}):
|
||||
if n not in c:
|
||||
x = n // 2
|
||||
c[n] = fib(x-1) * fib(n-x-1) + fib(x) * fib(n - x)
|
||||
return c[n]
|
||||
|
||||
fib(10000000) # calculating it takes a few seconds, printing it takes eons
|
||||
|
|
@ -1,9 +1 @@
|
|||
//syntactic sugar for Stream.cons, this is unnecessary but makes the definition prettier
|
||||
//Stream.cons(head,stream) becomes head::stream
|
||||
//I think 2.8 will have #::
|
||||
class PrettyStream[A](str: =>Stream[A]) {
|
||||
def ::(hd: A) = Stream.cons(hd, str)
|
||||
}
|
||||
implicit def streamToPrettyStream[A](str: =>Stream[A]) = new PrettyStream(str)
|
||||
|
||||
def fib: Stream[Int] = 0 :: 1 :: fib.zip(fib.tail).map{case (a,b) => a + b}
|
||||
lazy val fib: Stream[Int] = 0 #:: 1 #:: fib.zip(fib.tail).map{case (a,b) => a + b}
|
||||
|
|
|
|||
|
|
@ -1 +1,4 @@
|
|||
def fib: Stream[Int] = 0 #:: 1 #:: fib.zip(fib.tail).map{case (a,b) => a + b}
|
||||
def fib(i:Int, a:Int=1, b:Int=0):Int = i match{
|
||||
case 1 => b
|
||||
case _ => fib(i-1, b, a+b)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
def fib(i:Int):Int = {
|
||||
def fib2(i:Int, a:Int, b:Int):Int = i match{
|
||||
case 1 => b
|
||||
case _ => fib2(i-1, b, a+b)
|
||||
}
|
||||
fib2(i,1,0)
|
||||
// Fibonacci using BigInt with Stream.foldLeft optimized for GC (Scala v2.9 and above)
|
||||
// Does not run out of memory for very large Fibonacci numbers
|
||||
def fib(n:Int) = {
|
||||
|
||||
def series(i:BigInt,j:BigInt):Stream[BigInt] = i #:: series(j, i+j)
|
||||
|
||||
series(1,0).take(n).foldLeft(BigInt("0"))(_+_)
|
||||
}
|
||||
|
||||
// Small test
|
||||
(0 to 13) foreach {n => print(fib(n).toString + " ")}
|
||||
|
||||
// result: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue