We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticStringsillyGame(intn){intplayer=0;Set<Integer>primes=newTreeSet<>();for(inti=2;i<=n;i++){if(isPrime(i)){primes.add(i);}}if(!primes.isEmpty()){player=primes.size()%2;}returnplayer==0?"Bob":"Alice";}privatestaticbooleanisPrime(intm){// Corner caseif(m<=1)returnfalse;// For m=2 or m=3 it will checkif(m==2||m==3)returntrue;// For multiple of 2 or 3 This will checkif(m%2==0||m%3==0)returnfalse;// It will check all the others conditionfor(inti=5;i<=Math.sqrt(m);i=i+6)if(m%i==0||m%(i+2)==0)returnfalse;returntrue;}
Cookie support is required to access HackerRank
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