June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,4 +1,4 @@
fib_ana = (n) ->
sqrt = Math.sqrt
phi = ((1 + sqrt(5))/2)
return Math.round((Math.pow(phi, n)/sqrt(5)))
Math.round((Math.pow(phi, n)/sqrt(5)))

View file

@ -0,0 +1,16 @@
;; Project : Fibonacci sequence
;; Date : 2018/03/05
;; Author : Gal Zsolt [~ CalmoSoft ~]
;; Email : <calmosoft@gmail.com
(defun fibonacci (nr)
(cond ((= nr 0) 1)
((= nr 1) 1)
(t (+ (fibonacci (- nr 1))
(fibonacci (- nr 2))))))
(format t "~a" "First 10 Fibonacci numbers")
(dotimes (n 10)
(if (< n 1) (terpri))
(if (< n 9) (format t "~a" " "))
(write(+ n 1)) (format t "~a" ": ")
(write (fibonacci n)) (terpri))

View file

@ -0,0 +1,26 @@
import extensions.
fibu = (:i)
[
var ac := Array new(2); populate(:i)(i).
if (i < 2)
[ ^ ac[i] ];
[
2 to:i do(:i)
[
var t := ac[1].
ac[1] := ac[0] + ac[1].
ac[0] := t.
].
^ ac[1]
]
].
program =
[
0 to:10 do(:i)
[
console printLine(fibu(i)).
]
].

View file

@ -0,0 +1,4 @@
fib(N) -> fib(N, 0, 1).
fib(0, Result, _Next) -> Result;
fib(Iter, Result, Next) -> fib(Iter-1, Next, Result+Next).

View file

@ -1,44 +1 @@
import Data.List (transpose)
fib
:: (Integral b, Num a)
=> b -> a
fib 0 = 0 -- this line is necessary because "something ^ 0" returns "fromInteger 1", which unfortunately
-- in our case is not our multiplicative identity (the identity matrix) but just a 1x1 matrix of 1
fib n = (last . head . unMat) (Mat [[1, 1], [1, 0]] ^ n)
-- Code adapted from Matrix exponentiation operator task ---------------------
(<+>)
:: Num c
=> [c] -> [c] -> [c]
(<+>) = zipWith (+)
(<*>)
:: Num a
=> [a] -> [a] -> a
(<*>) = (sum .) . zipWith (*)
newtype Mat a = Mat
{ unMat :: [[a]]
} deriving (Eq)
instance Show a =>
Show (Mat a) where
show xm = "Mat " ++ show (unMat xm)
instance Num a =>
Num (Mat a) where
negate xm = Mat $ map (map negate) $ unMat xm
xm + ym = Mat $ zipWith (<+>) (unMat xm) (unMat ym)
xm * ym =
Mat
[ [ xs Main.<*> ys -- to distinguish from standard applicative operator
| ys <- transpose $ unMat ym ]
| xs <- unMat xm ]
fromInteger n = Mat [[fromInteger n]]
abs = undefined
signum = undefined
-- TEST ----------------------------------------------------------------------
main :: IO ()
main = (print . take 10 . show . fib) (10 ^ 5)
fib = 0 : 1 : (zipWith (+) <*> tail) fib

View file

@ -1,35 +1 @@
import Control.Arrow ((&&&))
fibstep :: (Integer, Integer) -> (Integer, Integer)
fibstep (a, b) = (b, a + b)
fibnums :: [Integer]
fibnums = map fst $ iterate fibstep (0, 1)
fibN2 :: Integer -> (Integer, Integer)
fibN2 m
| m < 10 = iterate fibstep (0, 1) !! fromIntegral m
fibN2 m = fibN2_next (n, r) (fibN2 n)
where
(n, r) = quotRem m 3
fibN2_next (n, r) (f, g)
| r == 0 = (a, b) -- 3n ,3n+1
| r == 1 = (b, c) -- 3n+1,3n+2
| r == 2 = (c, d) -- 3n+2,3n+3 (*)
where
a =
5 * f ^ 3 +
if even n
then 3 * f
else (-3 * f) -- 3n
b = g ^ 3 + 3 * g * f ^ 2 - f ^ 3 -- 3n+1
c = g ^ 3 + 3 * g ^ 2 * f + f ^ 3 -- 3n+2
d =
5 * g ^ 3 +
if even n
then (-3 * g)
else 3 * g -- 3(n+1) (*)
main :: IO ()
main = print $ (length &&& take 20) . show . fst $ fibN2 (10 ^ 2)
fib = 0 : 1 : next fib where next (a: t@(b:_)) = (a+b) : next t

View file

@ -1,2 +1 @@
*Main> (length &&& take 20) . show . fst $ fibN2 (10^6)
(208988,"19532821287077577316")
fib = 0 : scanl (+) 1 fib

View file

@ -1 +1,9 @@
f (n,(a,b)) = (2*n,(a*a+b*b,2*a*b+b*b)) -- iterate f (1,(0,1)) ; b is nth
import Data.List (foldl') --'
fib :: Integer -> Integer
fib n =
fst $
foldl' --'
(\(a, b) _ -> (b, a + b))
(0, 1)
[1 .. n]

View file

@ -1 +1,44 @@
g (n,(a,b)) = (2*n,(2*a*b-a*a,a*a+b*b)) -- iterate g (1,(1,1)) ; a is nth
import Data.List (transpose)
fib
:: (Integral b, Num a)
=> b -> a
fib 0 = 0 -- this line is necessary because "something ^ 0" returns "fromInteger 1", which unfortunately
-- in our case is not our multiplicative identity (the identity matrix) but just a 1x1 matrix of 1
fib n = (last . head . unMat) (Mat [[1, 1], [1, 0]] ^ n)
-- Code adapted from Matrix exponentiation operator task ---------------------
(<+>)
:: Num c
=> [c] -> [c] -> [c]
(<+>) = zipWith (+)
(<*>)
:: Num a
=> [a] -> [a] -> a
(<*>) = (sum .) . zipWith (*)
newtype Mat a = Mat
{ unMat :: [[a]]
} deriving (Eq)
instance Show a =>
Show (Mat a) where
show xm = "Mat " ++ show (unMat xm)
instance Num a =>
Num (Mat a) where
negate xm = Mat $ map (map negate) $ unMat xm
xm + ym = Mat $ zipWith (<+>) (unMat xm) (unMat ym)
xm * ym =
Mat
[ [ xs Main.<*> ys -- to distinguish from standard applicative operator
| ys <- transpose $ unMat ym ]
| xs <- unMat xm ]
fromInteger n = Mat [[fromInteger n]]
abs = undefined
signum = undefined
-- TEST ----------------------------------------------------------------------
main :: IO ()
main = (print . take 10 . show . fib) (10 ^ 5)

View file

@ -0,0 +1,35 @@
import Control.Arrow ((&&&))
fibstep :: (Integer, Integer) -> (Integer, Integer)
fibstep (a, b) = (b, a + b)
fibnums :: [Integer]
fibnums = map fst $ iterate fibstep (0, 1)
fibN2 :: Integer -> (Integer, Integer)
fibN2 m
| m < 10 = iterate fibstep (0, 1) !! fromIntegral m
fibN2 m = fibN2_next (n, r) (fibN2 n)
where
(n, r) = quotRem m 3
fibN2_next (n, r) (f, g)
| r == 0 = (a, b) -- 3n ,3n+1
| r == 1 = (b, c) -- 3n+1,3n+2
| r == 2 = (c, d) -- 3n+2,3n+3 (*)
where
a =
5 * f ^ 3 +
if even n
then 3 * f
else (-3 * f) -- 3n
b = g ^ 3 + 3 * g * f ^ 2 - f ^ 3 -- 3n+1
c = g ^ 3 + 3 * g ^ 2 * f + f ^ 3 -- 3n+2
d =
5 * g ^ 3 +
if even n
then (-3 * g)
else 3 * g -- 3(n+1) (*)
main :: IO ()
main = print $ (length &&& take 20) . show . fst $ fibN2 (10 ^ 2)

View file

@ -0,0 +1,2 @@
*Main> (length &&& take 20) . show . fst $ fibN2 (10^6)
(208988,"19532821287077577316")

View file

@ -0,0 +1 @@
f (n,(a,b)) = (2*n,(a*a+b*b,2*a*b+b*b)) -- iterate f (1,(0,1)) ; b is nth

View file

@ -0,0 +1 @@
g (n,(a,b)) = (2*n,(2*a*b-a*a,a*a+b*b)) -- iterate g (1,(1,1)) ; a is nth

View file

@ -1,5 +1,6 @@
fib n = go n 0 1
where
go n a b
| n == 0 = a
| otherwise = go (n - 1) b (a + b)
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo f where
f 0 = 0
f 1 = 1
f n = fib (n-1) + fib (n-2)

View file

@ -1 +1,6 @@
fib = 0 : 1 : zipWith (+) fib (tail fib)
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo $ \x -> case x of
0 -> 0
1 -> 1
n -> fib (n-1) + fib (n-2)

View file

@ -1 +1,7 @@
fib = 0 : 1 : (zipWith (+) <*> tail) fib
{-# Language LambdaCase #-}
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo $ \case
0 -> 0
1 -> 1
n -> fib (n-1) + fib (n-2)

View file

@ -1 +1,8 @@
fib = 0 : 1 : next fib where next (a: t@(b:_)) = (a+b) : next t
{-# Language LambdaCase #-}
import Data.MemoTrie
fib :: Integer -> Integer
fib = memo $ \case
0 -> 0
1 -> 1
n | n>0 -> fib (n-1) + fib (n-2)
| otherwise -> fib (n+2) - fib (n+1)

View file

@ -1 +1,5 @@
fib = 0 : scanl (+) 1 fib
fib n = go n 0 1
where
go n a b
| n == 0 = a
| otherwise = go (n - 1) b (a + b)

View file

@ -1,9 +1 @@
import Data.List (foldl') --'
fib :: Integer -> Integer
fib n =
fst $
foldl' --'
(\(a, b) _ -> (b, a + b))
(0, 1)
[1 .. n]
fib = 0 : 1 : zipWith (+) fib (tail fib)

View file

@ -0,0 +1,12 @@
function number = fibonacci(n)
%construct the Tartaglia/Pascal Triangle
pt=tril(ones(n));
for r = 3 : n
% Every element is the addition of the two elements
% on top of it. That means the previous row.
for c = 2 : r-1
pt(r, c) = pt(r-1, c-1) + pt(r-1, c);
end
end
number=trace(rot90(pt));
end

View file

@ -0,0 +1,2 @@
fib[n] /. RSolve[{fib[n] == fib[n - 1] + fib[n - 2], fib[0] == 0,
fib[1] == 1}, fib[n], n][[1]]

View file

@ -0,0 +1 @@
Fibonacci[n] // FunctionExpand // FullSimplify

View file

@ -0,0 +1 @@
(2^-n ((1 + Sqrt[5])^n - (-1 + Sqrt[5])^n Cos[n π]))/Sqrt[5]

View file

@ -0,0 +1,37 @@
MODULE Fibonacci;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
PROCEDURE Fibonacci(n : LONGINT) : LONGINT;
VAR
a,b,c : LONGINT;
BEGIN
IF n<0 THEN RETURN 0 END;
a:=1;
b:=1;
WHILE n>0 DO
c := a + b;
a := b;
b := c;
DEC(n)
END;
RETURN a
END Fibonacci;
VAR
buf : ARRAY[0..63] OF CHAR;
i : INTEGER;
r : LONGINT;
BEGIN
FOR i:=0 TO 10 DO
r := Fibonacci(i);
FormatString("%l\n", buf, r);
WriteString(buf);
END;
ReadChar
END Fibonacci.

View file

@ -1,7 +1 @@
matantihadamard(n)={
matrix(n,n,i,j,
my(t=j-i+1);
if(t<1,t%2,t<3)
);
}
fib(n)=matdet(matantihadamard(n))
fib(n)=n--;polchebyshev(n,2,I/2)*I^n;

View file

@ -1,7 +1 @@
fib(n)=
{
my(g=2^(n+1)-1);
sum(i=2^(n-1),2^n-1,
bitor(i,i<<1)==g
);
}
fib(n)=abs(polchebyshev(n-1,2,I/2));

View file

@ -1 +1,7 @@
fib(n)=my(k=0);while(n--,k++;while(!issquare(5*k^2+4)&&!issquare(5*k^2-4),k++));k
matantihadamard(n)={
matrix(n,n,i,j,
my(t=j-i+1);
if(t<1,t%2,t<3)
);
}
fib(n)=matdet(matantihadamard(n))

View file

@ -0,0 +1,7 @@
fib(n)=
{
my(g=2^(n+1)-1);
sum(i=2^(n-1),2^n-1,
bitor(i,i<<1)==g
);
}

View file

@ -0,0 +1 @@
fib(n)=my(k=0);while(n--,k++;while(!issquare(5*k^2+4)&&!issquare(5*k^2-4),k++));k

View file

@ -1,5 +1,4 @@
use experimental :cached;
proto fib (Int $n --> Int) is cached {*}
proto fib (Int $n --> Int) {*}
multi fib (0) { 0 }
multi fib (1) { 1 }
multi fib ($n) { fib($n - 1) + fib($n - 2) }

View file

@ -1,11 +1,15 @@
# Uses GMP method so very fast
use Math::AnyNum qw/fibonacci/;
say fibonacci(10000);
# Uses GMP method, so also very fast
use Math::GMP;
say Math::GMP::fibonacci(10000);
# Binary ladder, GMP if available, Pure Perl otherwise
use ntheory qw/lucasu/;
say lucasu(1, -1, 10000);
# Uses GMP internal method, so similar performance as above
use Math::GMP;
say Math::GMP::fibonacci(10000);
# All Perl
use Math::NumSeq::Fibonacci;
my $seq = Math::NumSeq::Fibonacci->new;

View file

@ -1,7 +1,4 @@
(de fibo (N)
(let (I 1 J 0)
(de fib (N)
(let (A 0 B 1)
(do N
(let (Tmp J)
(inc 'J I)
(setq I Tmp) ) )
J) )
(prog1 B (setq B (+ A B) A @)) ) ) )

View file

@ -0,0 +1,11 @@
(co 'fibo
(let (A 0 B 1)
(yield 'ready)
(while
(yield
(swap 'B (+ (swap 'A B) B)) ) ) ) )
(do 15
(printsp (yield 'next 'fibo)) )
(prinl)
(yield NIL 'fibo)

View file

@ -1,10 +1,8 @@
def fib():
"""Yield fib[n+1] + fib[n]"""
yield 1 # have to start somewhere
lhs, rhs = fib(), fib()
yield next(lhs) # move lhs one iteration ahead
while True:
yield next(lhs)+next(rhs)
f=fib()
print [next(f) for _ in range(9)]
F = {0: 0, 1: 1, 2: 1}
def fib(n):
if n in F:
return F[n]
f1 = fib(n // 2 + 1)
f2 = fib((n - 1) // 2)
F[n] = (f1 * f1 + f2 * f2 if n & 1 else f1 * f1 - f2 * f2)
return F[n]

View file

@ -1,11 +1,10 @@
from itertools import islice
def fib():
yield 0
yield 1
a, b = fib(), fib()
next(b)
"""Yield fib[n+1] + fib[n]"""
yield 1 # have to start somewhere
lhs, rhs = fib(), fib()
yield next(lhs) # move lhs one iteration ahead
while True:
yield next(a)+next(b)
yield next(lhs)+next(rhs)
print(tuple(islice(fib(), 10)))
f=fib()
print [next(f) for _ in range(9)]

View file

@ -0,0 +1,11 @@
from itertools import islice
def fib():
yield 0
yield 1
a, b = fib(), fib()
next(b)
while True:
yield next(a)+next(b)
print(tuple(islice(fib(), 10)))

View file

@ -4,7 +4,7 @@ Function fibo(n as integer) As UInt64
dim noTwo as UInt64 = 1
dim sum As UInt64
for i as integer = 1 to n
for i as integer = 3 to n
sum = noOne + noTwo
noTwo = noOne
noOne = sum

View file

@ -1,11 +1,4 @@
#lang racket
(require math/matrix)
(define (fibmat n) (matrix-ref
(matrix-expt (matrix ([1 1]
[1 0]))
n)
1 0))
(fibmat 1000)
(define (fib n (a 0) (b 1))
(if (< n 2)
1
(+ a (fib (- n 1) b (+ a b)))))

View file

@ -0,0 +1,11 @@
#lang racket
(require math/matrix)
(define (fibmat n) (matrix-ref
(matrix-expt (matrix ([1 1]
[1 0]))
n)
1 0))
(fibmat 1000)

View file

@ -1,5 +1,4 @@
def fib(x:Int, prev: BigInt = 0, next: BigInt = 1):BigInt = x match {
case 0 => prev
case 1 => next
case _ => fib(x-1, next, (next + prev))
case _ => fib(x-1, next, next + prev)
}

View file

@ -0,0 +1,7 @@
program fib
args n
clear
qui set obs `n'
qui gen a=1
qui replace a=a[_n-1]+a[_n-2] in 3/l
end

View file

@ -0,0 +1,12 @@
program fib
args n
clear
qui set obs `n'
qui gen a=.
dyngen {
update a=a[_n-1]+a[_n-2], missval(1)
}
end
fib 10
list