Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -3,7 +3,8 @@ The '''Fibonacci sequence''' is a sequence F<sub>n</sub> of natural numbers defi
|
|||
F<sub>1</sub> = 1
|
||||
F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub>, if n>1
|
||||
|
||||
Write a function to generate the nth Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion).
|
||||
Write a function to generate the nth Fibonacci number.
|
||||
Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion).
|
||||
|
||||
The sequence is sometimes extended into negative numbers by using a straightforward inverse of the positive definition:
|
||||
|
||||
|
|
|
|||
49
Task/Fibonacci-sequence/360-Assembly/fibonacci-sequence.360
Normal file
49
Task/Fibonacci-sequence/360-Assembly/fibonacci-sequence.360
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
FIBONACC CSECT
|
||||
USING FIBONACC,R12
|
||||
SAVEAREA B STM-SAVEAREA(R15)
|
||||
DC 17F'0'
|
||||
DC CL8'FIBONACC'
|
||||
STM STM R14,R12,12(R13)
|
||||
ST R13,4(R15)
|
||||
ST R15,8(R13)
|
||||
LR R12,R15
|
||||
* ---- CODE
|
||||
LA R1,0 f1=0
|
||||
LA R2,1 f2=1
|
||||
LA R4,1 i=1
|
||||
LA R6,1
|
||||
LH R7,N
|
||||
LOOP BXH R4,R6,ENDLOOP for i=2 to n
|
||||
LR R3,R2
|
||||
AR R3,R1 f3=f1+f2
|
||||
CVD R4,P i
|
||||
UNPK Z,P
|
||||
MVC C,Z
|
||||
OI C+L'C-1,X'F0'
|
||||
MVC WTOBUF+5(5),C+11
|
||||
CVD R3,P f3
|
||||
UNPK Z,P
|
||||
MVC C,Z
|
||||
OI C+L'C-1,X'F0'
|
||||
MVC WTOBUF+12(10),C+6
|
||||
WTO MF=(E,WTOMSG)
|
||||
LR R1,R2 f1=f2
|
||||
LR R2,R3 f2=f3
|
||||
B LOOP next i
|
||||
ENDLOOP EQU *
|
||||
* ---- END CODE
|
||||
RETURN EQU *
|
||||
LM R14,R12,12(R13)
|
||||
XR R15,R15
|
||||
BR R14
|
||||
* ---- DATA
|
||||
N DC H'46' max i
|
||||
P DS PL8
|
||||
Z DS ZL16
|
||||
C DS CL16
|
||||
WTOMSG DS 0F
|
||||
DC H'80'
|
||||
DC H'0'
|
||||
WTOBUF DC CL80'fibo(12345)=1234567890 '
|
||||
YREGS
|
||||
END FIBONACC
|
||||
1
Task/Fibonacci-sequence/APL/fibonacci-sequence-1.apl
Normal file
1
Task/Fibonacci-sequence/APL/fibonacci-sequence-1.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
↑+.×/N/⊂2 2⍴1 1 1 0
|
||||
1
Task/Fibonacci-sequence/APL/fibonacci-sequence-2.apl
Normal file
1
Task/Fibonacci-sequence/APL/fibonacci-sequence-2.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
↑0 1↓↑+.×/N/⊂2 2⍴1 1 1 0
|
||||
2
Task/Fibonacci-sequence/ATS/fibonacci-sequence-1.ats
Normal file
2
Task/Fibonacci-sequence/ATS/fibonacci-sequence-1.ats
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fun fib_rec(n: int): int =
|
||||
if n >= 2 then fib_rec(n-1) + fib_rec(n-2) else n
|
||||
9
Task/Fibonacci-sequence/ATS/fibonacci-sequence-2.ats
Normal file
9
Task/Fibonacci-sequence/ATS/fibonacci-sequence-2.ats
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(*
|
||||
** This one is also referred to as being tail-recursive
|
||||
*)
|
||||
fun
|
||||
fib_trec(n: int): int =
|
||||
if
|
||||
n > 0
|
||||
then (fix loop (i:int, r0:int, r1:int): int => if i > 1 then loop (i-1, r1, r0+r1) else r1)(n, 0, 1)
|
||||
else 0
|
||||
27
Task/Fibonacci-sequence/ATS/fibonacci-sequence-3.ats
Normal file
27
Task/Fibonacci-sequence/ATS/fibonacci-sequence-3.ats
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(*
|
||||
** This implementation is verified!
|
||||
*)
|
||||
|
||||
dataprop FIB (int, int) =
|
||||
| FIB0 (0, 0) | FIB1 (1, 1)
|
||||
| {n:nat} {r0,r1:int} FIB2 (n+2, r0+r1) of (FIB (n, r0), FIB (n+1, r1))
|
||||
// end of [FIB] // end of [dataprop]
|
||||
|
||||
fun
|
||||
fibats{n:nat}
|
||||
(n: int (n))
|
||||
: [r:int] (FIB (n, r) | int r) = let
|
||||
fun loop
|
||||
{i:nat | i <= n}{r0,r1:int}
|
||||
(
|
||||
pf0: FIB (i, r0), pf1: FIB (i+1, r1)
|
||||
| ni: int (n-i), r0: int r0, r1: int r1
|
||||
) : [r:int] (FIB (n, r) | int r) =
|
||||
if (ni > 0)
|
||||
then loop{i+1}(pf1, FIB2 (pf0, pf1) | ni - 1, r1, r0 + r1)
|
||||
else (pf0 | r0)
|
||||
// end of [if]
|
||||
// end of [loop]
|
||||
in
|
||||
loop {0} (FIB0 (), FIB1 () | n, 0, 1)
|
||||
end // end of [fibats]
|
||||
79
Task/Fibonacci-sequence/ATS/fibonacci-sequence-4.ats
Normal file
79
Task/Fibonacci-sequence/ATS/fibonacci-sequence-4.ats
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
(* ****** ****** *)
|
||||
//
|
||||
// How to compile:
|
||||
// patscc -o fib fib.dats
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
#include
|
||||
"share/atspre_staload.hats"
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
abst@ype
|
||||
int3_t0ype =
|
||||
(int, int, int)
|
||||
//
|
||||
typedef int3 = int3_t0ype
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
extern
|
||||
fun int3 : (int, int, int) -<> int3
|
||||
extern
|
||||
fun int3_1 : int3 -<> int
|
||||
extern
|
||||
fun mul_int3_int3: (int3, int3) -<> int3
|
||||
|
||||
(* ****** ****** *)
|
||||
|
||||
local
|
||||
|
||||
assume
|
||||
int3_t0ype = (int, int, int)
|
||||
|
||||
in (* in-of-local *)
|
||||
//
|
||||
implement
|
||||
int3 (x, y, z) = @(x, y, z)
|
||||
//
|
||||
implement int3_1 (xyz) = xyz.1
|
||||
//
|
||||
implement
|
||||
mul_int3_int3
|
||||
(
|
||||
@(a,b,c), @(d,e,f)
|
||||
) =
|
||||
(a*d + b*e, a*e + b*f, b*e + c*f)
|
||||
//
|
||||
end // end of [local]
|
||||
|
||||
(* ****** ****** *)
|
||||
//
|
||||
implement
|
||||
gnumber_int<int3> (n) = int3(n, 0, n)
|
||||
//
|
||||
implement gmul_val<int3> = mul_int3_int3
|
||||
//
|
||||
(* ****** ****** *)
|
||||
//
|
||||
fun
|
||||
fib (n: intGte(0)): int =
|
||||
int3_1(gpow_int_val<int3> (n, int3(1, 1, 0)))
|
||||
//
|
||||
(* ****** ****** *)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
{
|
||||
//
|
||||
val N = 10
|
||||
val () = println! ("fib(", N, ") = ", fib(N))
|
||||
val N = 20
|
||||
val () = println! ("fib(", N, ") = ", fib(N))
|
||||
val N = 30
|
||||
val () = println! ("fib(", N, ") = ", fib(N))
|
||||
val N = 40
|
||||
val () = println! ("fib(", N, ") = ", fib(N))
|
||||
//
|
||||
} (* end of [main0] *)
|
||||
|
|
@ -1,17 +1,20 @@
|
|||
main:
|
||||
{ argv 0 th $d
|
||||
fib
|
||||
%d cr << }
|
||||
((main
|
||||
{{iter fib !}
|
||||
20 times
|
||||
|
||||
fib!:
|
||||
{ dup zero?
|
||||
{ dup one?
|
||||
{ cp <- 2 - fib -> 1 - fib + }
|
||||
{ zap 1 }
|
||||
if }
|
||||
{ zap 1 }
|
||||
if }
|
||||
collect !
|
||||
rev
|
||||
|
||||
zero?!: { 0 = }
|
||||
{%d " " . <<}
|
||||
each})
|
||||
|
||||
one?!: { 1 = }
|
||||
(collect { -1 take })
|
||||
|
||||
(fib
|
||||
{{dup 2 <}
|
||||
{fnord}
|
||||
{dup
|
||||
<- 2 - fib ! ->
|
||||
1 - fib !
|
||||
+ }
|
||||
ifte}))
|
||||
|
|
|
|||
6
Task/Fibonacci-sequence/Clojure/fibonacci-sequence-7.clj
Normal file
6
Task/Fibonacci-sequence/Clojure/fibonacci-sequence-7.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(defn fib [n]
|
||||
(case n
|
||||
0 0
|
||||
1 1
|
||||
(+ (fib (- n 1))
|
||||
(fib (- n 2)))))
|
||||
8
Task/Fibonacci-sequence/Clojure/fibonacci-sequence-8.clj
Normal file
8
Task/Fibonacci-sequence/Clojure/fibonacci-sequence-8.clj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(def fib
|
||||
(memoize
|
||||
(fn [n]
|
||||
(case n
|
||||
0 0
|
||||
1 1
|
||||
(+ (fib (- n 1))
|
||||
(fib (- n 2)))))))
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
fib_iter = (n) ->
|
||||
if n < 2
|
||||
return n
|
||||
[prev, curr] = 0, 1
|
||||
for i in [1..n]
|
||||
[prev, curr] = [curr, curr + prev]
|
||||
return curr
|
||||
return n if n < 2
|
||||
[prev, curr] = [0, 1]
|
||||
[prev, curr] = [curr, curr + prev] for i in [1..n]
|
||||
curr
|
||||
|
|
|
|||
|
|
@ -1,5 +1,2 @@
|
|||
fib_rec = (n) ->
|
||||
if n < 2
|
||||
return n
|
||||
else
|
||||
return fib_rec(n-1) + fib_rec(n-2)
|
||||
if n < 2 then n else fib_rec(n-1) + fib_rec(n-2)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
(defconstant +2x2-identity+ '(1 0 0 1))
|
||||
(defconstant +fib-seed+ '(1 1 1 0))
|
||||
|
||||
(defun multiply-2x2 (matrix-1 matrix-2)
|
||||
(let* ((a (first matrix-1)) (b (second matrix-1)) (c (third matrix-1)) (d (fourth matrix-1))
|
||||
(e (first matrix-2)) (f (second matrix-2)) (g (third matrix-2)) (h (fourth matrix-2))
|
||||
(ae (* a e)) (bg (* b g)) (af (* a f)) (bh (* b h))
|
||||
(ce (* c e)) (dg (* d g)) (cf (* c f)) (dh (* d h)))
|
||||
(list (+ ae bg) (+ af bh) (+ ce dg) (+ cf dh))))
|
||||
|
||||
(defun square-2x2 (matrix)
|
||||
(multiply-2x2 matrix matrix))
|
||||
|
||||
(defun 2x2-exponentiation (matrix n)
|
||||
(cond ((zerop n) +2x2-identity+)
|
||||
((eql n 1) matrix)
|
||||
((evenp n) (square-2x2 (2x2-exponentiation matrix (/ n 2))))
|
||||
(t (multiply-2x2 (square-2x2 (2x2-exponentiation matrix (/ (1- n) 2))) matrix))))
|
||||
|
||||
(defun fib (n)
|
||||
(car (2x2-exponentiation +fib-seed+ (1- n))))
|
||||
|
|
@ -1,18 +1,18 @@
|
|||
int fib(int n) {
|
||||
if(n==0 || n==1) {
|
||||
if (n==0 || n==1) {
|
||||
return n;
|
||||
}
|
||||
int prev=1;
|
||||
int current=1;
|
||||
for(int i=2;i<n;i++) {
|
||||
int next=prev+current;
|
||||
prev=current;
|
||||
current=next;
|
||||
var prev=1;
|
||||
var current=1;
|
||||
for (var i=2; i<n; i++) {
|
||||
var next = prev + current;
|
||||
prev = current;
|
||||
current = next;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
int fibRec(int n) => n==0||n==1 ? n : fibRec(n-1)+fibRec(n-2);
|
||||
int fibRec(int n) => n==0 || n==1 ? n : fibRec(n-1) + fibRec(n-2);
|
||||
|
||||
main() {
|
||||
print(fib(11));
|
||||
|
|
|
|||
|
|
@ -1,26 +1,14 @@
|
|||
# fibonacci is the infinite list of all Fibonacci numbers.
|
||||
#
|
||||
# Note that this program uses the symbols "1" and "+". You can specify
|
||||
# the definitions of those symbols however you like, allowing you to use
|
||||
# any system of arithmetic you need. For example, you can use either the
|
||||
# built-in long arithmetic, or infinite precision arithmetic, by simply
|
||||
# defining "1" and "+" appropriately.
|
||||
|
||||
\fibonacci =
|
||||
# (fib n) = the nth Fibonacci number
|
||||
\fib=
|
||||
(
|
||||
\fibonacci == (\x\y
|
||||
item x;
|
||||
\z = (+ x y)
|
||||
fibonacci y z
|
||||
)
|
||||
|
||||
fibonacci 1 1
|
||||
(@\loop\x\y\n
|
||||
le n 0 x;
|
||||
\z=(+ x y)
|
||||
\n=(- n 1)
|
||||
loop y z n
|
||||
)
|
||||
0 1
|
||||
)
|
||||
|
||||
# OK, so that's the list of *all* Fibonacci numbers. If you want the nth number,
|
||||
# you can extract it with the fib function as follows. By the way, this *does*
|
||||
# have the effect of caching, so once a particular point in the sequence is
|
||||
# calculated, it doesn't have to be calculated again.
|
||||
|
||||
# (fib n) is the nth fibonacci number, starting with n==0, or 1 if n is negative.
|
||||
\fib = (\n list_at fibonacci n 1)
|
||||
# Now test it:
|
||||
for 0 20 (\n say (fib n))
|
||||
|
|
|
|||
|
|
@ -1,12 +1,24 @@
|
|||
module fibonacci
|
||||
contains
|
||||
recursive function fibR(n) result(fib)
|
||||
integer, intent(in) :: n
|
||||
integer :: fib
|
||||
|
||||
select case (n)
|
||||
case (:0); fib = 0
|
||||
case (1); fib = 1
|
||||
case default; fib = fibR(n-1) + fibR(n-2)
|
||||
end select
|
||||
end function fibR
|
||||
FUNCTION IFIB(N)
|
||||
IF (N.EQ.0) THEN
|
||||
ITEMP0=0
|
||||
ELSE IF (N.EQ.1) THEN
|
||||
ITEMP0=1
|
||||
ELSE IF (N.GT.1) THEN
|
||||
ITEMP1=0
|
||||
ITEMP0=1
|
||||
DO 1 I=2,N
|
||||
ITEMP2=ITEMP1
|
||||
ITEMP1=ITEMP0
|
||||
ITEMP0=ITEMP1+ITEMP2
|
||||
1 CONTINUE
|
||||
ELSE
|
||||
ITEMP1=1
|
||||
ITEMP0=0
|
||||
DO 2 I=-1,N,-1
|
||||
ITEMP2=ITEMP1
|
||||
ITEMP1=ITEMP0
|
||||
ITEMP0=ITEMP2-ITEMP1
|
||||
2 CONTINUE
|
||||
END IF
|
||||
IFIB=ITEMP0
|
||||
END
|
||||
|
|
|
|||
|
|
@ -1,20 +1,11 @@
|
|||
function fibI(n)
|
||||
integer, intent(in) :: n
|
||||
integer, parameter :: fib0 = 0, fib1 = 1
|
||||
integer :: fibI, back1, back2, i
|
||||
|
||||
select case (n)
|
||||
case (:0); fibI = fib0
|
||||
case (1); fibI = fib1
|
||||
|
||||
case default
|
||||
fibI = fib1
|
||||
back1 = fib0
|
||||
do i = 2, n
|
||||
back2 = back1
|
||||
back1 = fibI
|
||||
fibI = back1 + back2
|
||||
end do
|
||||
end select
|
||||
end function fibI
|
||||
end module fibonacci
|
||||
EXTERNAL IFIB
|
||||
CHARACTER*10 LINE
|
||||
PARAMETER ( LINE = '----------' )
|
||||
WRITE(*,900) 'N', 'F[N]', 'F[-N]'
|
||||
WRITE(*,900) LINE, LINE, LINE
|
||||
DO 1 N = 0, 10
|
||||
WRITE(*,901) N, IFIB(N), IFIB(-N)
|
||||
1 CONTINUE
|
||||
900 FORMAT(3(X,A10))
|
||||
901 FORMAT(3(X,I10))
|
||||
END
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
program fibTest
|
||||
use fibonacci
|
||||
module fibonacci
|
||||
contains
|
||||
recursive function fibR(n) result(fib)
|
||||
integer, intent(in) :: n
|
||||
integer :: fib
|
||||
|
||||
do i = 0, 10
|
||||
print *, fibr(i), fibi(i)
|
||||
end do
|
||||
end program fibTest
|
||||
select case (n)
|
||||
case (:0); fib = 0
|
||||
case (1); fib = 1
|
||||
case default; fib = fibR(n-1) + fibR(n-2)
|
||||
end select
|
||||
end function fibR
|
||||
|
|
|
|||
20
Task/Fibonacci-sequence/Fortran/fibonacci-sequence-4.f
Normal file
20
Task/Fibonacci-sequence/Fortran/fibonacci-sequence-4.f
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
function fibI(n)
|
||||
integer, intent(in) :: n
|
||||
integer, parameter :: fib0 = 0, fib1 = 1
|
||||
integer :: fibI, back1, back2, i
|
||||
|
||||
select case (n)
|
||||
case (:0); fibI = fib0
|
||||
case (1); fibI = fib1
|
||||
|
||||
case default
|
||||
fibI = fib1
|
||||
back1 = fib0
|
||||
do i = 2, n
|
||||
back2 = back1
|
||||
back1 = fibI
|
||||
fibI = back1 + back2
|
||||
end do
|
||||
end select
|
||||
end function fibI
|
||||
end module fibonacci
|
||||
7
Task/Fibonacci-sequence/Fortran/fibonacci-sequence-5.f
Normal file
7
Task/Fibonacci-sequence/Fortran/fibonacci-sequence-5.f
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
program fibTest
|
||||
use fibonacci
|
||||
|
||||
do i = 0, 10
|
||||
print *, fibr(i), fibi(i)
|
||||
end do
|
||||
end program fibTest
|
||||
16
Task/Fibonacci-sequence/Go/fibonacci-sequence-3.go
Normal file
16
Task/Fibonacci-sequence/Go/fibonacci-sequence-3.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
func fibNumber() func() int {
|
||||
fib1, fib2 := 0, 1
|
||||
return func() int {
|
||||
fib1, fib2 = fib2, fib1 + fib2
|
||||
return fib1
|
||||
}
|
||||
}
|
||||
|
||||
func fibSequence(n int) int {
|
||||
f := fibNumber()
|
||||
fib := 0
|
||||
for i := 0; i < n; i++ {
|
||||
fib = f()
|
||||
}
|
||||
return fib
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
fibsteps (a,b) n
|
||||
| n <= 0 = (a,b)
|
||||
| True = fibsteps (b, a+b) (n-1)
|
||||
| n <= 0 = (a,b)
|
||||
| otherwise = fibsteps (b, a+b) (n-1)
|
||||
|
||||
fibnums :: [Integer]
|
||||
fibnums = map fst $ iterate (`fibsteps` 1) (0,1)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,28 @@
|
|||
public static long recFibN(final int n)
|
||||
{
|
||||
return (n < 2) ? n : recFibN(n - 1) + recFibN(n - 2);
|
||||
/**
|
||||
* O(log(n))
|
||||
*/
|
||||
public static long fib(long n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
|
||||
long i = (int) (n - 1);
|
||||
long a = 1, b = 0, c = 0, d = 1, tmp1,tmp2;
|
||||
|
||||
while (i > 0) {
|
||||
if (i % 2 != 0) {
|
||||
tmp1 = d * b + c * a;
|
||||
tmp2 = d * (b + a) + c * b;
|
||||
a = tmp1;
|
||||
b = tmp2;
|
||||
}
|
||||
|
||||
tmp1 = (long) (Math.pow(c, 2) + Math.pow(d, 2));
|
||||
tmp2 = d * (2 * c + d);
|
||||
|
||||
c = tmp1;
|
||||
d = tmp2;
|
||||
|
||||
i = i / 2;
|
||||
}
|
||||
return a + b;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
public static long anFibN(final long n)
|
||||
public static long recFibN(final int n)
|
||||
{
|
||||
double p = (1 + Math.sqrt(5)) / 2;
|
||||
double q = 1 / p;
|
||||
return (long) ((Math.pow(p, n) + Math.pow(q, n)) / Math.sqrt(5));
|
||||
return (n < 2) ? n : recFibN(n - 1) + recFibN(n - 2);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
public static long fibTailRec(final int n)
|
||||
public static long anFibN(final long n)
|
||||
{
|
||||
return fibInner(0, 1, n);
|
||||
}
|
||||
|
||||
private static long fibInner(final long a, final long b, final int n)
|
||||
{
|
||||
return n < 1 ? a : n == 1 ? b : fibInner(b, a + b, n - 1);
|
||||
double p = (1 + Math.sqrt(5)) / 2;
|
||||
double q = 1 / p;
|
||||
return (long) ((Math.pow(p, n) + Math.pow(q, n)) / Math.sqrt(5));
|
||||
}
|
||||
|
|
|
|||
9
Task/Fibonacci-sequence/Java/fibonacci-sequence-5.java
Normal file
9
Task/Fibonacci-sequence/Java/fibonacci-sequence-5.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
public static long fibTailRec(final int n)
|
||||
{
|
||||
return fibInner(0, 1, n);
|
||||
}
|
||||
|
||||
private static long fibInner(final long a, final long b, final int n)
|
||||
{
|
||||
return n < 1 ? a : n == 1 ? b : fibInner(b, a + b, n - 1);
|
||||
}
|
||||
|
|
@ -1,18 +1,10 @@
|
|||
function fib(n)
|
||||
{
|
||||
var
|
||||
a = 0,
|
||||
b = 1,
|
||||
t;
|
||||
while (n-- > 0)
|
||||
{
|
||||
t = a;
|
||||
a = b;
|
||||
b += t;
|
||||
}
|
||||
return a;
|
||||
function fib(n) {
|
||||
var a = 0, b = 1, t;
|
||||
while (n-- > 0) {
|
||||
t = a;
|
||||
a = b;
|
||||
b += t;
|
||||
console.log(a);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
var i;
|
||||
for (i = 0; i < 10; ++i)
|
||||
alert(fib(i));
|
||||
|
|
|
|||
10
Task/Fibonacci-sequence/JavaScript/fibonacci-sequence-5.js
Normal file
10
Task/Fibonacci-sequence/JavaScript/fibonacci-sequence-5.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function* fibonacciGenerator() {
|
||||
var prev = 0;
|
||||
var curr = 1;
|
||||
while (true) {
|
||||
yield curr;
|
||||
curr = curr + prev;
|
||||
prev = curr - prev;
|
||||
}
|
||||
}
|
||||
var fib = fibonacciGenerator();
|
||||
4
Task/Fibonacci-sequence/Lush/fibonacci-sequence.lush
Normal file
4
Task/Fibonacci-sequence/Lush/fibonacci-sequence.lush
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(de fib-rec (n)
|
||||
(if (< n 2)
|
||||
n
|
||||
(+ (fib-rec (- n 2)) (fib-rec (- n 1)))))
|
||||
|
|
@ -1,16 +1,6 @@
|
|||
function F = fibonacci(n)
|
||||
|
||||
Fn = [1 0]; %Fn(1) is F_{n-2}, Fn(2) is F_{n-1}
|
||||
F = 0; %F is F_{n}
|
||||
|
||||
for i = (1:abs(n))
|
||||
Fn(2) = F;
|
||||
F = sum(Fn);
|
||||
Fn(1) = Fn(2);
|
||||
end
|
||||
|
||||
if n < 0
|
||||
F = F*((-1)^(n+1));
|
||||
end
|
||||
|
||||
function f = fib(n)
|
||||
|
||||
f = [1 1 ; 1 0]^(n-1);
|
||||
f = f(1,1);
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,16 @@
|
|||
function number = fibonacci2(n)
|
||||
function F = fibonacci(n)
|
||||
|
||||
if n == 1
|
||||
number = 1;
|
||||
elseif n == 0
|
||||
number = 0;
|
||||
elseif n < 0
|
||||
number = ((-1)^(n+1))*fibonacci2(-n);;
|
||||
else
|
||||
number = det(gallery('dramadah',n,3));
|
||||
Fn = [1 0]; %Fn(1) is F_{n-2}, Fn(2) is F_{n-1}
|
||||
F = 0; %F is F_{n}
|
||||
|
||||
for i = (1:abs(n))
|
||||
Fn(2) = F;
|
||||
F = sum(Fn);
|
||||
Fn(1) = Fn(2);
|
||||
end
|
||||
|
||||
if n < 0
|
||||
F = F*((-1)^(n+1));
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
13
Task/Fibonacci-sequence/MATLAB/fibonacci-sequence-3.m
Normal file
13
Task/Fibonacci-sequence/MATLAB/fibonacci-sequence-3.m
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function number = fibonacci2(n)
|
||||
|
||||
if n == 1
|
||||
number = 1;
|
||||
elseif n == 0
|
||||
number = 0;
|
||||
elseif n < 0
|
||||
number = ((-1)^(n+1))*fibonacci2(-n);;
|
||||
else
|
||||
number = det(gallery('dramadah',n,3));
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fibi[prvprv_Integer, prv_Integer, rm_Integer] :=
|
||||
If[rm < 1, prvprv, fibi[prv, prvprv + prv, rm - 1]]
|
||||
fib[n_Integer] := fibi[0, 1, n]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fib[n_Integer] := Block[{tmp, prvprv = 0, prv = 1},
|
||||
For[i = 0, i < n, i++, tmp = prv; prv += prvprv; prvprv = tmp];
|
||||
Return[prvprv]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fibi[{prvprv_Integer, prv_Integer}] := {prv, prvprv + prv}
|
||||
fibList[n_Integer] := Map[Take[#, 1] &, NestList[fibi, {0, 1}, n]] // Flatten
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, \
|
||||
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, \
|
||||
196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, \
|
||||
9227465, 14930352, 24157817, 39088169, 63245986, 102334155, \
|
||||
165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, \
|
||||
2971215073, 4807526976, 7778742049, 12586269025, 20365011074, \
|
||||
32951280099, 53316291173, 86267571272, 139583862445, 225851433717, \
|
||||
365435296162, 591286729879, 956722026041, 1548008755920, \
|
||||
2504730781961, 4052739537881, 6557470319842, 10610209857723, \
|
||||
17167680177565, 27777890035288, 44945570212853, 72723460248141, \
|
||||
117669030460994, 190392490709135, 308061521170129, 498454011879264, \
|
||||
806515533049393, 1304969544928657, 2111485077978050, \
|
||||
3416454622906707, 5527939700884757, 8944394323791464, \
|
||||
14472334024676221, 23416728348467685, 37889062373143906, \
|
||||
61305790721611591, 99194853094755497, 160500643816367088, \
|
||||
259695496911122585, 420196140727489673, 679891637638612258, \
|
||||
1100087778366101931, 1779979416004714189, 2880067194370816120, \
|
||||
4660046610375530309, 7540113804746346429, 12200160415121876738, \
|
||||
19740274219868223167, 31940434634990099905, 51680708854858323072, \
|
||||
83621143489848422977, 135301852344706746049, 218922995834555169026, \
|
||||
354224848179261915075}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
FIBON:
|
||||
REM Fibonacci sequence is generated to the Organiser II floating point variable limit.
|
||||
REM This method was derived from (not copied...) the original OPL manual that came with the CM and XP in the mid 1980s.
|
||||
REM CLEAR/ON key quits.
|
||||
REM Mikesan - http://forum.psion2.org/
|
||||
LOCAL A,B,C
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
fib(n)={
|
||||
if(n<0,return((-1)^(n+1)*fib(n)));
|
||||
my(a=0,b=1,t);
|
||||
while(n,
|
||||
t=a+b;
|
||||
a=b;
|
||||
b=t;
|
||||
n--
|
||||
);
|
||||
a
|
||||
if(n<2,
|
||||
n
|
||||
,
|
||||
my(s=self());
|
||||
s(n-2)+s(n-1)
|
||||
)
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1 +1,11 @@
|
|||
fib(n)=my(k=0);while(n--,k++;while(!issquare(5*k^2+4)&&!issquare(5*k^2-4),k++));k
|
||||
fib(n)={
|
||||
if(n<0,return((-1)^(n+1)*fib(n)));
|
||||
my(a=0,b=1,t);
|
||||
while(n,
|
||||
t=a+b;
|
||||
a=b;
|
||||
b=t;
|
||||
n--
|
||||
);
|
||||
a
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
fib(n)=my(k=0);while(n--,k++;while(!issquare(5*k^2+4)&&!issquare(5*k^2-4),k++));k
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
/* Form the n-th Fibonacci number, n > 1. */
|
||||
get list (n);
|
||||
f1 = 0; f2, f3 = 1;
|
||||
do i = 1 to n-2;
|
||||
get list(n);
|
||||
f1 = 0; f2 = 1;
|
||||
do i = 2 to n;
|
||||
f3 = f1 + f2;
|
||||
put skip edit('fibo(',i,')=',f3)(a,f(5),a,f(5));
|
||||
f1 = f2;
|
||||
f2 = f3;
|
||||
end;
|
||||
put skip list (f3);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,17 @@
|
|||
function fib(n: integer): integer;
|
||||
begin
|
||||
if (n = 0) or (n = 1)
|
||||
then
|
||||
fib := n
|
||||
else
|
||||
fib := fib(n-1) + fib(n-2)
|
||||
end;
|
||||
function fib(n: integer):longInt;
|
||||
const
|
||||
Sqrt5 = sqrt(5.0);
|
||||
C1 = ln((Sqrt5+1.0)*0.5);//ln( 1.618..)
|
||||
//C2 = ln((1.0-Sqrt5)*0.5);//ln(-0.618 )) tsetsetse
|
||||
C2 = ln((Sqrt5-1.0)*0.5);//ln(+0.618 ))
|
||||
begin
|
||||
IF n>0 then
|
||||
begin
|
||||
IF odd(n) then
|
||||
fib := round((exp(C1*n) + exp(C2*n) )/Sqrt5)
|
||||
else
|
||||
fib := round((exp(C1*n) - exp(C2*n) )/Sqrt5)
|
||||
end
|
||||
else
|
||||
Fibdirekt := 0
|
||||
end;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,8 @@
|
|||
function fib(n: integer): integer;
|
||||
var
|
||||
f0, f1, f2, k: integer;
|
||||
begin
|
||||
f0 := 0;
|
||||
f1 := 1;
|
||||
for k := 2 to n do
|
||||
begin
|
||||
f2:= f0 + f1;
|
||||
f0 := f1;
|
||||
f1 := f2;
|
||||
end;
|
||||
fib := f2;
|
||||
if (n = 0) or (n = 1)
|
||||
then
|
||||
fib := n
|
||||
else
|
||||
fib := fib(n-1) + fib(n-2)
|
||||
end;
|
||||
|
|
|
|||
22
Task/Fibonacci-sequence/Pascal/fibonacci-sequence-3.pascal
Normal file
22
Task/Fibonacci-sequence/Pascal/fibonacci-sequence-3.pascal
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function fib(n: integer): integer;
|
||||
var
|
||||
f0, f1, tmpf0, k: integer;
|
||||
begin
|
||||
f1 := n;
|
||||
IF f1 >1 then
|
||||
begin
|
||||
k := f1-1;
|
||||
f0 := 0;
|
||||
f1 := 1;
|
||||
repeat
|
||||
tmpf0 := f0;
|
||||
f0 := f1;
|
||||
f1 := f1+tmpf0;
|
||||
dec(k);
|
||||
until k = 0;
|
||||
end
|
||||
else
|
||||
IF f1 < 0 then
|
||||
f1 := 0;
|
||||
fib := f1;
|
||||
end;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
proto fib (Int $n --> Int) {*}
|
||||
proto fib (Int $n --> Int) is cached {*}
|
||||
multi fib (0) { 0 }
|
||||
multi fib (1) { 1 }
|
||||
multi fib ($n) { fib($n - 1) + fib($n - 2) }
|
||||
|
|
|
|||
|
|
@ -1,32 +1,7 @@
|
|||
# Iterative Fibonacci with bignum support.
|
||||
# Multi-licensed under your choice of:
|
||||
# 1. The GNU Free Documentation License (GFDL).
|
||||
# 2. The MIT/X11 license.
|
||||
# 3. The GNU General Publice License (GPL).
|
||||
# 4. The Public Domain as understood by the CC-Zero public domain dedication.
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Math::BigInt try => 'GMP';
|
||||
|
||||
sub fib_iter
|
||||
{
|
||||
my ($n) = @_;
|
||||
|
||||
my $this_fib = Math::BigInt->new(0);
|
||||
my $next_fib = Math::BigInt->new(1);
|
||||
|
||||
my $pos = 0;
|
||||
|
||||
while ($pos < $n)
|
||||
{
|
||||
($this_fib, $next_fib) = ($next_fib, $this_fib+$next_fib);
|
||||
}
|
||||
continue
|
||||
{
|
||||
$pos++;
|
||||
}
|
||||
|
||||
return $this_fib;
|
||||
sub fib_iter {
|
||||
my $n = shift;
|
||||
use bigint try => "GMP,Pari";
|
||||
my ($v2,$v1) = (-1,1);
|
||||
($v2,$v1) = ($v1,$v2+$v1) for 0..$n;
|
||||
$v1;
|
||||
}
|
||||
|
|
|
|||
20
Task/Fibonacci-sequence/Perl/fibonacci-sequence-3.pl
Normal file
20
Task/Fibonacci-sequence/Perl/fibonacci-sequence-3.pl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# 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;
|
||||
say $seq->ith(10000);
|
||||
|
||||
# All Perl
|
||||
use Math::Big qw/fibonacci/;
|
||||
say 0+fibonacci(10000); # Force scalar context
|
||||
|
||||
# Perl, gives floating point *approximation*
|
||||
use Math::Fibonacci qw/term/;
|
||||
say term(10000);
|
||||
10
Task/Fibonacci-sequence/Python/fibonacci-sequence-10.py
Normal file
10
Task/Fibonacci-sequence/Python/fibonacci-sequence-10.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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)]
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
def fibGen():
|
||||
f0, f1 = 0, 1
|
||||
while True:
|
||||
yield f0
|
||||
f0, f1 = f1, f0+f1
|
||||
def fibFastRec(n):
|
||||
def fib(prvprv, prv, c):
|
||||
if c < 1: return prvprv
|
||||
else: return fib(prv, prvprv + prv, c - 1)
|
||||
return fib(0, 1, n)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,4 @@
|
|||
>>> fg = fibGen()
|
||||
>>> for x in range(9):
|
||||
print fg.next()
|
||||
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
5
|
||||
8
|
||||
13
|
||||
21
|
||||
>>>
|
||||
def fibGen(n,a=0,b=1):
|
||||
while n>0:
|
||||
yield a
|
||||
a,b,n = b,a+b,n-1
|
||||
|
|
|
|||
|
|
@ -1,30 +1,3 @@
|
|||
def prevPowTwo(n):
|
||||
'Gets the power of two that is less than or equal to the given input'
|
||||
if ((n & -n) == n):
|
||||
return n
|
||||
else:
|
||||
n -= 1
|
||||
n |= n >> 1
|
||||
n |= n >> 2
|
||||
n |= n >> 4
|
||||
n |= n >> 8
|
||||
n |= n >> 16
|
||||
n += 1
|
||||
return (n/2)
|
||||
>>> [i for i in fibGen(11)]
|
||||
|
||||
def crazyFib(n):
|
||||
'Crazy fast fibonacci number calculation'
|
||||
powTwo = prevPowTwo(n)
|
||||
|
||||
q = r = i = 1
|
||||
s = 0
|
||||
|
||||
while(i < powTwo):
|
||||
i *= 2
|
||||
q, r, s = q*q + r*r, r * (q + s), (r*r + s*s)
|
||||
|
||||
while(i < n):
|
||||
i += 1
|
||||
q, r, s = q+r, q, r
|
||||
|
||||
return q
|
||||
[0,1,1,2,3,5,8,13,21,34,55]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,30 @@
|
|||
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]
|
||||
def prevPowTwo(n):
|
||||
'Gets the power of two that is less than or equal to the given input'
|
||||
if ((n & -n) == n):
|
||||
return n
|
||||
else:
|
||||
n -= 1
|
||||
n |= n >> 1
|
||||
n |= n >> 2
|
||||
n |= n >> 4
|
||||
n |= n >> 8
|
||||
n |= n >> 16
|
||||
n += 1
|
||||
return (n/2)
|
||||
|
||||
fib(10000000) # calculating it takes a few seconds, printing it takes eons
|
||||
def crazyFib(n):
|
||||
'Crazy fast fibonacci number calculation'
|
||||
powTwo = prevPowTwo(n)
|
||||
|
||||
q = r = i = 1
|
||||
s = 0
|
||||
|
||||
while(i < powTwo):
|
||||
i *= 2
|
||||
q, r, s = q*q + r*r, r * (q + s), (r*r + s*s)
|
||||
|
||||
while(i < n):
|
||||
i += 1
|
||||
q, r, s = q+r, q, r
|
||||
|
||||
return q
|
||||
|
|
|
|||
7
Task/Fibonacci-sequence/Python/fibonacci-sequence-9.py
Normal file
7
Task/Fibonacci-sequence/Python/fibonacci-sequence-9.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,23 +1,23 @@
|
|||
/*REXX program calculates the Nth Fibonacci number, N can be zero or neg*/
|
||||
numeric digits 210000 /*prepare for some big 'uns. */
|
||||
parse arg x y . /*allow a single number or range.*/
|
||||
if x=='' then do; x=-40; y=abs(x); end /*No input? Use range -40 ──► +40*/
|
||||
if y=='' then y=x /*if only one number, show fib(n)*/
|
||||
w=max(length(x),length(y)) /*used for making output pretty. */
|
||||
fw=10 /*minmum maximum width. Ka-razy*/
|
||||
do j=x to y; q=fib(j) /*process each Fibonacci request.*/
|
||||
fw=max(fw,length(q)) /*fib# length or the max so far. */
|
||||
numeric digits 210000 /*be able to handle some big 'uns*/
|
||||
parse arg x y . /*allow a single number or range.*/
|
||||
if x=='' then do; x=-40; y=+40; end /*No input? Use range -40 ──► +40*/
|
||||
if y=='' then y=x /*if only one number, show fib(n)*/
|
||||
w=max(length(x), length(y)) /*used for making output pretty. */
|
||||
fw=10 /*minmum maximum width. Ka-razy.*/
|
||||
do j=x to y; q=fib(j) /*process each Fibonacci request.*/
|
||||
L=length(q) /*obtain the length (width) of Q.*/
|
||||
fw=max(fw, L) /*fib# length or the max so far. */
|
||||
say 'Fibonacci('right(j,w)") = " right(q,fw) /*right justify Q.*/
|
||||
if length(q)>10 then say 'Fibonacci('right(j,w)") has a length of" l
|
||||
end /*j*/
|
||||
if L>10 then say 'Fibonacci('right(j,w)") has a length of" L
|
||||
end /*j*/ /* [↑] list a Fib seq. of x──►y */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────FIB subroutine (non-recursive)───*/
|
||||
fib: procedure; parse arg n; na=abs(n); a=0; b=1
|
||||
if na<2 then return na /*handle couple of special cases.*/
|
||||
|
||||
do k=2 to na; s=a+b /*sum the numbers up to │n│ */
|
||||
parse value b s with a b /*faster version of: a=b; s=b */
|
||||
end /*k*/
|
||||
|
||||
if n>0 | na//2==1 then return s /*if positive or odd negative ...*/
|
||||
return -s /* return a negative Fib number.*/
|
||||
/*──────────────────────────────────FIB subroutine──────────────────────*/
|
||||
fib: procedure; parse arg n; a=0; b=1; na=abs(n) /*use |n| */
|
||||
if na<2 then return na /*handle 3 special cases (-1,0,1)*/
|
||||
/* [↓] method is non-recursive.*/
|
||||
do k=2 to na; s=a+b; a=b; b=s /*sum the numbers up to │n│ */
|
||||
end /*k*/ /* [↑] (only positive Fibs used)*/
|
||||
/* [↓] na//2 [same as] na/2==1 */
|
||||
if n>0 | na//2 then return s /*if positive or odd negative ···*/
|
||||
return -s /*return a negative Fib number. */
|
||||
|
|
|
|||
5
Task/Fibonacci-sequence/Racket/fibonacci-sequence-1.rkt
Normal file
5
Task/Fibonacci-sequence/Racket/fibonacci-sequence-1.rkt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(define (fib n)
|
||||
(let loop ((cnt 0) (a 0) (b 1))
|
||||
(if (= n cnt)
|
||||
a
|
||||
(loop (+ cnt 1) b (+ a b)))))
|
||||
11
Task/Fibonacci-sequence/Racket/fibonacci-sequence-2.rkt
Normal file
11
Task/Fibonacci-sequence/Racket/fibonacci-sequence-2.rkt
Normal 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)
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
(define (fibo number)
|
||||
(define (fibo-rec number n i)
|
||||
(if (<= number 0)
|
||||
i
|
||||
(fibo-rec (- number 1) (+ n i) n)))
|
||||
(fibo-rec number 1 0))
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
def fibIter(n)
|
||||
def fib_iter(n)
|
||||
return 0 if n == 0
|
||||
fibPrev, fib = 1, 1
|
||||
(n.abs - 2).times { fibPrev, fib = fib, fib + fibPrev }
|
||||
fib * (n<0 ? (-1)**(n+1) : 1)
|
||||
fib_prev, fib = 1, 1
|
||||
(n.abs - 2).times { fib_prev, fib = fib, fib + fib_prev }
|
||||
fib * (n < 0 ? (-1)**(n + 1) : 1)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
def fibRec(n)
|
||||
def fib_rec(n)
|
||||
if n <= -2
|
||||
(-1)**(n+1) * fibRec(n.abs)
|
||||
(-1)**(n + 1) * fib_rec(n.abs)
|
||||
elsif n <= 1
|
||||
n.abs
|
||||
else
|
||||
fibRec(n-1) + fibRec(n-2)
|
||||
fib_rec(n - 1) + fib_rec(n - 2)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@
|
|||
|
||||
fib = Hash.new do |f, n|
|
||||
f[n] = if n <= -2
|
||||
(-1)**(n+1) * f[n.abs]
|
||||
(-1)**(n + 1) * f[n.abs]
|
||||
elsif n <= 1
|
||||
n.abs
|
||||
else
|
||||
f[n-1] + f[n-2]
|
||||
f[n - 1] + f[n - 2]
|
||||
end
|
||||
end
|
||||
# examples: fib[10] => 55, fib[-10] => (-55/1)
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ M = Matrix[[0, 1], [1,1]]
|
|||
# and then multiplying that by by M**(the remaining number of times). E.g., to compute
|
||||
# M**19, compute partial = ((M**2)**2) = M**16, and then compute partial*(M**3) = M**19.
|
||||
# That's only 5 matrix multiplications of M to compute M*19.
|
||||
def self.fibMatrix(n)
|
||||
def self.fib_matrix(n)
|
||||
return 0 if n <= 0 # F(0)
|
||||
return 1 if n == 1 # F(1)
|
||||
# To get F(n >= 2), compute M**(n - 1) and extract the lower right element.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
require 'generator'
|
||||
|
||||
def fibGen
|
||||
def fib_gen
|
||||
Generator.new do |g|
|
||||
f0, f1 = 0, 1
|
||||
loop do
|
||||
g.yield f0
|
||||
f0, f1 = f1, f0+f1
|
||||
f0, f1 = f1, f0 + f1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,12 +1,17 @@
|
|||
fn fib(n: int, f: fn (num: i64) -> bool) -> (i64, int) {
|
||||
// Works with 0.13.0-dev (f673e9841)
|
||||
fn fib<F>(n: i64, f: F) -> (i64, i64) where F: Fn(i64) {
|
||||
if n < 0 {
|
||||
// Let these variables be mutated, otherwise too slow
|
||||
let mut n1:i64 = 0, n2:i64 = -1, i:int = 0, tmp:i64;
|
||||
let mut n1:i64 = 0;
|
||||
let mut n2:i64 = -1;
|
||||
let mut i:i64 = 0;
|
||||
let mut tmp:i64;
|
||||
|
||||
while i > n {
|
||||
f(n1);
|
||||
tmp = n1-n2;
|
||||
if (tmp > 0 && n2 > 0) { //Detect overflow
|
||||
io::println("\nReached the limit of i64, halting");
|
||||
if tmp > 0 && n2 > 0 { //Detect overflow
|
||||
println!("\nReached the limit of i64, halting");
|
||||
return (n1, i);
|
||||
}
|
||||
n1 = n2;
|
||||
|
|
@ -16,12 +21,16 @@ fn fib(n: int, f: fn (num: i64) -> bool) -> (i64, int) {
|
|||
(n1+n2, n)
|
||||
} else if n > 0 {
|
||||
// And these variables
|
||||
let mut n1:i64 = 0, n2:i64 = 1, i:int = 0, tmp:i64;
|
||||
let mut n1:i64 = 0;
|
||||
let mut n2:i64 = 1;
|
||||
let mut i:i64 = 0;
|
||||
let mut tmp:i64;
|
||||
|
||||
while i < n {
|
||||
f(n1);
|
||||
tmp = n1+n2;
|
||||
if (tmp < 0) { //Detect overflow
|
||||
io::println("\nReached the limit of i64, halting");
|
||||
if tmp < 0 { //Detect overflow
|
||||
println!("\nReached the limit of i64, halting");
|
||||
return (n1, i);
|
||||
}
|
||||
n1 = n2;
|
||||
|
|
@ -36,21 +45,11 @@ fn fib(n: int, f: fn (num: i64) -> bool) -> (i64, int) {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let args = os::args();
|
||||
let n = if args.len() == 1 {
|
||||
10
|
||||
} else if args.len() > 1 {
|
||||
// Convert from a string
|
||||
match (int::from_str(args[1])) {
|
||||
Some(num) => num,
|
||||
None => 10 //Fall back to default
|
||||
}
|
||||
} else {
|
||||
/* Required to use the if as an expression.
|
||||
* We know that args.len() is always >= 1, the compiler
|
||||
* does not. fail lets it know that we can't get past here.
|
||||
*/
|
||||
fail ~"No arguments given, somehow...";
|
||||
let args = std::os::args();
|
||||
let default_n = 10i64;
|
||||
let n = match args.len() {
|
||||
1 => default_n,
|
||||
_ => args[1].parse().unwrap_or(default_n)
|
||||
};
|
||||
|
||||
/* Use the loop protocol to be able to do things
|
||||
|
|
@ -58,10 +57,10 @@ fn main() {
|
|||
* The loop itself returns a tuple with where it got to and
|
||||
* what the number is.
|
||||
*/
|
||||
let (result, n) = for fib(n) |num| {
|
||||
let (result, n) = fib(n, |num| {
|
||||
//print out the sequence
|
||||
io::print(fmt!("%? ", num));
|
||||
};
|
||||
print!("{} ", num);
|
||||
});
|
||||
|
||||
io::println(fmt!("\nThe %dth fibonacci number is: %?", n, result));
|
||||
println!("\nThe {}th fibonacci number is: {}", n, result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
// Works with 0.13.0-dev (f673e9841)
|
||||
fn main() {
|
||||
fn fib(n: int) -> int {
|
||||
fn _fib(n: int, a: int, b: int) -> int {
|
||||
match (n, a, b) {
|
||||
(0, _, _) => a,
|
||||
_ => _fib(n-1, a+b, a)
|
||||
}
|
||||
}
|
||||
|
||||
fn fib(n: int) -> int {
|
||||
_fib(n, 0, 1)
|
||||
}
|
||||
|
||||
fn _fib(n: int, a: int, b: int) -> int {
|
||||
match (n, a, b) {
|
||||
(0, _, _) => a,
|
||||
_ => _fib(n-1, a+b, a)
|
||||
}
|
||||
}
|
||||
|
||||
_fib(n, 0, 1)
|
||||
|
||||
}
|
||||
|
||||
for n in range(0,20) {
|
||||
println(fmt!("%d", fib(n)))
|
||||
}
|
||||
for n in range(0, 20) {
|
||||
println!("{}", fib(n));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
Task/Fibonacci-sequence/SQL/fibonacci-sequence.sql
Normal file
27
Task/Fibonacci-sequence/SQL/fibonacci-sequence.sql
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
CREATE FUNCTION fib(n int) RETURNS numeric AS $$
|
||||
-- This recursive with generates endless list of Fibonacci numbers.
|
||||
WITH RECURSIVE fibonacci(current, previous) AS (
|
||||
-- Initialize the current with 0, so the first value will be 0.
|
||||
-- The previous value is set to 1, because its only goal is not
|
||||
-- special casing the zero case, and providing 1 as the second
|
||||
-- number in the sequence.
|
||||
--
|
||||
-- The numbers end with dots to make them numeric type in
|
||||
-- Postgres. Numeric type has almost arbitrary precision
|
||||
-- (technically just 131,072 digits, but that's good enough for
|
||||
-- most purposes, including calculating huge Fibonacci numbers)
|
||||
SELECT 0., 1.
|
||||
UNION ALL
|
||||
-- To generate Fibonacci number, we need to add together two
|
||||
-- previous Fibonacci numbers. Current number is saved in order
|
||||
-- to be accessed in the next iteration of recursive function.
|
||||
SELECT previous + current, current FROM fibonacci
|
||||
)
|
||||
-- The user is only interested in current number, not previous.
|
||||
SELECT current FROM fibonacci
|
||||
-- We only need one number, so limit to 1
|
||||
LIMIT 1
|
||||
-- Offset the query by the requested argument to get the correct
|
||||
-- position in the list.
|
||||
OFFSET n
|
||||
$$ LANGUAGE SQL RETURNS NULL ON NULL INPUT IMMUTABLE;
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
def fib(i:Int, a:Int=1, b:Int=0):Int = i match{
|
||||
case 1 => b
|
||||
case _ => fib(i-1, b, a+b)
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,3 @@
|
|||
// 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
|
||||
val it = Iterator.iterate((0,1)){case (a,b) => (b,a+b)}.map(_._1)
|
||||
//example:
|
||||
println(it.take(13).mkString(",")) //prints: 0,1,1,2,3,5,8,13,21,34,55,89,144
|
||||
|
|
|
|||
11
Task/Fibonacci-sequence/Scilab/fibonacci-sequence.scilab
Normal file
11
Task/Fibonacci-sequence/Scilab/fibonacci-sequence.scilab
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
clear
|
||||
n=46
|
||||
f1=0; f2=1
|
||||
printf("fibo(%d)=%d\n",0,f1)
|
||||
printf("fibo(%d)=%d\n",1,f2)
|
||||
for i=2:n
|
||||
f3=f1+f2
|
||||
printf("fibo(%d)=%d\n",i,f3)
|
||||
f1=f2
|
||||
f2=f3
|
||||
end
|
||||
14
Task/Fibonacci-sequence/Visual-Basic/fibonacci-sequence.vb
Normal file
14
Task/Fibonacci-sequence/Visual-Basic/fibonacci-sequence.vb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Sub fibonacci()
|
||||
Const n = 139
|
||||
Dim i As Integer
|
||||
Dim f1 As Variant, f2 As Variant, f3 As Variant 'for Decimal
|
||||
f1 = CDec(0): f2 = CDec(1) 'for Decimal setting
|
||||
Debug.Print "fibo("; 0; ")="; f1
|
||||
Debug.Print "fibo("; 1; ")="; f2
|
||||
For i = 2 To n
|
||||
f3 = f1 + f2
|
||||
Debug.Print "fibo("; i; ")="; f3
|
||||
f1 = f2
|
||||
f2 = f3
|
||||
Next i
|
||||
End Sub 'fibonacci
|
||||
Loading…
Add table
Add a link
Reference in a new issue