Can I ask a dumb question?🤓 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can I ask a dumb question?🤓

So I was making a code to print prime numbers and get the sum of them and it was working just fine and everything and it prints all the prime numbers, each one individually ( I needed the sum of all prime numbers less than 1 million) It was going just fine, till it stopped at 224,197 so is that a problem with the compiler ( I mean do they have a limit with numbers ?) If anyone knows about this or why did it stop please tell me and thanks in advance 🙌🏻 Ps ( I’m coding with C)

18th Jun 2020, 11:24 PM
Rowan
5 Answers
+ 5
Look into these algorithms. While yours is fine for small numbers, larger numbers will have a very bad time complexity. Also, the use of int for sum could be causing overflow issues. This code won't finish running in the playground and may have issues in other online compilers as well due to the time it takes the loops to run. https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html https://www.google.com/amp/s/www.geeksforgeeks.org/sieve-of-eratosthenes/amp/ Also, research memoization.
18th Jun 2020, 11:56 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Can you share your code? Are you attempting to run this code here in the playground or elsewhere? You may be timing out if running here for larger prime number, depending on how you're obtaining them in your program. Also, when summing the primes, what variable type are you using? An int may not have a large enough upper bounds when summing and may overflow.
18th Jun 2020, 11:35 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
aha okay, thank you so much for your help !🤍
19th Jun 2020, 12:28 AM
Rowan
0
I usually use codeblocks but i tried this code with an online compiler so i don’t really know about the timing
18th Jun 2020, 11:39 PM
Rowan