Skip to content

Instantly share code, notes, and snippets.

@joriki
Created May 8, 2020 06:55
Show Gist options
  • Save joriki/429b8a8d62dd817f3aef15d73fbce53f to your computer and use it in GitHub Desktop.
Save joriki/429b8a8d62dd817f3aef15d73fbce53f to your computer and use it in GitHub Desktop.
Count intervals of size 1/n hit by the numbers 1/k; see https://math.stackexchange.com/questions/3664030.
public class Question3664030 {
public static void main(String [] args) {
for (int n = 1;n < 2000;n++) {
int count = 0;
boolean [] hit = new boolean [n];
for (int k = 1;k <= n + 1;k++) {
double x = 1. / k;
// to count values on boundaries only towards the lower interval, subtract 1e-12 here ...
int index = (int) (n * x);
if (index < n && !hit [index]) {
hit [index] = true;
count++;
}
// ... and remove this second block.
if (index == n * x && !hit [index - 1]) {
hit [index - 1] = true;
count++;
}
}
System.out.println(n + " " + count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment