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.
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.
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 →
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.