Skip to content

Instantly share code, notes, and snippets.

@anoopelias
Created November 22, 2014 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoopelias/85f8cf47d22cbb649e83 to your computer and use it in GitHub Desktop.
Save anoopelias/85f8cf47d22cbb649e83 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
class TestClass {
private static final int[] PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23 };
public static void main(String args[]) throws Exception {
goodVsBad(23, 46, 22);
}
public static void goodVsBad(int hh, int mm, int ss) {
int total = (60 - ss) + ((59 - mm) * 60) + ((23 - hh) * 3600);
int bads = 0;
List<Integer> primes = null;
for (int h = hh; h < 24; h++) {
primes = new ArrayList<>();
for (int i = 0; i < PRIMES.length; i++) {
if (h % PRIMES[i] == 0) {
primes.add(PRIMES[i]);
}
}
if (h == hh)
bads += combinations(mm, ss, primes);
else
bads += combinations(0, 0, primes);
}
int goods = total - bads;
int hcf = hcf(goods, bads);
System.out.println((bads / hcf) + ":" + (goods / hcf));
}
public static int combinations(int mm, int ss, List<Integer> primes) {
return count(mm, primes) * count(ss, primes);
}
public static int count(int start, List<Integer> primes) {
int count = 0;
for (int i = start; i < 60; i++) {
for (int p : primes) {
if (i % p == 0) {
count++;
break;
}
}
}
return count;
}
public static int hcf(int n1, int n2) {
int hcf = 1;
int min = Math.min(n1, n2);
for (int i = 1; i <= min; i++) {
if (n1 % i == 0 && n2 % i == 0) {
hcf = i;
}
}
return hcf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment