Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 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 fibb(long long a, long long b, int n) {
|
||||
return (--n>0)?(fibb(b, a+b, n)):(a);
|
||||
}
|
||||
9
Task/Fibonacci-sequence/C/fibonacci-sequence-2.c
Normal file
9
Task/Fibonacci-sequence/C/fibonacci-sequence-2.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
long long int fibb(int n) {
|
||||
int fnow = 0, fnext = 1, tempf;
|
||||
while(--n>0){
|
||||
tempf = fnow + fnext;
|
||||
fnow = fnext;
|
||||
fnext = tempf;
|
||||
}
|
||||
return fnext;
|
||||
}
|
||||
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");
|
||||
}
|
||||
74
Task/Fibonacci-sequence/C/fibonacci-sequence-5.c
Normal file
74
Task/Fibonacci-sequence/C/fibonacci-sequence-5.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <gmp.h>
|
||||
|
||||
typedef struct node node;
|
||||
struct node {
|
||||
int n;
|
||||
mpz_t v;
|
||||
node *next;
|
||||
};
|
||||
|
||||
#define CSIZE 37
|
||||
node *cache[CSIZE];
|
||||
|
||||
// very primitive linked hash table
|
||||
node * find_cache(int n)
|
||||
{
|
||||
int idx = n % CSIZE;
|
||||
node *p;
|
||||
|
||||
for (p = cache[idx]; p && p->n != n; p = p->next);
|
||||
if (p) return p;
|
||||
|
||||
p = malloc(sizeof(node));
|
||||
p->next = cache[idx];
|
||||
cache[idx] = p;
|
||||
|
||||
if (n < 2) {
|
||||
p->n = n;
|
||||
mpz_init_set_ui(p->v, 1);
|
||||
} else {
|
||||
p->n = -1; // -1: value not computed yet
|
||||
mpz_init(p->v);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
mpz_t tmp1, tmp2;
|
||||
mpz_t *fib(int n)
|
||||
{
|
||||
int x;
|
||||
node *p = find_cache(n);
|
||||
|
||||
if (p->n < 0) {
|
||||
p->n = n;
|
||||
x = n / 2;
|
||||
|
||||
mpz_mul(tmp1, *fib(x-1), *fib(n - x - 1));
|
||||
mpz_mul(tmp2, *fib(x), *fib(n - x));
|
||||
mpz_add(p->v, tmp1, tmp2);
|
||||
}
|
||||
return &p->v;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, n;
|
||||
if (argc < 2) return 1;
|
||||
|
||||
mpz_init(tmp1);
|
||||
mpz_init(tmp2);
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
n = atoi(argv[i]);
|
||||
if (n < 0) {
|
||||
printf("bad input: %s\n", argv[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// about 75% of time is spent in printing
|
||||
gmp_printf("%Zd\n", *fib(n));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue