Alice and Bob's Silly Game

Sort by

recency

|

55 Discussions

|

  • + 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

  • + 0 comments

    Linear Sieve+Prefix Sums

    void sive(){
    	memset(lp,0,sizeof lp);
    	for (int i=2; i<=N; ++i) {
    	if (lp[i] == 0) {
    		lp[i] = i;
    		pr.push_back (i);
    	}
    	for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j){
    		lp[i * pr[j]] = pr[j];
    	}
        }
        a[0]=a[1]=0;
        for(int i=2;i<mxn;i++){
            if(lp[i]==i){
                a[i]=a[i-1]+1;
                continue;
            }
            a[i]=a[i-1];
        }
    }
    void test_case(){
    	int n,cnt=0;
    	read(n);
    	if(n<2){
    		cout<<"Bob"<<endl;
    		return;
    	}
    	cnt=a[n];
    	cout<<(cnt&1?"Alice":"Bob")<<endl;
    }
    int main(){
    	int tt;
        sync();
    	read(tt);
    	sive();
    	while(tt--){
    		test_case();
    	}
    }