A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
3
Task/Fibonacci-sequence/C/fibonacci-sequence-1.c
Normal file
3
Task/Fibonacci-sequence/C/fibonacci-sequence-1.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
long long unsigned fib(unsigned n) {
|
||||
return n < 2 ? n : fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
10
Task/Fibonacci-sequence/C/fibonacci-sequence-2.c
Normal file
10
Task/Fibonacci-sequence/C/fibonacci-sequence-2.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
long long unsigned fib(unsigned n) {
|
||||
long long unsigned last = 0, this = 1, new, i;
|
||||
if (n < 2) return n;
|
||||
for (i = 1 ; i < n ; ++i) {
|
||||
new = last + this;
|
||||
last = this;
|
||||
this = new;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
6
Task/Fibonacci-sequence/C/fibonacci-sequence-3.c
Normal file
6
Task/Fibonacci-sequence/C/fibonacci-sequence-3.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include <tgmath.h>
|
||||
#define PHI ((1 + sqrt(5))/2)
|
||||
|
||||
long long unsigned fib(unsigned n) {
|
||||
return floor( (pow(PHI, n) - pow(1 - PHI, n))/sqrt(5) );
|
||||
}
|
||||
37
Task/Fibonacci-sequence/C/fibonacci-sequence-4.c
Normal file
37
Task/Fibonacci-sequence/C/fibonacci-sequence-4.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
typedef enum{false=0, true=!0} bool;
|
||||
typedef void iterator;
|
||||
|
||||
#include <setjmp.h>
|
||||
/* declare label otherwise it is not visible in sub-scope */
|
||||
#define LABEL(label) jmp_buf label; if(setjmp(label))goto label;
|
||||
#define GOTO(label) longjmp(label, true)
|
||||
|
||||
/* the following line is the only time I have ever required "auto" */
|
||||
#define FOR(i, iterator) { auto bool lambda(i); yield_init = (void *)λ iterator; bool lambda(i)
|
||||
#define DO {
|
||||
#define YIELD(x) if(!yield(x))return
|
||||
#define BREAK return false
|
||||
#define CONTINUE return true
|
||||
#define OD CONTINUE; } }
|
||||
|
||||
static volatile void *yield_init; /* not thread safe */
|
||||
#define YIELDS(type) bool (*yield)(type) = yield_init
|
||||
|
||||
iterator fibonacci(int stop){
|
||||
YIELDS(int);
|
||||
int f[] = {0, 1};
|
||||
int i;
|
||||
for(i=0; i<stop; i++){
|
||||
YIELD(f[i%2]);
|
||||
f[i%2]=f[0]+f[1];
|
||||
}
|
||||
}
|
||||
|
||||
main(){
|
||||
printf("fibonacci: ");
|
||||
FOR(int i, fibonacci(16)) DO
|
||||
printf("%d, ",i);
|
||||
OD;
|
||||
printf("...\n");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue