This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,12 @@
USING: kernel math ;
IN: rosettacode.fibonacci.ar
: fib ( n -- m )
dup 0 < [ "fib of negative" throw ] when
[
! If n < 2, then drop q, else find q(n - 1) + q(n - 2).
[ dup 2 < ] dip swap [ drop ] [
[ [ 1 - ] dip dup call ]
[ [ 2 - ] dip dup call ] 2bi +
] if
] dup call( n q -- m ) ;

View file

@ -0,0 +1,25 @@
function fib(x)
if x < 0
raise ParamError(description|"Negative argument invalid", extra|"Fibbonacci sequence is undefined for negative numbers")
else
return (function(y)
if y == 0
return 0
elif y == 1
return 1
else
return fself(y-1) + fself(y-2)
end
end)(x)
end
end
try
>fib(2)
>fib(3)
>fib(4)
>fib(-1)
catch in e
> e
end

View file

@ -0,0 +1,4 @@
def fib = {
assert it > -1
{i -> i < 2 ? i : {j -> owner.call(j)}(i-1) + {k -> owner.call(k)}(i-2)}(it)
}

View file

@ -0,0 +1,9 @@
def fib0to20 = (0..20).collect(fib)
println fib0to20
try {
println fib(-25)
} catch (Throwable e) {
println "KABOOM!!"
println e.message
}

View file

@ -0,0 +1,24 @@
procedure main(A)
every write("fib(",a := numeric(!A),")=",fib(a))
end
procedure fib(n)
local source, i
static cache
initial {
cache := table()
cache[0] := 0
cache[1] := 1
}
if type(n) == "integer" & n >= 0 then
return n @ makeProc {{
i := @(source := &source) # 1
/cache[i] := ((i-1)@makeProc(^&current)+(i-2)@makeProc(^&current)) # 2
cache[i] @ source # 3
}}
end
procedure makeProc(A)
A := if type(A) == "list" then A[1]
return (@A, A) # prime and return
end

View file

@ -0,0 +1,9 @@
procedure fib(n)
local source, i
if type(n) == "integer" & n >= 0 then
return n @ makeProc {{
i := @(source := &source)
if i = (0|1) then i@source
((i-1)@makeProc(^&current) + (i-2)@makeProc(^&current)) @ source
}}
end

View file

@ -0,0 +1 @@
fibN=: (-&2 +&$: -&1)^:(1&<) M."0

View file

@ -0,0 +1,4 @@
fibN 12
144
fibN i.31
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

View file

@ -0,0 +1 @@
fib: {:[x<0; "Error Negative Number"; {:[x<2;x;_f[x-2]+_f[x-1]]}x]}

View file

@ -0,0 +1,4 @@
fib'!10
0 1 1 2 3 5 8 13 21 34
fib -1
"Error Negative Number"

View file

@ -0,0 +1,32 @@
HAI 1.3
HOW IZ I fib YR x
DIFFRINT x AN BIGGR OF x AN 0, O RLY?
YA RLY, FOUND YR "ERROR"
OIC
HOW IZ I fib_i YR n
DIFFRINT n AN BIGGR OF n AN 2, O RLY?
YA RLY, FOUND YR n
OIC
FOUND YR SUM OF...
I IZ fib_i YR DIFF OF n AN 2 MKAY AN...
I IZ fib_i YR DIFF OF n AN 1 MKAY
IF U SAY SO
FOUND YR I IZ fib_i YR x MKAY
IF U SAY SO
HOW IZ I fib_i YR n
VISIBLE "SRY U CANT HAS FIBS DIS TIEM"
IF U SAY SO
IM IN YR fibber UPPIN YR i TIL BOTH SAEM i AN 5
I HAS A i ITZ DIFF OF i AN 1
VISIBLE "fib(:{i}) = " I IZ fib YR i MKAY
IM OUTTA YR fibber
I IZ fib_i YR 3 MKAY
KTHXBYE

View file

@ -0,0 +1,13 @@
Fib := proc( n :: nonnegint )
proc( k )
option remember; # automatically memoise
if k = 0 then
0
elif k = 1 then
1
else
# Recurse, anonymously
thisproc( k - 1 ) + thisproc( k - 2 )
end
end( n )
end proc:

View file

@ -0,0 +1,6 @@
> seq( Fib( i ), i = 0 .. 10 );
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
> Fib( -1 );
Error, invalid input: Fib expects its 1st argument, n, to be of type
nonnegint, but received -1

View file

@ -0,0 +1,5 @@
check := #<0&
fib := If[check[#],Throw["Negative Argument"],If[#<=1,1,#0[#-2]+#0[#-1]]&[#]]&
fib /@ Range[0,10]
{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89}

View file

@ -0,0 +1,9 @@
check := (Print[#];#<0)&
fib /@ Range[0,4]
0
1
2
3
4
{1, 1, 2, 3, 5}