C
Date: 1972 (Standardized in ANSI X3.159-1989 and ISO/IEC 9899:1990)
Type: Imperative low-level language
Usage: Everything written for UNIX-based operating systems, and the vast majority of system and application software everywhere.
Note: These examples print the 46th Fibonacci number, which is the largest number representable by unsigned 32-bit integer data type.
| ALGORITHM 1A: BINARY RECURSION |
#include <stdio.h>
unsigned int f(int n) {
return n<2 ? 1 : f(n-1)+f(n-2);
}
void f_print(int n) {
printf("%dth Fibonacci number is %lu\n",n,f(n));
}
int main(void) {
f_print(46);
return 0;
}
|
| ALGORITHM 2A-3: DATA STRUCTURE - SIMPLE LIST |
#include <stdio.h>
#include <stdlib.h>
struct FL {
unsigned int f;
struct FL* next;
};
struct FL* fl_init(void) {
struct FL* fl_h;
fl_h = (struct FL*)calloc(1,sizeof(struct FL));
fl_h->next = (struct FL*)calloc(1,sizeof(struct FL));
fl_h->f = fl_h->next->f = 1;
return fl_h;
}
unsigned int fl_get(struct FL* fl_h, int n)
{
int i;
struct FL* fl;
fl=fl_h;
for(i=0; (fl->next!=NULL) && (i<n); i++, fl=fl->next);
for(; i<n; i++, fl=fl->next) {
fl->next=(struct FL*)calloc(1,sizeof(struct FL));
fl->next->f=fl_get(fl_h,i-1)+fl_get(fl_h,i);
}
return fl->f;
}
void f_print(int n) {
struct FL* fl;
fl=fl_init();
printf("%dth Fibonacci number is %lu\n",n,fl_get(fl,n));
}
int main(void) {
f_print(46);
return 0;
}
|
| ALGORITHM 2B: SIMPLE RECURSION |
#include <stdio.h>
unsigned int l(unsigned int n1,unsigned int n2,int n) {
return n<2 ? n1 : l(n1+n2, n1, n-1);
}
unsigned int f(int n) {
l(1,1,n);
}
void f_print(int n) {
printf("%dth Fibonacci number is %lu\n",n,f(n));
}
int main(void) {
f_print(46);
return 0;
}
|
| ALGORITHM 2C: NON-RECURSIVE LOOP |
#include <stdio.h>
unsigned int f(int n) {
int i;
unsigned int n1=1,n2=1;
for(i=1; i<=n; i++) {
n1+=n2;
n1^=n2^=n1^=n2;
}
return n1;
}
void f_print(int n) {
printf("%dth Fibonacci number is %lu\n",n,f(n));
}
int main(void) {
f_print(46);
return 0;
}
|
| ALGORITHM 3A: MATRIX EQUATION |
#include <stdio.h>
struct M2x2 { unsigned int a[2][2]; };
struct M2x2 m_mult(struct M2x2 M1, struct M2x2 M2) {
int i,j,k;
struct M2x2 R;
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
R.a[i][j]=0;
for(k=0; k<2; k++)
R.a[i][j]+=M1.a[i][k]*M2.a[k][j];
}
return R;
}
struct M2x2 m_copy(struct M2x2 s) {
struct M2x2 d;
int i,j;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
d.a[i][j]=s.a[i][j];
return d;
}
struct M2x2 m_pow(struct M2x2 a, int n) {
struct M2x2 b;
b=m_copy(a);
if(n>1) {
a=m_pow(a,n/2);
a=m_mult(a,a);
if(n%2) a=m_mult(a,b);
}
return a;
}
unsigned int f(int n) {
struct M2x2 a;
unsigned int r;
a.a[0][0]=a.a[0][1]=a.a[1][0]=1; a.a[1][1]=0;
a=m_pow(a,n);
r=a.a[0][0];
return r;
}
void f_print(int n) {
printf("%dth Fibonacci number is %lu\n",n,f(n));
}
int main(void) {
f_print(46);
return 0;
}
|
| ALGORITHM 3B: FAST RECURSION |
#include <stdio.h>
void l(unsigned int* n1, unsigned int* n2, int n) {
unsigned int k1,k2;
if(n<=1) { *n1=1; *n2=1; return; }
if(n==2) { *n1=2; *n2=1; return; }
if(n%2) {
l(&k1,&k2,(n-1)/2);
*n1 = k1*(k1+k2) + k1*k2;
*n2 = k1*k1 + k2*k2;
} else {
l(&k1,&k2,(n/2)-1);
*n1 = (k1+k2)*(k1+k2) + k1*k1;
*n2 = (k1+k2)*k1 + k1*k2;
}
}
unsigned int f(int n) {
unsigned int n1, n2;
l(&n1,&n2,n);
return n1;
}
void f_print(int n) {
printf("%dth Fibonacci number is %lu\n",n,f(n));
}
int main(void) {
f_print(46);
return 0;
}
|
| ALGORITHM 3C: BINET'S FORMULA |
#include <stdio.h>
#include <math.h>
unsigned int f(int n) {
if (n<2) return 1;
double phi = (1+sqrt(5))/2;
return (pow(phi, n+1) - pow(1-phi, n+1)) / sqrt(5);
}
void f_print(int n) {
printf("%dth Fibonacci number is %lu\n",n,f(n));
}
int main(void) {
f_print(46);
return 0;
}
|