Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1 +1 @@
+.×/N/2 21 1 1 0
fib{1: ( -1)+ -2}

View file

@ -1 +1 @@
0 1+.×/N/2 21 1 1 0
+.×/N/2 21 1 1 0

View file

@ -1 +1 @@
.5+(((1+PHI)÷2)*N)÷PHI5*.5
0 1+.×/N/2 21 1 1 0

View file

@ -0,0 +1 @@
.5+(((1+PHI)÷2)*N)÷PHI5*.5

View file

@ -2,7 +2,7 @@ import extensions;
fibu(n)
{
int[] ac := new int[] { 0,1 };
int[] ac := new int[]::( 0,1 );
if (n < 2)
{
^ ac[n]

View file

@ -0,0 +1,12 @@
fibi is op n {
if n<2 then
n
else
x1:=0; x2:=1;
for i with tell (n - 1) do
x:=x1+x2;
x1:=x2;
x2:=x;
endfor;
x2
endif};

View file

@ -0,0 +1 @@
fibf is op n {1 pick ((n- 1) fold [1 pick, +] 0 1)};

View file

@ -0,0 +1 @@
fibf2 is 1 pick fold [1 pick, +] reverse (0 1 hitch) (-1+);

View file

@ -0,0 +1 @@
fibr is op n {fork [2>, +, + [fibr (-1 +), fibr (-2 +)]] n};

View file

@ -0,0 +1 @@
fibr2 is fork [2>, +, + [fibr2 (-1 +), fibr2 (-2 +)]];

View file

@ -0,0 +1 @@
fibm is op n {floor (0 1 pick (reduce ip (n reshape [2 2 reshape 1 1 1 0])))};

View file

@ -0,0 +1,5 @@
$ is reshape;
~ is tr f op a b {b f a}; % Goes before verb, rather than after like in J;
_ is floor; % Not really J, but J-ish? (Cannot redefine "<.".);
fibm2 is _(0 1 pick reduce ip([2 2$1 1 1 0](~$)));

View file

@ -0,0 +1 @@
fibm3 is op n {a:=2 2$1 1 1 0; _(0 1 pick ((n- 1) fold (a ip) a))};

View file

@ -1,19 +1,11 @@
open Num
let rec fib_rec n =
if n < 2 then
n
else
fib_rec (n - 1) + fib_rec (n - 2)
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)
(* support for negatives *)
let fib n =
if n < 0 && n mod 2 = 0 then minus_num (fib (abs n))
else fib (abs n)
;;
(* It can be called from the command line with an argument *)
(* Result is send to standart output *)
let n = int_of_string Sys.argv.(1) in
print_endline (string_of_num (fib n))
let rec fib = function
0 -> 0
| 1 -> 1
| n -> if n > 0 then fib (n-1) + fib (n-2)
else fib (n+2) - fib (n+1)

View file

@ -1,16 +1,19 @@
open Num
let mul (a,b,c) (d,e,f) = let bxe = b*/e in
(a*/d +/ bxe, a*/e +/ b*/f, bxe +/ c*/f)
let id = (Int 1, Int 0, Int 1)
let rec pow a n =
if n=0 then id 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 =
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)
(* support for negatives *)
let fib n =
let (_,y,_) = (pow (Int 1, Int 1, Int 0) n) in
string_of_num y
if n < 0 && n mod 2 = 0 then minus_num (fib (abs n))
else fib (abs n)
;;
Printf.printf "fib %d = %s\n" 300 (fib 300)
(* It can be called from the command line with an argument *)
(* Result is send to standart output *)
let n = int_of_string Sys.argv.(1) in
print_endline (string_of_num (fib n))

View file

@ -0,0 +1,16 @@
open Num
let mul (a,b,c) (d,e,f) = let bxe = b*/e in
(a*/d +/ bxe, a*/e +/ b*/f, bxe +/ c*/f)
let id = (Int 1, Int 0, Int 1)
let rec pow a n =
if n=0 then id 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,8 @@
CREATE OR REPLACE FUNCTION fib(n INTEGER) RETURNS INTEGER AS $$
BEGIN
IF (n < 2) THEN
RETURN n;
END IF;
RETURN fib(n - 1) + fib(n - 2);
END;
$$ LANGUAGE plpgsql;

View file

@ -0,0 +1,5 @@
CREATE OR REPLACE FUNCTION fibFormula(n INTEGER) RETURNS INTEGER AS $$
BEGIN
RETURN round(pow((pow(5, .5) + 1) / 2, n) / pow(5, .5));
END;
$$ LANGUAGE plpgsql;

View file

@ -0,0 +1,17 @@
CREATE OR REPLACE FUNCTION fibLinear(n INTEGER) RETURNS INTEGER AS $$
DECLARE
prevFib INTEGER := 0;
fib INTEGER := 1;
BEGIN
IF (n < 2) THEN
RETURN n;
END IF;
WHILE n > 1 LOOP
SELECT fib, prevFib + fib INTO prevFib, fib;
n := n - 1;
END LOOP;
RETURN fib;
END;
$$ LANGUAGE plpgsql;

View file

@ -0,0 +1,9 @@
CREATE OR REPLACE FUNCTION fibTailRecursive(n INTEGER, prevFib INTEGER DEFAULT 0, fib INTEGER DEFAULT 1)
RETURNS INTEGER AS $$
BEGIN
IF (n = 0) THEN
RETURN prevFib;
END IF;
RETURN fibTailRecursive(n - 1, fib, prevFib + fib);
END;
$$ LANGUAGE plpgsql;

View file

@ -1,14 +1,9 @@
#![feature(conservative_impl_trait)]
fn main() {
for num in fibonacci_gen(10) {
println!("{}", num);
fn fib_tail_recursive(nth: usize) -> usize {
fn fib_tail_iter(n: usize, prev_fib: usize, fib: usize) -> usize {
match n {
0 => prev_fib,
n => fib_tail_iter(n - 1, fib, prev_fib + fib),
}
}
fn fibonacci_gen(terms: i32) -> impl Iterator<Item=u64> {
let sqrt_5 = 5.0f64.sqrt();
let p = (1.0 + sqrt_5) / 2.0;
let q = 1.0/p;
(1..terms).map(move |n| ((p.powi(n) + q.powi(n)) / sqrt_5 + 0.5) as u64)
}
fib_tail_iter(nth, 0, 1)
}

View file

@ -1,29 +1,14 @@
use std::mem;
struct Fib {
prev: usize,
curr: usize,
}
impl Fib {
fn new() -> Self {
Fib {prev: 0, curr: 1}
}
}
impl Iterator for Fib {
type Item = usize;
fn next(&mut self) -> Option<Self::Item>{
mem::swap(&mut self.curr, &mut self.prev);
self.curr.checked_add(self.prev).map(|n| {
self.curr = n;
n
})
}
}
#![feature(conservative_impl_trait)]
fn main() {
for num in Fib::new() {
for num in fibonacci_gen(10) {
println!("{}", num);
}
}
fn fibonacci_gen(terms: i32) -> impl Iterator<Item=u64> {
let sqrt_5 = 5.0f64.sqrt();
let p = (1.0 + sqrt_5) / 2.0;
let q = 1.0/p;
(1..terms).map(move |n| ((p.powi(n) + q.powi(n)) / sqrt_5 + 0.5) as u64)
}

View file

@ -0,0 +1,29 @@
use std::mem;
struct Fib {
prev: usize,
curr: usize,
}
impl Fib {
fn new() -> Self {
Fib {prev: 0, curr: 1}
}
}
impl Iterator for Fib {
type Item = usize;
fn next(&mut self) -> Option<Self::Item>{
mem::swap(&mut self.curr, &mut self.prev);
self.curr.checked_add(self.prev).map(|n| {
self.curr = n;
n
})
}
}
fn main() {
for num in Fib::new() {
println!("{}", num);
}
}

View file

@ -0,0 +1,23 @@
func Fib1(N); \Return Nth Fibonacci number using iteration
int N;
int Fn, F0, F1;
[F0:= 0; F1:= 1; Fn:= N;
while N > 1 do
[Fn:= F0 + F1;
F0:= F1;
F1:= Fn;
N:= N-1;
];
return Fn;
];
func Fib2(N); \Return Nth Fibonacci number using recursion
int N;
return if N < 2 then N else Fib2(N-1) + Fib2(N-2);
int N;
[for N:= 0 to 20 do [IntOut(0, Fib1(N)); ChOut(0, ^ )];
CrLf(0);
for N:= 0 to 20 do [IntOut(0, Fib2(N)); ChOut(0, ^ )];
CrLf(0);
]