Alice and Bob's Silly Game

Sort by

recency

|

56 Discussions

|

  • + 0 comments

    Java

        public static String sillyGame(int n) {
    
            int player = 0;
                    Set<Integer> primes = new TreeSet<>();
    
            for (int i = 2; i <= n; i++){
                if (isPrime(i)){
                    primes.add(i);
                }
            }
    
            if (!primes.isEmpty()){
                player = primes.size() % 2;
            }
    
            return player == 0 ? "Bob" : "Alice";
    
        }
    
        private static boolean isPrime(int m) {
            // Corner case
            if (m <= 1)
                return false;
            // For m=2 or m=3 it will check
            if (m == 2 || m == 3)
                return true;
            // For multiple of 2 or 3 This will check
            if (m % 2 == 0 || m % 3 == 0)
                return false;
            // It will check all the others condition
            for (int i = 5; i <= Math.sqrt(m); i = i + 6)
                if (m % i == 0 || m % (i + 2) == 0)
                    return false;
    
            return true;
        }
    
  • + 0 comments

    Java

    public static String sillyGame(int n) {
    // Write your code here
         if(n==1 || n==3 || n==4 || n==7 || n==8 || n==9 || n==10 ){
             return "Bob";
         }
         if(n==2 || n==5 || n==6 ){
            return "Alice"; 
         }
         List<Integer> list=new ArrayList<>();
         List<Integer> li=new ArrayList<>();
         li.add(2);
         li.add(3);
         li.add(5);
         li.add(7);
    
         for(int i=11;i<=n;i++){
        if(i%2==0 || i%3==0 || i%4==0 || i%5==0 || i%6==0 || i%7==0 || i%8==0 || i%9==0 || i%10==0){
                 continue;
             }else{
                 list.add(i);
             }
         }
    
         int c;
         for(int i=0;i<list.size();i++){
             c=0;
            for(int j=4;j<li.size();j++){
                   if(li.get(j)>(int)Math.sqrt(list.get(i))){
                       break;
                   }
                if(list.get(i)%li.get(j)==0){
                    c++;
                    break;
                }else{
                    continue;
                }
            }
            if(c==0){
                li.add(list.get(i));
            } 
         }
    
         System.out.println(list.size());
         System.out.println(li.size());
    
         if(li.size()%2==0){
             return "Bob";
         } 
         else{
             return "Alice";
         }   
    }
    
  • + 1 comment

    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;
    }
    
  • + 0 comments

    JavaScript Solution

    let ans = 0
        if (n === 1) return "Bob"
        else ans++      // because n>0
        function isPrime(i) {
    // optimal the loop condition
            let limit = Math.round(Math.sqrt(i)) 
            for (let mod = 2; mod <= limit; mod++) {
                if (i % mod === 0) {
                    return false;
                }
            }
            return true
        }
    // eleminate the even number except 2 to optimal the loop
        for (let i = 3; i <= n; i += 2) {   
            if (isPrime(i))
                ans++
        }
        return ans % 2 === 0 ? "Bob" : "Alice"
    
  • + 0 comments

    Here is Alice and Bob's Silly game problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-alice-and-bobs-silly-game-problem-solution.html