langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,18 @@
using System;
using System.Console;
module Fibonacci
{
Fibonacci(x : long) : long
{
|x when x < 2 => x
|_ => Fibonacci(x - 1) + Fibonacci(x - 2)
}
Main() : void
{
def num = Int64.Parse(ReadLine());
foreach (n in $[0 .. num])
WriteLine("{0}: {1}", n, Fibonacci(n));
}
}

View file

@ -0,0 +1,13 @@
Fibonacci(x : long, current : long, next : long) : long
{
match(x)
{
|0 => current
|_ => Fibonacci(x - 1, next, current + next)
}
}
Fibonacci(x : long) : long
{
Fibonacci(x, 0, 1)
}

View file

@ -0,0 +1,37 @@
/* NetRexx */
options replace format comments java crossref savelog symbols
numeric digits 210000 /*prepare for some big 'uns. */
parse arg x y . /*allow a single number or range.*/
if x == '' then do /*no input? Then assume -30-->+30*/
x = -30
y = -x
end
if y == '' then y = x /*if only one number, show fib(n)*/
loop k = x to y /*process each Fibonacci request.*/
q = fib(k)
w = q.length /*if wider than 25 bytes, tell it*/
say 'Fibonacci' k"="q
if w > 25 then say 'Fibonacci' k "has a length of" w
end k
exit
/*-------------------------------------FIB subroutine (non-recursive)---*/
method fib(arg) private static
parse arg n
na = n.abs
if na < 2 then return na /*handle special cases. */
a = 0
b = 1
loop j = 2 to na
s = a + b
a = b
b = s
end j
if n > 0 | na // 2 == 1 then return s /*if positive or odd negative... */
else return -s /*return a negative Fib number. */

View file

@ -0,0 +1,4 @@
(define (fibonacci n)
(if (< n 2) 1
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
(print(fibonacci 10)) ;;89

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): int64 =
var first: int64 = 0
var second: int64 = 1
var t: int64 = 0
while n > 1:
t = first + second
first = second
second = t
dec(n)
result = second

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,11 @@
let fib_iter n =
if n < 2 then
n
else let fib_prev = ref 1
and fib = ref 1 in
for num = 2 to n - 1 do
let temp = !fib in
fib := !fib + !fib_prev;
fib_prev := temp
done;
!fib

View file

@ -0,0 +1,5 @@
let rec fib_rec n =
if n < 2 then
n
else
fib_rec (n - 1) + fib_rec (n - 2)

View file

@ -0,0 +1,7 @@
let fib n =
let rec fib_aux n a b =
match n with
| 0 -> a
| _ -> fib_aux (n-1) b (a+b)
in
fib_aux n 0 1

View file

@ -0,0 +1,9 @@
open Num
let fib =
let rec fib_aux f0 f1 = function
| 0 -> f0
| 1 -> f1
| n -> fib_aux f1 (f1 +/ f0) (n - 1)
in
fib_aux (num_of_int 0) (num_of_int 1)

View file

@ -0,0 +1,15 @@
open Num
let mul (a,b,c) (d,e,f) =
(a*/d +/ b*/e, a*/e +/ b*/f, b*/e +/ c*/f)
let rec pow a n =
if n=1 then a else
let b = pow a (n/2) in
if (n mod 2) = 0 then mul b b else mul a (mul b b)
let fib n =
let (_,y,_) = (pow (Int 1, Int 1, Int 0) n) in
string_of_num y
;;
Printf.printf "fib %d = %s\n" 300 (fib 300)

View file

@ -0,0 +1,14 @@
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
A=1 :B=1 :C=1
PRINT A,
DO
C=A+B
A=B
B=C
PRINT A,
UNTIL GET=1

View file

@ -0,0 +1,17 @@
bundle Default {
class Fib {
function : Main(args : String[]), Nil {
for(i := 0; i <= 10; i += 1;) {
Fib(i)->PrintLine();
};
}
function : native : Fib(n : Int), Int {
if(n < 2) {
return n;
};
return Fib(n-1) + Fib(n-2);
}
}
}

View file

@ -0,0 +1,10 @@
-(long)fibonacci:(int)position
{
long result = 0;
if (position < 2) {
result = position;
} else {
result = [self fibonacci:(position -1)] + [self fibonacci:(position -2)];
}
return result;
}

View file

@ -0,0 +1,9 @@
+(long)fibonacci:(int)index {
long beforeLast = 0, last = 1;
while (index > 0) {
last += beforeLast;
beforeLast = last - beforeLast;
--index;
}
return last;
}

View file

@ -0,0 +1,8 @@
% recursive
function fibo = recfibo(n)
if ( n < 2 )
fibo = n;
else
fibo = recfibo(n-1) + recfibo(n-2);
endif
endfunction

View file

@ -0,0 +1,16 @@
% iterative
function fibo = iterfibo(n)
if ( n < 2 )
fibo = n;
else
f = zeros(2,1);
f(1) = 0;
f(2) = 1;
for i = 2 : n
t = f(2);
f(2) = f(1) + f(2);
f(1) = t;
endfor
fibo = f(2);
endif
endfunction

View file

@ -0,0 +1,4 @@
% testing
for i = 0 : 20
printf("%d %d\n", iterfibo(i), recfibo(i));
endfor

View file

@ -0,0 +1,10 @@
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fib_rec \
ORDER_PP_FN(8fn(8N, \
8if(8less(8N, 2), \
8N, \
8add(8fib_rec(8sub(8N, 1)), \
8fib_rec(8sub(8N, 2))))))
ORDER_PP(8fib_rec(10))

View file

@ -0,0 +1,13 @@
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fib \
ORDER_PP_FN(8fn(8N, \
8fib_iter(8N, 0, 1)))
#define ORDER_PP_DEF_8fib_iter \
ORDER_PP_FN(8fn(8N, 8I, 8J, \
8if(8is_0(8N), \
8I, \
8fib_iter(8dec(8N), 8J, 8add(8I, 8J)))))
ORDER_PP(8to_lit(8fib(8nat(5,0,0))))

View file

@ -0,0 +1,25 @@
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fib_memo \
ORDER_PP_FN(8fn(8N, \
8tuple_at(0, 8fib_memo_inner(8N, 8seq))))
#define ORDER_PP_DEF_8fib_memo_inner \
ORDER_PP_FN(8fn(8N, 8M, \
8cond((8less(8N, 8seq_size(8M)), 8pair(8seq_at(8N, 8M), 8M)) \
(8equal(8N, 0), 8pair(0, 8seq(0))) \
(8equal(8N, 1), 8pair(1, 8seq(0, 1))) \
(8else, \
8lets((8S, 8fib_memo_inner(8sub(8N, 2), 8M)) \
(8T, 8fib_memo_inner(8dec(8N), 8tuple_at(1, 8S))) \
(8U, 8add(8tuple_at(0, 8S), 8tuple_at(0, 8T))), \
8pair(8U, \
8seq_append(8tuple_at(1, 8T), 8seq(8U))))))))
ORDER_PP(
8for_each_in_range(8fn(8N,
8print(8to_lit(8fib_memo(8N)) (,) 8space)),
1, 21)
)

View file

@ -0,0 +1,12 @@
fun{FibI N}
Temp = {NewCell 0}
A = {NewCell 0}
B = {NewCell 1}
in
for I in 1..N do
Temp := @A + @B
A := @B
B := @Temp
end
@A
end

View file

@ -0,0 +1,5 @@
fun{FibR N}
if N < 2 then N
else {FibR N-1} + {FibR N-2}
end
end

View file

@ -0,0 +1,11 @@
fun{Fib N}
fun{Loop N A B}
if N == 0 then
B
else
{Loop N-1 A+B A}
end
end
in
{Loop N 1 0}
end

View file

@ -0,0 +1,20 @@
declare
fun lazy {FiboSeq}
{LazyMap
{Iterate fun {$ [A B]} [B A+B] end [0 1]}
Head}
end
fun {Head A|_} A end
fun lazy {Iterate F I}
I|{Iterate F {F I}}
end
fun lazy {LazyMap Xs F}
case Xs of X|Xr then {F X}|{LazyMap Xr F}
[] nil then nil
end
end
in
{Show {List.take {FiboSeq} 8}}

View file

@ -0,0 +1 @@
fibonocci(n)

View file

@ -0,0 +1,11 @@
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
};

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

@ -0,0 +1 @@
([1,1;1,0]^n)[1,2]

View file

@ -0,0 +1 @@
fib(n)=my(phi=(1+sqrt(5))/2);round((phi^n-phi^-n)/sqrt(5))

View file

@ -0,0 +1 @@
fib(n)=round(((1+sqrt(5))/2)^n/sqrt(5))

View file

@ -0,0 +1 @@
fib(n)=imag(quadgen(5)^n)

View file

@ -0,0 +1 @@
fib(n)=my(phi=quadgen(5));(phi^n-(-1/phi)^n)/(2*phi-1)

View file

@ -0,0 +1 @@
fib(n)=polcoeff(x/(1-x-x^2)+O(x^(n+1)),n)

View file

@ -0,0 +1,18 @@
fib(n)={
if(n<=0,
if(n,(-1)^(n+1)*fib(n),0)
,
my(v=lucas(n-1));
(2*v[1]+v[2])/5
)
};
lucas(n)={
if (!n, return([2,1]));
my(v=lucas(n >> 1), z=v[1], t=v[2], pr=v[1]*v[2]);
n=n%4;
if(n%2,
if(n==3,[v[1]*v[2]+1,v[2]^2-2],[v[1]*v[2]-1,v[2]^2+2])
,
if(n,[v[1]^2+2,v[1]*v[2]+1],[v[1]^2-2,v[1]*v[2]-1])
)
};

View file

@ -0,0 +1,11 @@
fib(n)={
if(n<2,
if(n<0,
(-1)^(n+1)*fib(n)
,
n
)
,
fib(n-1)+fib(n)
)
};

View file

@ -0,0 +1,9 @@
/* Form the n-th Fibonacci number, n > 1. */
get list (n);
f1 = 0; f2, f3 = 1;
do i = 1 to n-2;
f3 = f1 + f2;
f1 = f2;
f2 = f3;
end;
put skip list (f3);

View file

@ -0,0 +1,24 @@
Create or replace Function fnu_fibonnaci(p_iNumber integer)
return integer
is
nuFib integer;
nuP integer;
nuQ integer;
Begin
if p_iNumber is not null then
if p_iNumber=0 then
nuFib:=0;
Elsif p_iNumber=1 then
nuFib:=1;
Else
nuP:=0;
nuQ:=1;
For nuI in 2..p_iNumber loop
nuFib:=nuP+nuQ;
nuP:=nuQ;
nuQ:=nuFib;
End loop;
End if;
End if;
return(nuFib);
End fnu_fibonnaci;

View file

@ -0,0 +1,8 @@
function fib(n: integer): integer;
begin
if (n = 0) or (n = 1)
then
fib := n
else
fib := fib(n-1) + fib(n-2)
end;

View file

@ -0,0 +1,14 @@
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;
end;

View file

@ -0,0 +1,6 @@
sub fib (Int $n --> Int) {
$n > 1 or return $n;
my ($prev, $this) = 0, 1;
($prev, $this) = $this, $this + $prev for 1 ..^ $n;
return $this;
}

View file

@ -0,0 +1,4 @@
proto fib (Int $n --> Int) {*}
multi fib (0) { 0 }
multi fib (1) { 1 }
multi fib ($n) { fib($n - 1) + fib($n - 2) }

View file

@ -0,0 +1,6 @@
sub fib (Int $n --> Int) {
constant φ1 = 1 / constant φ = (1 + sqrt 5)/2;
constant invsqrt5 = 1 / sqrt 5;
floor invsqrt5 * (φ**$n + φ1**$n);
}

View file

@ -0,0 +1 @@
my @fib := 0, 1, *+* ... *;

View file

@ -0,0 +1 @@
sub fib ($n) { @fib[$n] }

View file

@ -0,0 +1,2 @@
my @neg_fib := 0, 1, *-* ... *;
sub fib ($n) { $n >= 0 and @fib[$n] or @neg_fib[-$n]; }

View file

@ -0,0 +1,15 @@
int
fibIter(int n) {
int fibPrev, fib, i;
if (n < 2) {
return 1;
}
fibPrev = 0;
fib = 1;
for (i = 1; i < n; i++) {
int oldFib = fib;
fib += fibPrev;
fibPrev = oldFib;
}
return fib;
}

View file

@ -0,0 +1,7 @@
int
fibRec(int n) {
if (n < 2) {
return(1);
}
return( fib(n-2) + fib(n-1) );
}

View file

@ -0,0 +1,9 @@
define fib(x);
lvars a , b;
1 -> a;
1 -> b;
repeat x - 1 times
(a + b, b) -> (b, a);
endrepeat;
a;
enddefine;

View file

@ -0,0 +1,20 @@
%!PS
% We want the 'n'th fibonacci number
/n 13 def
% Prepare output canvas:
/Helvetica findfont 20 scalefont setfont
100 100 moveto
%define the function recursively:
/fib { dup
3 lt
{ pop 1 }
{ dup 1 sub fib exch 2 sub fib add }
ifelse
} def
(Fib\() show n (....) cvs show (\)=) show n fib (.....) cvs show
showpage

View file

@ -0,0 +1,48 @@
FUNCTION fibonacci (n AS LONG) AS QUAD
DIM u AS LONG, a AS LONG, L0 AS LONG, outP AS QUAD
STATIC fibNum() AS QUAD
u = UBOUND(fibNum)
a = ABS(n)
IF u < 1 THEN
REDIM fibNum(1)
fibNum(1) = 1
u = 1
END IF
SELECT CASE a
CASE 0 TO 92
IF a > u THEN
REDIM PRESERVE fibNum(a)
FOR L0 = u + 1 TO a
fibNum(L0) = fibNum(L0 - 1) + fibNum(L0 - 2)
IF 88 = L0 THEN fibNum(88) = fibNum(88) + 1
NEXT
END IF
IF n < 0 THEN
fibonacci = fibNum(a) * ((-1)^(a+1))
ELSE
fibonacci = fibNum(a)
END IF
CASE ELSE
'Even without the above-mentioned bug, we're still limited to
'F(+/-92), due to data type limits. (F(93) = &hA94F AD42 221F 2702)
ERROR 6
END SELECT
END FUNCTION
FUNCTION PBMAIN () AS LONG
DIM n AS LONG
#IF NOT %DEF(%PB_CC32)
OPEN "out.txt" FOR OUTPUT AS 1
#ENDIF
FOR n = -92 TO 92
#IF %DEF(%PB_CC32)
PRINT STR$(n); ": "; FORMAT$(fibonacci(n), "#")
#ELSE
PRINT #1, STR$(n) & ": " & FORMAT$(fibonacci(n), "#")
#ENDIF
NEXT
CLOSE
END FUNCTION

View file

@ -0,0 +1,26 @@
function fib ($n) {
if ($n -eq 0) { return 0 }
if ($n -eq 1) { return 1 }
$m = 1
if ($n -lt 0) {
if ($n % 2 -eq -1) {
$m = 1
} else {
$m = -1
}
$n = -$n
}
$a = 0
$b = 1
for ($i = 1; $i -lt $n; $i++) {
$c = $a + $b
$a = $b
$b = $c
}
return $m * $b
}

View file

@ -0,0 +1,8 @@
function fib($n) {
switch ($n) {
0 { return 0 }
1 { return 1 }
{ $_ -lt 0 } { return [Math]::Pow(-1, -$n + 1) * (fib (-$n)) }
default { return (fib ($n - 1)) + (fib ($n - 2)) }
}
}

View file

@ -0,0 +1,3 @@
fib n = loop 0 1 n with
loop a b n = if n==0 then a else loop b (a+b) (n-1);
end;

View file

@ -0,0 +1,3 @@
Macro Fibonacci (n)
Int((Pow(((1+Sqr(5))/2),n)-Pow(((1-Sqr(5))/2),n))/Sqr(5))
EndMacro

View file

@ -0,0 +1,7 @@
Procedure FibonacciReq(n)
If n<2
ProcedureReturn n
Else
ProcedureReturn FibonacciReq(n-1)+FibonacciReq(n-2)
EndIf
EndProcedure

View file

@ -0,0 +1,21 @@
Procedure Fibonacci(n)
Static NewMap Fib.i()
Protected FirstRecursion
If MapSize(Fib())= 0 ; Init the hash table the first run
Fib("0")=0: Fib("1")=1
FirstRecursion = #True
EndIf
If n >= 2
Protected.s s=Str(n)
If Not FindMapElement(Fib(),s) ; Calculate only needed parts
Fib(s)= Fibonacci(n-1)+Fibonacci(n-2)
EndIf
n = Fib(s)
EndIf
If FirstRecursion ; Free the memory when finalizing the first call
ClearMap(Fib())
EndIf
ProcedureReturn n
EndProcedure

View file

@ -0,0 +1,5 @@
data Fib1 = FoldNat
<
const (Cons One (Cons One Empty)),
(uncurry Cons) . ((uncurry Add) . (Head, Head . Tail), id)
>

View file

@ -0,0 +1,2 @@
type (Stream A?,,,Unfold) = gfix X. A? . X?
data Fib2 = Unfold ((outl, (uncurry Add, outl))) ((curry id) One One)

View file

@ -0,0 +1,11 @@
import Histo
data Fib3 = Histo . Memoize
<
const One,
(p1 =>
<
const One,
(p2 => Add (outl $p1) (outl $p2)). UnmakeCofree
> (outr $p1)) . UnmakeCofree
>

View file

@ -0,0 +1,5 @@
(define fib
0 -> 0
1 -> 1
N -> (+ (fib-r (- N 1))
(fib-r (- N 2))))

View file

@ -0,0 +1,6 @@
(define fib-0
V2 V1 0 -> V2
V2 V1 N -> (fib-0 V1 (+ V2 V1) (1- N)))
(define fib
N -> (fib-0 0 1 N))

View file

@ -0,0 +1,14 @@
Function fibo(n as integer) As UInt64
dim noOne as UInt64 = 1
dim noTwo as UInt64 = 1
dim sum As UInt64
for i as integer = 1 to n
sum = noOne + noTwo
noTwo = noOne
noOne = sum
Next
Return noOne
End Function

View file

@ -0,0 +1 @@
: fib ( n-m ) dup [ 0 = ] [ 1 = ] bi or if; [ 1- fib ] sip [ 2 - fib ] do + ;

View file

@ -0,0 +1,2 @@
: fib ( n-N )
[ 0 1 ] dip [ over + swap ] times drop ;

View file

@ -0,0 +1,18 @@
for i = 0 to 10
print i;" ";fibR(i);" ";fibI(i)
next i
end
function fibR(n)
if n < 2 then fibR = n else fibR = fibR(n-1) + fibR(n-2)
end function
function fibI(n)
b = 1
for i = 1 to n
t = a + b
a = b
b = t
next i
fibI = a
end function

View file

@ -0,0 +1,67 @@
fn fib(n: int, f: fn (num: i64) -> bool) -> (i64, int) {
if n < 0 {
// Let these variables be mutated, otherwise too slow
let mut n1:i64 = 0, n2:i64 = -1, i:int = 0, 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");
return (n1, i);
}
n1 = n2;
n2 = tmp;
i -= 1;
}
(n1+n2, n)
} else if n > 0 {
// And these variables
let mut n1:i64 = 0, n2:i64 = 1, i:int = 0, tmp:i64;
while i < n {
f(n1);
tmp = n1+n2;
if (tmp < 0) { //Detect overflow
io::println("\nReached the limit of i64, halting");
return (n1, i);
}
n1 = n2;
n2 = tmp;
i += 1;
}
(n2-n1, n)
} else {
f(0);
(0,1)
}
}
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...";
};
/* Use the loop protocol to be able to do things
* with the sequence given, in this case, print them out.
* The loop itself returns a tuple with where it got to and
* what the number is.
*/
let (result, n) = for fib(n) |num| {
//print out the sequence
io::print(fmt!("%? ", num));
};
io::println(fmt!("\nThe %dth fibonacci number is: %?", n, result));
}

View file

@ -0,0 +1,19 @@
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)
}
}
_fib(n, 0, 1)
}
for int::range(0,20) |n| {
io::println(fmt!("%?", fib(n)))
}
}

View file

@ -0,0 +1,12 @@
/* building a table with fibonacci sequence */
data fib;
a=0;
b=1;
do n=0 to 20;
f=a;
output;
a=b;
b=f+a;
end;
keep n f;
run;

View file

@ -0,0 +1,8 @@
define("fib(a)") :(fib_end)
fib fib = lt(a,2) a :s(return)
fib = fib(a - 1) + fib(a - 2) :(return)
fib_end
while a = trim(input) :f(end)
output = a " " fib(a) :(while)
end

View file

@ -0,0 +1,4 @@
define('trfib(n,a,b)') :(trfib_end)
trfib trfib = eq(n,0) a :s(return)
trfib = trfib(n - 1, a + b, a) :(return)
trfib_end

View file

@ -0,0 +1,6 @@
define('ifib(n)f1,f2') :(ifib_end)
ifib ifib = le(n,2) 1 :s(return)
f1 = 1; f2 = 1
if1 ifib = gt(n,2) f1 + f2 :f(return)
f1 = f2; f2 = ifib; n = n - 1 :(if1)
ifib_end

View file

@ -0,0 +1,5 @@
define('afib(n)s5') :(afib_end)
afib s5 = sqrt(5)
afib = (((1 + s5) / 2) ^ n - ((1 - s5) / 2) ^ n) / s5
afib = convert(afib,'integer') :(return)
afib_end

View file

@ -0,0 +1,5 @@
loop i = lt(i,10) i + 1 :f(show)
s1 = s1 fib(i) ' ' ; s2 = s2 trfib(i,0,1) ' '
s3 = s3 ifib(i) ' '; s4 = s4 afib(i) ' ' :(loop)
show output = s1; output = s2; output = s3; output = s4
end

View file

@ -0,0 +1,10 @@
const func integer: fib (in integer: number) is func
result
var integer: result is 1;
begin
if number > 2 then
result := fib(pred(number)) + fib(number - 2);
elsif number = 0 then
result := 0;
end if;
end func;

View file

@ -0,0 +1,14 @@
const func bigInteger: fib (in integer: number) is func
result
var bigInteger: result is 1_;
local
var integer: i is 0;
var bigInteger: a is 0_;
var bigInteger: c is 0_;
begin
for i range 1 to pred(number) do
c := a;
a := result;
result +:= c;
end for;
end func;

View file

@ -0,0 +1,9 @@
n@(Integer traits) fib
[
n <= 0 ifTrue: [^ 0].
n = 1 ifTrue: [^ 1].
(n - 1) fib + (n - 2) fib
].
slate[15]> 10 fib = 55.
True

View file

@ -0,0 +1,7 @@
fun fib n =
let
fun fib' (0,a,b) = a
| fib' (n,a,b) = fib' (n-1,a+b,a)
in
fib' (n,0,1)
end

View file

@ -0,0 +1,10 @@
void->int feedbackloop Fib {
join roundrobin(0,1);
body in->int filter {
work pop 1 push 1 peek 2 { push(peek(0) + peek(1)); pop(); }
};
loop Identity<int>;
split duplicate;
enqueue(0);
enqueue(1);
}

View file

@ -0,0 +1,13 @@
:Disp "0" //Dirty, I know, however this does not interfere with the code
:Disp "1"
:Disp "1"
:1→A
:1→B
:0→C
:Goto 1
:Lbl 1
:A+B→C
:Disp C
:B→A
:C→B
:Goto 1

View file

@ -0,0 +1,9 @@
:Prompt N
:0→A
:1→B
:For(I,1,N)
:A→C
:B→A
:C+B→B
:End
:A

View file

@ -0,0 +1,3 @@
:Prompt N
:.5(1+√(5))→P
:(P^N(-1/P)^N)/√(5)

View file

@ -0,0 +1,2 @@
fib(n)
when(n<2, n, fib(n-1) + fib(n-2))

View file

@ -0,0 +1,12 @@
fib(n)
Func
Local a,b,c,i
0→a
1→b
For i,1,n
a→c
b→a
c+b→b
EndFor
a
EndFunc

View file

@ -0,0 +1,44 @@
// library: math: get: series: fibonacci <description></description> <version control></version control> <version>1.0.0.0.3</version> <version control></version control> (filenamemacro=getmasfi.s) [<Program>] [<Research>] [kn, ri, su, 20-01-2013 22:04:02]
INTEGER PROC FNMathGetSeriesFibonacciI( INTEGER nI )
//
// Method:
//
// 1. Take the sum of the last 2 terms
//
// 2. Let the sum be the last term
// and goto step 1
//
INTEGER I = 0
INTEGER minI = 1
INTEGER maxI = nI
INTEGER term1I = 0
INTEGER term2I = 1
INTEGER term3I = 0
//
FOR I = minI TO maxI
//
// make value 3 equal to sum of two previous values 1 and 2
//
term3I = term1I + term2I
//
// make value 1 equal to next value 2
//
term1I = term2I
//
// make value 2 equal to next value 3
//
term2I = term3I
//
ENDFOR
//
RETURN( term3I )
//
END
PROC Main()
STRING s1[255] = "3"
REPEAT
IF ( NOT ( Ask( " = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF
Warn( FNMathGetSeriesFibonacciI( Val( s1 ) ) ) // gives e.g. 3
UNTIL FALSE
END

View file

@ -0,0 +1,14 @@
$$ MODE TUSCRIPT
ASK "What fibionacci number do you want?": searchfib=""
IF (searchfib!='digits') STOP
Loop n=0,{searchfib}
IF (n==0) THEN
fib=fiba=n
ELSEIF (n==1) THEN
fib=fibb=n
ELSE
fib=fiba+fibb, fiba=fibb, fibb=fib
ENDIF
IF (n!=searchfib) CYCLE
PRINT "fibionacci number ",n,"=",fib
ENDLOOP

Some files were not shown because too many files have changed in this diff Show more