#include long long seq[100]; int bits[1000]; int int2bits (n) long long n; { int i = 0; long long pow2 = 1; int pow2exp = 0; int result; for ( i = 0; i<1000; i++ ) bits[i] = 0; while ( (n - pow2) >= 0) { pow2 = 2*pow2; pow2exp++; } result = pow2exp; while (pow2exp >= 0) { if ((n - pow2) >= 0) { bits[pow2exp] = 1; n = n - pow2; } else bits[pow2exp] = 0; pow2 = pow2/2; pow2exp--; } return result; } void fibbin(limit) int limit; { int n = 2; int i, j, k; int bcount; long long sum; seq[0] = 0; seq[1] = 1; seq[2] = 1; while ( n < limit ) { bcount = int2bits(seq[n]); sum = seq[n]; i = n - 1; while ( bcount > 0 ) { sum += bits[bcount-1]*seq[i]; bcount--; i--; } seq[n+1] = sum; n++; } } main (argc, argv) int argc; char **argv; { int terms, i; terms = atoi(argv[1]); fibbin(terms); for ( i = 0 ; i<=terms ; i++) printf("Binary Fib [%d] = %lld\n", i, seq[i]); }