Skip to content

Instantly share code, notes, and snippets.

@lccxx
Created November 3, 2011 04:01
Show Gist options
  • Save lccxx/1335748 to your computer and use it in GitHub Desktop.
Save lccxx/1335748 to your computer and use it in GitHub Desktop.
F[0]=1; F[1]=1; F[n]=F[n div 2]+F[n div 3]+F[n div 5]+F[n div 7] with C
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
typedef struct {
unsigned long long buff[65535];
int size;
} collection;
void collection_init(collection *c) {
c->size = 0;
}
void collection_add(collection *c, unsigned long long e) {
c->buff[c->size] = e;
c->size ++;
}
void collect_div(collection *c, int div, unsigned long long n) {
unsigned long long div_r;
div_r = n;
while (true) {
div_r /= div;
if (div_r == 0 || div_r == 1) {
collection_add(c, 1);
break;
}
collection_add(c, div_r);
}
}
unsigned long long loop_f(unsigned long long n) {
collection c_2, c_3, c_5, c_7;
collection_init(&c_2);
collection_init(&c_3);
collection_init(&c_5);
collection_init(&c_7);
collect_div(&c_2, 2, n);
collect_div(&c_3, 3, n);
collect_div(&c_5, 5, n);
collect_div(&c_7, 7, n);
unsigned long long sum = 0;
int i2, i3, i5, i7;
for (i2 = 0; i2 < c_2.size; i2++)
for (i3 = 0; i3 < c_3.size; i3++)
for (i5 = 0; i5 < c_5.size; i5++)
for (i7 = 0; i7 < c_7.size; i7++)
sum += c_2.buff[i2] + c_3.buff[i3] + c_5.buff[i5] + c_7.buff[i7];
return sum;
}
int main(void) {
unsigned long long n = 400000000000;
printf("result: %llu\n", loop_f(n));
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment