Last active
October 28, 2018 05:23
-
-
Save jalola/5720738 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
//alpha = 1; beta = 3.14 | |
private static long retrieval(double x) { | |
createPascal((int) x); | |
long sum = 0; | |
int level = (int) (x / beta) - 1; // first level | |
int k = level; | |
int remember = 0; | |
while (k >= 0) { | |
double delta = x - (level - k) * alpha - k * beta; // the last P will be processed first | |
if (delta <= 4) { | |
sum += pascal[level][k] - remember; // add pascal to sum | |
k--; | |
remember = 0; | |
} else { | |
remember += pascal[level][k+1]; | |
level++; // increase level to 1 because delta is not small enough | |
k++; | |
} | |
} | |
return sum; | |
} | |
private static void createPascal(int n) { | |
pascal = new int[n][n]; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j <= i; j++) { | |
if (j == 0 || j == i) { | |
pascal[i][j] = 1; | |
} else { | |
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
khà khà