Optimizing C++ prime tester
Hello,
I'm trying to solve this problem, but always get Time limit exceeded. I've tried my best but still can't get under time limit. Could you please give me some hint about it? Code:
#include <iostream> |
Maybe you could consider removing all white-spaces and newlines?
Seriously, I would remove the calls to cout that are in the for-loop. Store the prime numbers in an STL set. When you are done with the loop, then iterate through the set to print out the values. You should not judge the efficiency of your program if you plan to print to standard-out or to a file. |
Try using the Sieve of Atkins algorithm. It should be much faster than the trial division-based algorithm that you're currently using.
|
You should also think about all of the information you're ignoring. For example, all natural numbers except 1 have a prime factorization consisting of numbers less-than or equal-to that number. Therefore, if you know all primes below the number you're testing you only need to divide by those. I'm assuming that's why the problem doesn't have you go from 0 to X; it would be too easy to start at 0. You're on the right track with sqrt, though.
The problem you're solving is "tell me if this number is prime" millions of times. The problem you should be solving is "tell me all of the primes that fall within this range". Even before testing a single number you know that all multiples (>=2) of that number aren't prime. Additionally, if A fails the test when %B, you know that A+xB isn't prime for all integer x. I think this is more of a computer science problem than a math problem. There are a lot of ways to solve it mathematically, but you need to find one that cuts down both memory and number of redundant operations. Kevin Barry |
Thanks for your answers.
dugan: I'll take a look at that algorithm, but at first look it seems to be quite complicated to make. dwhitney67: Afaik, removing whitespaces etc. won't make any difference. I will try your idea with storing primes, but I'm not sure if won't do more bad than good, as that's one more loop. ta0kira: I tried to make a little upgrade, here it is: Code:
#include <iostream> The main difference is that it tests numbers ascending, so it should hit the most probable divisors sooner. (2,3 etc.). Also the new line after each "testing session" is now done in first loop - there are less conditions. However, I still can't get through it, and I really don't what to make better. Any further advice? Thanks. |
You should test only odd numbers:
Code:
#include <cmath> Code:
int is_prime(int n) |
Thanks for your reply.
I thought it would be enough to start testing with number 2, it should be the same as with the upgrade you posted. Quote:
|
Quote:
Code:
for (;fst_number<=lst_number;fst_number+=2) Markus |
Quote:
Kevin Barry |
Could you please post some symbolic pseudo code? I mean, the concept is really usable, but I doubt it's easy implementation / time requirement.
markush: Sorry, I overlooked that part/concept. I will update the code with that in mind. |
Quote:
Quote:
The fastest program for primes I ever wrote was with an array to store the (already found) prime-numbers and check new numbers only against them. If one uses pointers to run through this array one can learn much about C-programming. Markus |
Quote:
|
Quote:
Here is how I'd start:
PS Actually, if you made N=sqrt(end) you'd be done without 4-7. |
Alright, I made following update to code, it uses different way to get results, just as you guys suggested. It's not exactly what You said, Ta0kira, I'm working on it currently. Yet, I'd like to know why do I get SIGABRT signal for that code:
Code:
#include <iostream> |
I've tested your code, here it works.
Markus |
All times are GMT -5. The time now is 11:38 PM. |