![]() |
| ||
| How to optimize this code further for prime numbers Could there be any better ways to determine whether a number is prime or not particularly with respect to code execution. #include <iostream.h> |
| ||
| Re: How to optimize this code further for prime numbers Hi lets start with code correctness in relationship to standard c++ and improve some parts too :D #include <iostream> Keep in mind that important for u is to decrease the execution time of the PrimeTest function. When u included there the cout statements, u added extra overhead of msecs through these into the function. So if u place the call to your PrimeTest within a for loop and repeat the call for 1000 times u will notice a relatively big difference (if u r concerned for msecs time) when including those cout statements within the function. for example try: for (int k=0;k<1000;++k)with the code i posted and with yours: for (int k=0;k<1000;++k)and compare |
| ||
| Re: How to optimize this code further for prime numbers Quote:
|
| ||
| Re: How to optimize this code further for prime numbers One more think u can do is to apply Wheel factorization based on the Sieve of Eratosthenes. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Here an implementation for sbdy: an Implemenattion or use the Sieve of Atkin depending on the total amount of prime numbers that u want to calculate: http://en.wikipedia.org/wiki/Sieve_of_Atkin |
| All times are GMT -4. The time now is 7:28 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC