Fibs in your favourite language
1
Name:
Anonymous
2008-12-20 15:04
fibs = 1 : zipWith (+) fibs (tail fibs)
2
Name:
Anonymous
2008-12-20 15:16
Not this shit again.
3
Name:
Anonymous
2008-12-20 15:23
fibs = [ 1,1,2.. ]
4
Name:
Anonymous
2008-12-20 15:43
I think we have enough fibs and fact threads.
5
Name:
Anonymous
2008-12-20 15:52
fibs and fact are all that most /prog/ regulars are capable of
6
Name:
Anonymous
2008-12-20 15:54
int fibs[] = {0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765};
int fib(int n){
if(n<0 || n>20) exit();
return fibs[n];
}
Compile with -O3.
7
Name:
Anonymous
2008-12-20 16:10
8
Name:
Anonymous
2008-12-20 16:10
F# fibonacci, using sequences
let fibs = (1I, 1I) |> Seq.unfold (fun (a,b) -> Some (a, (b,a+b)))
What it does:
The bigint tuple (1I, 1I) is piped into the Seq.unfold function. Seq.unfold takes a function and a seed (the 2-tuple in this case.) The function you pass in should take in as an argument the same type as the seed, so it's the tuple (a,b) as above.
The function returns a 2-tuple option value. An option value is simple None or an actual value. Returning None at any stage will termiante the sequence.
The first value in the returned tuple is the value you want to yield in the sequence. The second value in the tuple is the new accumulator, to be passed into the function on the next iteration of the sequence. In our case, it will be the next iteration of fibonacci, (b, a+b).
F# sequences, obviously, are lazy evaluated. To access the nth value of the sequence, simply use
let nthfib = fibs |> Seq.nth n
9
Name:
Anonymous
2008-12-20 16:11
My fibs and fact defined in my personal language:
fibs = 0 + 1 + (.. + .)
fact = n * (. * [n - 1 != 0])
. and .. are defined as the previous arguments to the function.
10
Name:
Anonymous
2008-12-20 16:29
11
Name:
Anonymous
2008-12-20 16:32
>>10
I've been really getting into F# lately, especially since Microsoft have decided to include it as an official .NET language in the next Visual Studio iteration.
Check out the wikibook at
http://en.wikibooks.org/wiki/Programming:F_Sharp
Also my user page at
http://en.wikibooks.org/wiki/User:Awesome_Princess
12
Name:
Anonymous
2008-12-20 17:04
13
Name:
Anonymous
2008-12-20 17:19
Exercise 2: Define fact in terms of fibs .
14
Name:
Anonymous
2008-12-20 17:37
>>13
fact n = (n*fibs(1))*(fact n-1)
15
Name:
Anonymous
2008-12-20 18:46
>> ಠ_ಠ
16
Name:
Anonymous
2008-12-20 19:30
GOD DAMN IT HASKELL BE USEFUL FOR ONCE
17
Name:
Anonymous
2008-12-20 20:02
>>16
Haskell can be used for making super fast fibonacci functions (Thanks
dons !) It can also be used to parallelize fibonacci functions!
Honestly, what more do you need?
IO ? Input from the outside world is
PIG DISGUSTING . Your programs are not pure.
18
Name:
Anonymous
2008-12-20 20:34
>>16
One word Xmonad thread over
19
Name:
Anonymous
2008-12-20 20:44
>>18
Awesome can do the same thing without requiring a 380 mb Haskell compiler.
20
Name:
Anonymous
2008-12-20 20:52
>>19
What's the matter, don't want to actually use the 320GB h.disc?
21
Name:
Anonymous
2008-12-20 20:56
Nobody seems to have pointed out that
>>1 doesn't work.
22
Name:
Anonymous
2008-12-20 20:57
>>16
How can you expect something so lazy to be useful?
23
Name:
Anonymous
2008-12-20 21:02
>>19
Anonix developer detected
24
Name:
Anonymous
2008-12-20 21:14
sbif([1,0]).
sbif([A,B,C|Rest]) :- sbif([B,C|Rest]), A is B + C.
fibs(Fibs) :- sbif(Sbif), reverse(Sbif, Fibs).
25
Name:
Anonymous
2008-12-20 21:27
26
Name:
Anonymous
2008-12-20 22:24
>>18
>>19
I also use XMonad. It is made of win and good. It's also stable, and has never crashed for me, unlike Awesome.
27
Name:
Anonymous
2008-12-20 22:36
#include <math.h>
#define SQRT5 sqrtl(5)
#define PI (atanl(1) * 4)
#define FIB(n) ((1 / SQRT5) * (powl((SQRT5 + 1) / 2, n) - powl(2 / (SQRT5 + 1), n) * cosl(n * PI)))
28
Name:
Anonymous
2008-12-20 23:49
>>27
Enjoy you're loss of precision.
(define (fib n)
(define (iter a b p q count)
(cond ((= count 0) b)
((even? count)
(iter a
b
(+ (* p p) (* q q))
(+ (* 2 p q) (* q q))
(/ count 2)))
(else (iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
(iter 1 0 0 1 n))
29
Name:
Anonymous
2008-12-20 23:55
>>28
enjoy your slow as fuck.
30
Name:
Anonymous
2008-12-21 0:00
(def fibs (lazy-cat [1 1] (map + fibs (rest fibs))))
user=> (time (nth fibs 40))
"Elapsed time: 0.05548 msecs"
165580141
Copy/Paste Clojure
31
Name:
Anonymous
2008-12-21 0:10
>>29
Excuse me? This is an optimal algorithm, handed down from Der Süßmann himself.
32
Name:
Anonymous
2008-12-21 0:10
(time (nth fibs 10000))
"Elapsed time: 166.846 msecs"
54438373113565281338734260993750380135389184554695967026247715841208582865622349017083051547938960541173822675978026317384359584751116241439174702642959169925586334117906063048089793531476108466259072759367899150677960088306597966641965824937721800381441158841042480997984696487375337180028163763317781927941101369262750979509800713596718023814710669912644214775254478587674568963808002962265133111359929762726679441400101575800043510777465935805362502461707918059226414679005690752321895868142367849593880756423483754386342639635970733756260098962462668746112041739819404875062443709868654315626847186195620146126642232711815040367018825205314845875817193533529827837800351902529239517836689467661917953884712441028463935449484614450778762529520961887597272889220768537396475869543159172434537193611263743926337313005896167248051737986306368115003088396749587102619524631352447499505204198305187168321623283859794627245919771454628218399695789223798912199431775469705216131081096559950638297261253848242007897109054754028438149611930465061866170122983288964352733750792786069444761853525144421077928045979904561298129423809156055033032338919609162236698759922782923191896688017718575555520994653320128446502371153715141749290913104897203455577507196645425232862022019506091483585223882711016708433051169942115775151255510251655931888164048344129557038825477521111577395780115868397072602565614824956460538700280331311861485399805397031555727529693399586079850381581446276433858828529535803424850845426446471681531001533180479567436396815653326152509571127480411928196022148849148284389124178520174507305538928717857923509417743383331506898239354421988805429332440371194867215543576548565499134519271098919802665184564927827827212957649240235507595558205647569365394873317659000206373126570643509709482649710038733517477713403319028105575667931789470024118803094604034362953471997461392274791549730356412633074230824051999996101549784667340458326852960388301120765629245998136251652347093963049734046445106365304163630823669242257761468288461791843224793434406079917883360676846711185597501
Wow you can't do that in C.
33
Name:
Anonymous
2008-12-21 0:14
>>31
He was using it to demonstrate linear growth.
With SICP is the Instructor's Manual.
You need both.
34
Name:
Anonymous
2008-12-21 0:17
>>30
hotaru@olorin$ cat test.c < ~ >
#include <stdio.h>
#include "fib.c"
int main(void){
for(int i = 0; i < 1000; ++i) printf("%Lf\n", FIB(i));
return 0;
}
hotaru@olorin$ gcc -std=c99 -O2 -s -lm test.c < ~ >
hotaru@olorin$ time ./a.out > /dev/null < ~ >
./a.out > /dev/null 0,08s user 0,01s system 96% cpu 0,095 total
35
Name:
Anonymous
2008-12-21 0:17
fib←{⎕ml←3
⍵≤2:1
{(+/^\⍵='0')↓⍵
},'ZI9'⎕fmt(⍵-2){
⍺>0:(⍺-1)∇(↑⌽⍵)({
^/⍵<1e9:⍵ ⋄ (1e9|⍵)+1⌽⍵≥1e9
}(↑⍵)+↑⌽⍵) ⋄ ↑⌽⍵
}2⍴⊂(-⌈⍵÷40)↑1
}
36
Name:
Anonymous
2008-12-21 0:24
>>32
hotaru@olorin$ cat test.c < ~ >
#include <stdio.h>
#include <gmp.h>
int main(void){
mpz_t n;
mpz_init(n);
mpz_fib_ui(n, 10001);
mpz_out_str(stdout, 10, n);
putchar('\n');
return 0;
}
hotaru@olorin$ gcc -std=c99 -O2 -s -lgmp test.c < ~ >
hotaru@olorin$ time ./a.out < ~ >
54438373113565281338734260993750380135389184554695967026247715841208582865622349017083051547938960541173822675978026317384359584751116241439174702642959169925586334117906063048089793531476108466259072759367899150677960088306597966641965824937721800381441158841042480997984696487375337180028163763317781927941101369262750979509800713596718023814710669912644214775254478587674568963808002962265133111359929762726679441400101575800043510777465935805362502461707918059226414679005690752321895868142367849593880756423483754386342639635970733756260098962462668746112041739819404875062443709868654315626847186195620146126642232711815040367018825205314845875817193533529827837800351902529239517836689467661917953884712441028463935449484614450778762529520961887597272889220768537396475869543159172434537193611263743926337313005896167248051737986306368115003088396749587102619524631352447499505204198305187168321623283859794627245919771454628218399695789223798912199431775469705216131081096559950638297261253848242007897109054754028438149611930465061866170122983288964352733750792786069444761853525144421077928045979904561298129423809156055033032338919609162236698759922782923191896688017718575555520994653320128446502371153715141749290913104897203455577507196645425232862022019506091483585223882711016708433051169942115775151255510251655931888164048344129557038825477521111577395780115868397072602565614824956460538700280331311861485399805397031555727529693399586079850381581446276433858828529535803424850845426446471681531001533180479567436396815653326152509571127480411928196022148849148284389124178520174507305538928717857923509417743383331506898239354421988805429332440371194867215543576548565499134519271098919802665184564927827827212957649240235507595558205647569365394873317659000206373126570643509709482649710038733517477713403319028105575667931789470024118803094604034362953471997461392274791549730356412633074230824051999996101549784667340458326852960388301120765629245998136251652347093963049734046445106365304163630823669242257761468288461791843224793434406079917883360676846711185597501
./a.out 0,01s user 0,00s system 76% cpu 0,009 total
37
Name:
Anonymous
2008-12-21 0:33
38
Name:
Anonymous
2008-12-21 0:34
39
Name:
Anonymous
2008-12-21 0:36
>>35
It carries out and is ⍵.
40
Name:
Anonymous
2008-12-21 0:46
>>37
i cried while reading those posts
Newer Posts