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.
I tend not to use external math formula because I am crap at math and I cannot google during the job interview. I see mentioning checking prefect square or Sieve method in the discussion comments. However I wrote simple code without those things and it passed the test easily. Here is what I considered to avoid timeout.
I used a list of prime numbers which is topped up on demand. Therefore prime number chekcing is performed only once per number. For this I add fillPrimes(nth) and call it when N exceeds the size of the list.
Plus, my isPrime() function uses sqrt() for the check. This is fundamental knowledge but very important for this kind of quiz. Otherwise you could be stuck in timeout.
skip even number. Small saving :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #7: 10001st prime
You are viewing a single comment's thread. Return to all comments →
I tend not to use external math formula because I am crap at math and I cannot google during the job interview. I see mentioning checking prefect square or Sieve method in the discussion comments. However I wrote simple code without those things and it passed the test easily. Here is what I considered to avoid timeout.
I used a list of prime numbers which is topped up on demand. Therefore prime number chekcing is performed only once per number. For this I add fillPrimes(nth) and call it when N exceeds the size of the list.
Plus, my isPrime() function uses sqrt() for the check. This is fundamental knowledge but very important for this kind of quiz. Otherwise you could be stuck in timeout.
skip even number. Small saving :)