Alice and Bob's Silly Game

  • + 1 comment

    Your algorithm has O(n^1.5) complexity which roughly translates to an order of 10^10 operations when g=1000 and n=100000. As mentioned in other comments, you need to use Sieve of Eratosthenes to improve the time complextiy. As a further improvemnt, you can store pre-calculated sieve and use that across all inputs.