![]() |
|
[C] Primes with the Sieve of Eratosthenes - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Primes with the Sieve of Eratosthenes (/showthread.php?tid=1798506) |
Primes with the Sieve of Eratosthenes - JJaeger - Oct 7, 2011 Hey all! I was bored today so I wrote a function that calculates all prime numbers between 2 and n. So I looked up a few ways to do it. There are quite alot options to calculate primes but I ended up with the Sieve of Eratosthenes. This option might not be the most efficient nor the easiest but it was fun to make. On my laptop it calculated the prime numbers between 0 and 100,000 in 5,042 milliseconds. I intentionally didn't use a STL container. If you got any comments just post them below. I hope someone can learn from this. code: Code #include <iostream>* - ohlawd - Oct 7, 2011 You should change it to use std::vector instead of dynamically creating an array, you could then remove each non-prime number rather than setting it to -1. edit: (Oct 7, 2011 01:56 PM)JJaeger Wrote: I intentionally didn't use a STL container. Sorry, didn't see this... * - JJaeger - Oct 7, 2011 I'm not sure but I think my way is faster then removing non prime numbers from vectors. Every time you remove one element it needs to shift the elements on the right to the left. I guess that will take a sh*tload of time. I also don't feel like writing one with vectors today but I got this code first: Code #include <iostream>It's ~150 ms slower on my laptop. * - ohlawd - Oct 7, 2011 (Oct 7, 2011 02:13 PM)JJaeger Wrote: I'm not sure but I think my way is faster then removing non prime numbers from vectors. Every time you remove one element it needs to shift the elements on the right to the left. I guess that will take a sh*tload of time. Yeah, std::list would actually be better, I've read you should use vector/list whenever possible instead of dynamically creating arrays in C++. Just safer I guess, oh well.🤝 edit: ^ that doesn't look right :P, you should be able to get it faster than that. * - JJaeger - Oct 7, 2011 Ye i know a STL container would be a better option but sometimes I'm in the mood to use dynamically allocated arrays. In a real project I probably would'nt have done it like this :p * - ohlawd - Oct 7, 2011 Well if you care, here is a nicer way to do it imo. Code #include <iostream>* - tflo - Oct 7, 2011 (Oct 7, 2011 02:13 PM)JJaeger Wrote: [code]#include <iostream> I like your style of importing the scope right after including the header, might have to jack that. 🙂 * - Navigator - Oct 8, 2011 JJaeger, your code is quite messy. Shodan, you're doing trial division and using that for sieving is very slow, additionally your IsPrime function is totally flawed. Also in order for Sieve of Eratosthenes to work you can't remove elements while the algorithm is working - if you find a prime you need to increment the array index by that prime to eliminate all multiples. Here's a clean way of doing it. Code #include <iostream>* - JJaeger - Oct 8, 2011 @Navigator, as stated in my first post I intentionally didn't use the STL. This because I like to do it on my own. In a real project I would've used the STL. If you got any cleaner solution without using the STL I'd like to see it. Thanks for your feedback, though. @Shodan, Navigator is right. Your code doesn't produce prime numbers. |