You are viewing a single comment's thread. Return to all comments →
Java
public static List<Integer> PRIME_NUMBERS = new ArrayList<>(); public static String sillyGame(int n) { int numberOfPrimes = PRIME_NUMBERS.size(); Integer largestPrimeNumber = numberOfPrimes > 0 ? PRIME_NUMBERS.get(numberOfPrimes -1) : 0; if (numberOfPrimes == 0) { for (int i = 2; i <= n; i++) { if (isPrime(i)) PRIME_NUMBERS.add(i); } } else if (n >= largestPrimeNumber) { for (int i = largestPrimeNumber + 1; i <=n; i++) { if (isPrime(i)) PRIME_NUMBERS.add(i); } } else { PRIME_NUMBERS.removeIf(p -> p > n); } return PRIME_NUMBERS.size() % 2 == 0 ? "Bob" : "Alice"; } static boolean isPrime(int num) { if (num <= 1) return false; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) return false; } return true; }
Seems like cookies are disabled on this browser, please enable them to open this website
Alice and Bob's Silly Game
You are viewing a single comment's thread. Return to all comments →
Java