Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,8 @@
: fibon \ n -- fib(n)
>r 0 1
( tuck n:+ ) \ fib(n-2) fib(n-1) -- fib(n-1) fib(n)
r> n:1- times ;
: fib \ n -- fib(n)
dup 1 n:= if 1 ;; then
fibon nip ;

View file

@ -0,0 +1,4 @@
/Sequence
fib:{<0;1> {x,<x[-1]+x[-2]>}/ range[x]}
/nth
fibn:{fib[x][x]}

View file

@ -0,0 +1,43 @@
/*
author: snugsfbay
date: March 3, 2016
description: Create a list of x numbers in the Fibonacci sequence.
- user may specify the length of the list
- enforces a minimum of 2 numbers in the sequence because any fewer is not a sequence
- enforces a maximum of 47 because further values are too large for integer data type
- Fibonacci sequence always starts with 0 and 1 by definition
*/
public class FibNumbers{
final static Integer MIN = 2; //minimum length of sequence
final static Integer MAX = 47; //maximum length of sequence
/*
description: method to create a list of numbers in the Fibonacci sequence
param: user specified integer representing length of sequence should be 2-47, inclusive.
- Sequence starts with 0 and 1 by definition so the minimum length could be as low as 2.
- For 48th number in sequence or greater, code would require a Long data type rather than an Integer.
return: list of integers in sequence.
*/
public static List<Integer> makeSeq(Integer len){
List<Integer> fib = new List<Integer>{0,1}; // initialize list with first two values
Integer i;
if(len<MIN || len==null || len>MAX) {
if (len>MAX){
len=MAX; //set length to maximum if user entered too high a value
}else{
len=MIN; //set length to minimum if user entered too low a value or none
}
} //This could be refactored using teneray operator, but we want code coverage to be reflected for each condition
//start with initial list size to find previous two values in the sequence, continue incrementing until list reaches user defined length
for(i=fib.size(); i<len; i++){
fib.add(fib[i-1]+fib[i-2]); //create new number based on previous numbers and add that to the list
}
return fib;
}
}

View file

@ -0,0 +1,11 @@
Lbl FIB
r₁→N
0→I
1→J
For(K,1,N)
I+J→T
J→I
T→J
End
J
Return

View file

@ -0,0 +1,43 @@
// Fibonacci sequence, recursive version
fun fibb
loop
a = funparam[0]
break (a < 2)
a--
// Save "a" while calling fibb
a -> stack
// Set the parameter and call fibb
funparam[0] = a
call fibb
// Handle the return value and restore "a"
b = funparam[0]
stack -> a
// Save "b" while calling fibb again
b -> stack
a--
// Set the parameter and call fibb
funparam[0] = a
call fibb
// Handle the return value and restore "b"
c = funparam[0]
stack -> b
// Sum the results
b += c
a = b
funparam[0] = a
break
end
end
// vim: set syntax=c ts=4 sw=4 et:

View file

@ -0,0 +1,22 @@
loop: LDA y ; higher No.
STA temp
ADD x ; lower No.
STA y
LDA temp
STA x
LDA count
SUB one
BRZ done
STA count
JMP loop
done: LDA y
STP
one: 1
count: 8 ; n = 10
x: 1
y: 1
temp: 0

View file

@ -0,0 +1,29 @@
//Calculates Fibonacci sequence up to n steps using Binet's closed form solution
FibFunction(UNSIGNED2 n) := FUNCTION
REAL Sqrt5 := Sqrt(5);
REAL Phi := (1+Sqrt(5))/2;
REAL Phi_Inv := 1/Phi;
UNSIGNED FibValue := ROUND( ( POWER(Phi,n)-POWER(Phi_Inv,n) ) /Sqrt5);
RETURN FibValue;
END;
FibSeries(UNSIGNED2 n) := FUNCTION
Fib_Layout := RECORD
UNSIGNED5 FibNum;
UNSIGNED5 FibValue;
END;
FibSeq := DATASET(n+1,
TRANSFORM
( Fib_Layout
, SELF.FibNum := COUNTER-1
, SELF.FibValue := IF(SELF.FibNum<2,SELF.FibNum, FibFunction(SELF.FibNum) )
)
);
RETURN FibSeq;
END; }

View file

@ -0,0 +1,52 @@
[ Fibonacci sequence
==================
A program for the EDSAC
Calculates the nth Fibonacci
number and displays it at the
top of storage tank 3
The default value of n is 10
To calculate other Fibonacci
numbers, set the starting value
of the count to n-2
Works with Initial Orders 2 ]
T56K [ set load point ]
GK [ set theta ]
[ Orders ]
[ 0 ] T20@ [ a = 0 ]
A17@ [ a += y ]
U18@ [ temp = a ]
A16@ [ a += x ]
T17@ [ y = a; a = 0 ]
A18@ [ a += temp ]
T16@ [ x = a; a = 0 ]
A19@ [ a = count ]
S15@ [ a -= 1 ]
U19@ [ count = a ]
E@ [ if a>=0 go to θ ]
T20@ [ a = 0 ]
A17@ [ a += y ]
T96F [ C(96) = a; a = 0]
ZF [ halt ]
[ Data ]
[ 15 ] P0D [ const: 1 ]
[ 16 ] P0F [ var: x = 0 ]
[ 17 ] P0D [ var: y = 1 ]
[ 18 ] P0F [ var: temp = 0 ]
[ 19 ] P4F [ var: count = 8 ]
[ 20 ] P0F [ used to clear a ]
EZPF [ begin execution ]

View file

@ -0,0 +1,27 @@
!-------------------------------------------
! derived from my book "PROGRAMMARE IN ERRE"
! iterative solution
!-------------------------------------------
PROGRAM FIBONACCI
!$DOUBLE
!VAR F1#,F2#,TEMP#,COUNT%,N%
BEGIN !main
INPUT("Number",N%)
F1=0
F2=1
REPEAT
TEMP=F2
F2=F1+F2
F1=TEMP
COUNT%=COUNT%+1
UNTIL COUNT%=N%
PRINT("FIB(";N%;")=";F2)
! Obviously a FOR loop or a WHILE loop can
! be used to solve this problem
END PROGRAM

View file

@ -0,0 +1,8 @@
(define (fib n)
(if (< n 2) n
(+ (fib (- n 2)) (fib (1- n)))))
(remember 'fib #(0 1))
(for ((i 12)) (write (fib i)))
0 1 1 2 3 5 8 13 21 34 55 89

View file

@ -0,0 +1,5 @@
fibonacci : Int -> Int
fibonacci n = if n < 2 then
n
else
fibonacci(n - 2) + fibonacci(n - 1)

View file

@ -0,0 +1,11 @@
01.10 TYPE "FIBONACCI NUMBERS" !
01.20 ASK "N =", N
01.30 SET A=0
01.40 SET B=1
01.50 FOR I=2,N; DO 2.0
01.60 TYPE "F(N) ", %8, B, !
01.70 QUIT
02.10 SET T=B
02.20 SET B=A+B
02.30 SET A=T

View file

@ -0,0 +1,23 @@
FIBONACCI PUSH R1
PUSH R2
PUSH R3
MOVE 0, R1
MOVE 1, R2
FIB_LOOP SUB R0, 1, R0
JP_Z FIB_DONE
MOVE R2, R3
ADD R1, R2, R2
MOVE R3, R1
JP FIB_LOOP
FIB_DONE MOVE R2, R0
POP R3
POP R2
POP R1
RET

View file

@ -0,0 +1,83 @@
'Fibonacci extended
'Freebasic version 24 Windows
Dim Shared ADDQmod(0 To 19) As Ubyte
Dim Shared ADDbool(0 To 19) As Ubyte
For z As Integer=0 To 19
ADDQmod(z)=(z Mod 10+48)
ADDbool(z)=(-(10<=z))
Next z
Function plusINT(NUM1 As String,NUM2 As String) As String
Dim As Byte flag
#macro finish()
three=Ltrim(three,"0")
If three="" Then Return "0"
If flag=1 Then Swap NUM2,NUM1
Return three
Exit Function
#endmacro
var lenf=Len(NUM1)
var lens=Len(NUM2)
If lens>lenf Then
Swap NUM2,NUM1
Swap lens,lenf
flag=1
End If
var diff=lenf-lens-Sgn(lenf-lens)
var three="0"+NUM1
var two=String(lenf-lens,"0")+NUM2
Dim As Integer n2
Dim As Ubyte addup,addcarry
addcarry=0
For n2=lenf-1 To diff Step -1
addup=two[n2]+NUM1[n2]-96
three[n2+1]=addQmod(addup+addcarry)
addcarry=addbool(addup+addcarry)
Next n2
If addcarry=0 Then
finish()
End If
If n2=-1 Then
three[0]=addcarry+48
finish()
End If
For n2=n2 To 0 Step -1
addup=two[n2]+NUM1[n2]-96
three[n2+1]=addQmod(addup+addcarry)
addcarry=addbool(addup+addcarry)
Next n2
three[0]=addcarry+48
finish()
End Function
Function fibonacci(n As Integer) As String
Dim As String sl,l,term
sl="0": l="1"
If n=1 Then Return "0"
If n=2 Then Return "1"
n=n-2
For x As Integer= 1 To n
term=plusINT(l,sl)
sl=l
l=term
Next x
Function =term
End Function
'============== EXAMPLE ===============
print "THE SEQUENCE TO 10:"
print
For n As Integer=1 To 10
Print "term";n;": "; fibonacci(n)
Next n
print
print "Selected Fibonacci number"
print "Fibonacci 500"
print
print fibonacci(500)
Sleep

View file

@ -0,0 +1,4 @@
def
fib( 0 ) = 0
fib( 1 ) = 1
fib( n ) = fib( n - 1 ) + fib( n - 2 )

View file

@ -0,0 +1,7 @@
def fib( n ) =
def
_fib( 0, prev, _ ) = prev
_fib( 1, _, next ) = next
_fib( n, prev, next ) = _fib( n - 1, next, next + prev )
_fib( n, 0, 1 )

View file

@ -0,0 +1,6 @@
val fib =
def _fib( a, b ) = a # _fib( b, a + b )
_fib( 0, 1 )
println( fib(10000) )

View file

@ -0,0 +1,7 @@
def fib( n ) =
a, b = 0, 1
for i <- 1..n
a, b = b, a+b
a

View file

@ -0,0 +1,5 @@
import math.sqrt
def fib( n ) =
phi = (1 + sqrt( 5 ))/2
int( (phi^n - (-phi)^-n)/sqrt(5) + .5 )

View file

@ -0,0 +1,19 @@
def mul( a, b ) =
res = array( a.length(), b(0).length() )
for i <- 0:a.length(), j <- 0:b(0).length()
res( i, j ) = sum( a(i, k)*b(k, j) | k <- 0:b.length() )
vector( res )
def
pow( _, 0 ) = ((1, 0), (0, 1))
pow( x, 1 ) = x
pow( x, n )
| 2|n = pow( mul(x, x), n\2 )
| otherwise = mul(x, pow( mul(x, x), (n - 1)\2 ) )
def fib( n ) = pow( ((0, 1), (1, 1)), n )(0, 1)
for i <- 0..10
println( fib(i) )

View file

@ -0,0 +1,4 @@
fun main(n: int): int =
loop((a,b) = (0,1)) = for _i < n do
(b, a + b)
in a

View file

@ -0,0 +1,30 @@
include "Tlbx Timer.incl"
include "ConsoleWindow"
local fn Fibonacci( n as long ) as Long
begin globals
dim as long s1, s2// static
end globals
dim as long temp
if ( n < 2 )
s1 = n
exit fn
else
temp = s1 + s2
s2 = s1
s1 = temp
exit fn
end if
end fn = s1
dim as long i
dim as UnsignedWide start, finish
Microseconds( @start )
for i = 0 to 40
print i; ". "; fn Fibonacci(i)
next i
Microseconds( @finish )
print "Compute time:"; (finish.lo - start.lo ) / 1000; " ms"

View file

@ -0,0 +1,37 @@
'
' Compute nth Fibonacci number
'
' open a window for display
OPENW 1
CLEARW 1
' Display some fibonacci numbers
' Fib(46) is the largest number GFA Basic can reach
' (long integers are 4 bytes)
FOR i%=0 TO 46
PRINT "fib(";i%;")=";@fib(i%)
NEXT i%
' wait for a key press and tidy up
~INP(2)
CLOSEW 1
'
' Function to compute nth fibonacci number
' n must be in range 0 to 46, inclusive
'
FUNCTION fib(n%)
LOCAL n0%,n1%,nn%,i%
n0%=0
n1%=1
SELECT n%
CASE 0
RETURN n0%
CASE 1
RETURN n1%
DEFAULT
FOR i%=2 TO n%
nn%=n0%+n1%
n0%=n1%
n1%=nn%
NEXT i%
RETURN nn%
ENDSELECT
ENDFUNC

View file

@ -0,0 +1,3 @@
#include "harbour.ch"
Function fibb(a,b,n)
return(if(--n>0,fibb(b,a+b,n),a))

View file

@ -0,0 +1,9 @@
#include "harbour.ch"
Function fibb(n)
local fnow:=0, fnext:=1, tempf
while (--n>0)
tempf:=fnow+fnext
fnow:=fnext
fnext:=tempf
end while
return(fnext)

View file

@ -0,0 +1,4 @@
(defn fib [n]
(if (< n 2)
n
(+ (fib (- n 2)) (fib (- n 1)))))

View file

@ -0,0 +1,5 @@
fibAnalytic : Nat -> Double
fibAnalytic n =
floor $ ((pow goldenRatio n) - (pow (-1.0/goldenRatio) n)) / sqrt(5)
where goldenRatio : Double
goldenRatio = (1.0 + sqrt(5)) / 2.0

View file

@ -0,0 +1,4 @@
fibRecursive : Nat -> Nat
fibRecursive Z = Z
fibRecursive (S Z) = (S Z)
fibRecursive (S (S n)) = fibRecursive (S n) + fibRecursive n

View file

@ -0,0 +1,5 @@
fibIterative : Nat -> Nat
fibIterative n = fibIterative' n Z (S Z)
where fibIterative' : Nat -> Nat -> Nat -> Nat
fibIterative' Z a _ = a
fibIterative' (S n) a b = fibIterative' n b (a + b)

View file

@ -0,0 +1,5 @@
fibLazy : Lazy (List Nat)
fibLazy = 0 :: 1 :: zipWith (+) fibLazy (
case fibLazy of
(x::xs) => xs
[] => [])

View file

@ -0,0 +1,2 @@
(defn int fib (int n) (return (? (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))
(main (prn (fib 30)))

View file

@ -0,0 +1,6 @@
(defun fib
((0) 0)
((1) 1)
((n)
(+ (fib (- n 1))
(fib (- n 2)))))

View file

@ -0,0 +1,9 @@
(defun fib
((n) (when (>= n 0))
(fib n 0 1)))
(defun fib
((0 result _)
result)
((n result next)
(fib (- n 1) next (+ result next))))

View file

@ -0,0 +1,23 @@
define fibonacci(n::integer) => {
#n < 1 ? return false
local(
swap = 0,
n1 = 0,
n2 = 1
)
loop(#n) => {
#swap = #n1 + #n2;
#n2 = #n1;
#n1 = #swap;
}
return #n1
}
fibonacci(0) //->output false
fibonacci(1) //->output 1
fibonacci(2) //->output 1
fibonacci(3) //->output 2

View file

@ -0,0 +1,4 @@
on fib (n)
if n<2 then return n
return fib(n-1)+fib(n-2)
end

View file

@ -0,0 +1,11 @@
on fib (n)
if n<2 then return n
fibPrev = 0
fib = 1
repeat with i = 2 to n
tmp = fib
fib = fib + fibPrev
fibPrev = tmp
end repeat
return fib
end

View file

@ -0,0 +1,6 @@
on fib (n)
sqrt5 = sqrt(5.0)
p = (1+sqrt5)/2
q = 1 - p
return integer((power(p,n)-power(q,n))/sqrt5)
end

View file

@ -0,0 +1,20 @@
-- Iterative, translation of the basic version.
function fibi n
put 0 into aa
put 1 into b
repeat with i = 1 to n
put aa + b into temp
put b into aa
put temp into b
end repeat
return aa
end fibi
-- Recursive
function fibr n
if n <= 1 then
return n
else
return fibr(n-1) + fibr(n-2)
end if
end fibr

View file

@ -0,0 +1,9 @@
function fib(x: int): int = (
let cache = {} in
let fibc x = if x<=1 then x else (
if x not in cache then
cache[x] = fibc(x-1) + fibc(x-2);
cache[x]
) in fibc(x)
);;
for x in range(10) do print(fib(x))

View file

@ -0,0 +1,12 @@
# Main
Lei ha clacsonato
voglio un nonnulla, Necchi mi porga un nonnulla
il nonnulla come se fosse brematurata la supercazzola bonaccia con il nonnulla o scherziamo?
un nonnulla a posterdati
# Fibonacci function 'bonaccia'
blinda la supercazzola Necchi bonaccia con antani Necchi o scherziamo? che cos'è l'antani?
minore di 3: vaffanzum 1! o tarapia tapioco: voglio unchiamo, Necchi come se fosse brematurata
la supercazzola bonaccia con antani meno 1 o scherziamo? voglio duechiamo,
Necchi come se fosse brematurata la supercazzola bonaccia con antani meno 2 o scherziamo? vaffanzum
unchiamo più duechiamo! e velocità di esecuzione

View file

@ -0,0 +1 @@
function fib(n) = if n < 2 then n else fib(n - 2) + fib(n - 1);

View file

@ -0,0 +1,11 @@
F fib(n:Int) {
n < 2 returns n
local a = 1, b = 1
# i is automatically local because of for()
for(i=2; i<n; i=i+1) {
local next = a + b
a = b
b = next
}
b
}

View file

@ -0,0 +1,5 @@
proc Fibonacci(n: int): int64 =
var fn = float64(n)
var p: float64 = (1.0 + sqrt(5.0)) / 2.0
var q: float64 = 1.0 / p
return int64((pow(p, fn) + pow(q, fn)) / sqrt(5.0))

View file

@ -0,0 +1,10 @@
proc Fibonacci(n: int): int =
var
first = 0
second = 1
for i in 0 .. <n:
swap first, second
second += first
result = first

View file

@ -0,0 +1,5 @@
proc Fibonacci(n: int): int64 =
if n <= 2:
result = 1
else:
result = Fibonacci(n - 1) + Fibonacci(n - 2)

View file

@ -0,0 +1,7 @@
proc Fibonacci(n: int, current: int64, next: int64): int64 =
if n == 0:
result = current
else:
result = Fibonacci(n - 1, next, current + next)
proc Fibonacci(n: int): int64 =
result = Fibonacci(n, 0, 1)

View file

@ -0,0 +1,11 @@
iterator fib: int {.closure.} =
var a = 0
var b = 1
while true:
yield a
swap a, b
b = a + b
var f = fib
for i in 0.. <10:
echo f()

View file

@ -0,0 +1 @@
: fib 0 1 rot #[ tuck + ] times drop ;

View file

@ -0,0 +1,18 @@
sequence fcache = {1,1}
function fibonamem(integer n) -- memoized, works for -ve numbers, inaccurate above 78
integer absn = abs(n)
if n=0 then return 0 end if
if absn>length(fcache) then
fcache = append(fcache,fibonamem(absn-1)+fibonamem(absn-2))
if absn!=length(fcache) then ?9/0 end if
end if
if n<0 and remainder(n,2)=0 then return -fcache[absn] end if
return fcache[absn]
end function
for i=0 to 30 do
printf(1,"%d", fibonamem(i))
if i!=30 then puts(1,", ") end if
end for
puts(1,"\n")

View file

@ -0,0 +1,22 @@
include builtins\bigatom.e
sequence fcacheba = {BA_ONE,BA_ONE}
function fibonamemba(integer n) -- memoized, works for -ve numbers, yields bigatom
integer absn = abs(n)
if n=0 then return BA_ZERO end if
if absn>length(fcacheba) then
fcacheba = append(fcacheba,ba_add(fibonamemba(absn-1),fibonamemba(absn-2)))
if absn!=length(fcacheba) then ?9/0 end if
end if
if n<0 and remainder(n,2)=0 then return ba_sub(0,fcacheba[absn]) end if
return fcacheba[absn]
end function
for i=0 to 30 do
ba_printf(1,"%B", fibonamemba(i))
if i!=30 then puts(1,", ") end if
end for
puts(1,"\n")
ba_printf(1,"%B", fibonamemba(777))
puts(1,"\n")

View file

@ -0,0 +1,5 @@
recursive = (n):
if (n <= 1): 1. else: recursive (n - 1) + recursive (n - 2)..
n = 40
("fib(", n, ")= ", recursive (n), "\n") join print

View file

@ -0,0 +1,11 @@
iterative = (n) :
curr = 0
prev = 1
tmp = 0
n times:
tmp = curr
curr = curr + prev
prev = tmp
.
curr
.

View file

@ -0,0 +1,20 @@
sqr = (x): x * x.
# Based on the fact that
# F2n = Fn(2Fn+1 - Fn)
# F2n+1 = Fn ^2 + Fn+1 ^2
matrix = (n) :
algorithm = (n) :
"computes (Fn, Fn+1)"
if (n < 2): return ((0, 1), (1, 1)) at(n).
# n = e + {0, 1}
q = algorithm(n / 2) # q = (Fe/2, Fe/2+1)
q = (q(0) * (2 * q(1) - q(0)), sqr(q(0)) + sqr(q(1))) # q => (Fe, Fe+1)
if (n % 2 == 1) : # q => (Fe+{0, 1}, Fe+1+{0,1}) = (Fn, Fn+1)
q = (q(1), q(1) + q(0))
.
q
.
algorithm(n)(0)
.

View file

@ -0,0 +1,13 @@
fibonacci = (n) :
myFavorite = matrix
if (n >= 0) :
myFavorite(n)
. else :
n = n * -1
if (n % 2 == 1) :
myFavorite(n)
. else :
myFavorite(n) * -1
.
.
.

View file

@ -0,0 +1,45 @@
class FibonacciSequence
**Prints the nth fibonacci number**
on start
args := program arguments
if args empty
print .fibonacci(8)
else
try
print .fibonacci(integer.parse(args[0]))
catch FormatException
print to Console.error made !, "Input must be an integer"
exit program with error code
catch OverflowException
print to Console.error made !, "Number too large"
exit program with error code
define fibonacci(n as integer) as integer is shared
**Returns the nth fibonacci number**
test
assert fibonacci(0) = 0
assert fibonacci(1) = 1
assert fibonacci(2) = 1
assert fibonacci(3) = 2
assert fibonacci(4) = 3
assert fibonacci(5) = 5
assert fibonacci(6) = 8
assert fibonacci(7) = 13
assert fibonacci(8) = 21
body
a, b := 0, 1
for n
a, b := b, a + b
return a

View file

@ -0,0 +1,7 @@
give n
x = fib(n)
see n + " Fibonacci is : " + x
func fib nr if nr = 0 return 0 ok
if nr = 1 return 1 ok
if nr > 1 return fib(nr-1) + fib(nr-2) ok

View file

@ -0,0 +1,26 @@
10101000000000100000000000000000 0. -21 to c acc = -n
01101000000001100000000000000000 1. c to 22 temp = acc
00101000000001010000000000000000 2. Sub. 20 acc -= m
10101000000001100000000000000000 3. c to 21 n = acc
10101000000000100000000000000000 4. -21 to c acc = -n
10101000000001100000000000000000 5. c to 21 n = acc
01101000000000100000000000000000 6. -22 to c acc = -temp
00101000000001100000000000000000 7. c to 20 m = acc
11101000000000100000000000000000 8. -23 to c acc = -count
00011000000001010000000000000000 9. Sub. 24 acc -= -1
00000000000000110000000000000000 10. Test skip next if acc<0
10011000000000000000000000000000 11. 25 to CI goto (15 + 1)
11101000000001100000000000000000 12. c to 23 count = acc
11101000000000100000000000000000 13. -23 to c acc = -count
11101000000001100000000000000000 14. c to 23 count = acc
00011000000000000000000000000000 15. 24 to CI goto (-1 + 1)
10101000000000100000000000000000 16. -21 to c acc = -n
10101000000001100000000000000000 17. c to 21 n = acc
10101000000000100000000000000000 18. -21 to c acc = -n
00000000000001110000000000000000 19. Stop
00000000000000000000000000000000 20. 0 var m = 0
10000000000000000000000000000000 21. 1 var n = 1
00000000000000000000000000000000 22. 0 var temp = 0
10010000000000000000000000000000 23. 9 var count = 9
11111111111111111111111111111111 24. -1 const -1
11110000000000000000000000000000 25. 15 const 15

View file

@ -0,0 +1,4 @@
fibonacci(n) :=
n when n < 2
else
fibonacci(n - 1) + fibonacci(n - 2);

View file

@ -0,0 +1,8 @@
fibonacci(n) := fibonacciHelper(0, 1, n);
fibonacciHelper(prev, next, n) :=
prev when n < 1
else
next when n = 1
else
fibonacciHelper(next, next + prev, n - 1);

View file

@ -0,0 +1,11 @@
fibonacci(n) := fibonacciHelper([[1,0],[0,1]], n);
fibonacciHelper(M(2), n) :=
let
N := [[1,1],[1,0]];
in
M[1,1] when n <= 1
else
fibonacciHelper(matmul(M, N), n - 1);
matmul(A(2), B(2)) [i,j] := sum( A[i,all] * B[all,j] );

View file

@ -0,0 +1,6 @@
(define fib
0 -> 0
1 -> 1
N -> (+ (fib (+ N 1)) (fib (+ N 2)))
where (< N 0)
N -> (+ (fib (- N 1)) (fib (- N 2))))

View file

@ -0,0 +1,7 @@
func fib_iter(n) {
var fib = [1, 1];
(n - fib.len).times {
fib = [fib[-1], fib[-2] + fib[-1]]
};
fib[-1];
}

View file

@ -0,0 +1,3 @@
func fib_rec(n) {
n < 2 ? n : (__FUNC__(n-1) + __FUNC__(n-2));
}

View file

@ -0,0 +1,3 @@
func fib_mem (n) is cached {
n < 2 ? n : (__FUNC__(n-1) + __FUNC__(n-2));
}

View file

@ -0,0 +1,5 @@
func fib_closed(n) {
define S = (1.25.sqrt + 0.5);
define T = (-S + 1);
(S**n - T**n) / (-T + S) -> roundf(0);
}

View file

@ -0,0 +1 @@
42.fibonacci

View file

@ -0,0 +1,5 @@
// Assuming code is in Integer.fibonacci() method
() Integer
[
if this < 2 [this] else [[this - 1].fibonacci + [this - 2].fibonacci]
]

View file

@ -0,0 +1,5 @@
// Assuming in fibonacci(n) procedure
(Integer n) Integer
[
if n < 2 [n] else [fibonacci(n - 1) + fibonacci(n - 2)]
]

View file

@ -0,0 +1,19 @@
// Assuming code is in Integer.fibonacci() method
() Integer
[
if this < 2
[this]
else
[
!prev: 1
!next: 1
2.to_pre this
[
!sum : prev + next
prev := next
next := sum
]
next
]
]

View file

@ -0,0 +1,24 @@
// Bind : is faster than assignment :=
// loop is faster than to_pre (which uses a closure)
() Integer
[
if this < 2
[this]
else
[
!prev: 1
!next: 1
!sum
!count: this - 2
loop
[
if count = 0 [exit]
count--
sum : prev + next
prev : next
next : sum
]
next
]
]

View file

@ -0,0 +1,12 @@
import Cocoa
func fibonacci(n: Int) -> Int {
let square_root_of_5 = sqrt(5.0)
let p = (1 + square_root_of_5) / 2
let q = 1 / p
return Int((pow(p,CDouble(n)) + pow(q,CDouble(n))) / square_root_of_5 + 0.5)
}
for i in 1...30 {
println(fibonacci(i))
}

View file

@ -0,0 +1,11 @@
func fibonacci(n: Int) -> Int {
if n < 2 {
return n
}
var fibPrev = 1
var fib = 1
for num in 2...n {
(fibPrev, fib) = (fib, fib + fibPrev)
}
return fib
}

View file

@ -0,0 +1,9 @@
func fibonacci() -> SequenceOf<UInt> {
return SequenceOf {() -> GeneratorOf<UInt> in
var window: (UInt, UInt, UInt) = (0, 0, 1)
return GeneratorOf {
window = (window.1, window.2, window.1 + window.2)
return window.0
}
}
}

View file

@ -0,0 +1,9 @@
func fibonacci(n: Int) -> Int {
if n < 2 {
return n
} else {
return fibonacci(n-1) + fibonacci(n-2)
}
}
println(fibonacci(30))

View file

@ -0,0 +1,12 @@
def fibIter (int n)
if (< n 2)
return n
end if
decl int fib fibPrev num
set fib (set fibPrev 1)
for (set num 2) (< num n) (inc num)
set fib (+ fib fibPrev)
set fibPrev (- fib fibPrev)
end for
return fib
end

View file

@ -0,0 +1,4 @@
def (fib n)
if (n < 2)
n
(+ (fib n-1) (fib n-2))

View file

@ -0,0 +1,5 @@
def (fib n)
(+ (fib n-1) (fib n-2))
def (fib n) :case (n < 2)
n

View file

@ -0,0 +1,6 @@
def (fib n saved)
# all args in wart are optional, and we expect callers to not provide saved
default saved :to (table 0 0 1 1) # pre-populate base cases
default saved.n :to
(+ (fib n-1 saved) (fib n-2 saved))
saved.n

View file

@ -0,0 +1,2 @@
(DEFUN FIBONACCI (N)
(FLOOR (+ (/ (EXPT (/ (+ (SQRT 5) 1) 2) N) (SQRT 5)) 0.5)))

View file

@ -0,0 +1,5 @@
(DEFUN RANGE (X Y)
(IF (<= X Y)
(CONS X (RANGE (+ X 1) Y))))
(PRINT (MAPCAR FIBONACCI (RANGE 1 50)))

View file

@ -0,0 +1,4 @@
def nth_fib_naive(n):
if (n < 2) then n
else nth_fib_naive(n - 1) + nth_fib_naive(n - 2)
end;

View file

@ -0,0 +1,8 @@
def nth_fib(n):
# input: [f(i-2), f(i-1), countdown]
def fib: (.[0] + .[1]) as $sum
| .[2] as $n
| if ($n <= 0) then $sum
else [ .[1], $sum, $n - 1 ]
| fib end;
[-1, 1, n] | fib;

View file

@ -0,0 +1 @@
(range(0;5), 50) | [., nth_fib(.)]

View file

@ -0,0 +1,6 @@
[0,0]
[1,1]
[2,1]
[3,2]
[4,3]
[50,12586269025]

View file

@ -0,0 +1,7 @@
def fib_binet(n):
(5|sqrt) as $rt
| ((1 + $rt)/2) as $phi
| (($phi | log) * n | exp) as $phin
| (if 0 == (n % 2) then 1 else -1 end) as $sign
| ( ($phin - ($sign / $phin) ) / $rt ) + .5
| floor;

View file

@ -0,0 +1,8 @@
# Generator
def fibonacci(n):
# input: [f(i-2), f(i-1), countdown]
def fib: (.[0] + .[1]) as $sum
| if .[2] == 0 then $sum
else $sum, ([ .[1], $sum, .[2] - 1 ] | fib)
end;
[-1, 1, n] | fib;