Last active
December 20, 2015 00:59
-
-
Save wellle/6045466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// first couple of triangular numbers | |
// http://en.wikipedia.org/wiki/Triangular_number | |
const int triag[] = {0, 1, 3, 6, 10, 15, 21}; | |
// returns numbers of elements in first `h` rows of pascal's triangle that are | |
// not divisible by 7 | |
// http://projecteuler.net/problem=148 | |
long not7(long h) { | |
long n; // current number of elements not divisible by 7 | |
long d; // current digit of `h` in base 7 representation | |
long b; // current base (7^(number of digits handled already)) | |
// iterate through the digits of `h` in base 7 from right to left | |
for (n = 0, b = 1; h > 0; h /= 7, b *= 28) { | |
d = h % 7; | |
n = n * (d+1) + triag[d] * b; | |
} | |
return n; | |
} | |
int main() { | |
long h = 1e9; | |
long n = not7(h); | |
printf("not divisible by 7 in %ld rows: %ld\n", h, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example invocation:
So for comparison I propose the following benchmark of 10M different iterations:
For example:
If it takes too long, try fewer iterations with
h += 1000
etc.