![]() |
|
[C#] Calculate primes - 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#] Calculate primes (/showthread.php?tid=3889750) |
Calculate primes - maarten1012 - Nov 26, 2013 Hey, I was bored today, so i made a little program that calculates primes.. it works, but my question is how can i make it more efficient? Here is the source: Code class ProgramThanks in advance! * - Ban - Nov 26, 2013 Here, quick modification of yours: http://pastebin.com/KUTjX1Nr By the way, .Equals() tests equality and == tests reference. .Equals can be slower however it can also be far more effective than == when comparing objects. Practically there really is no difference since it's a defined type and being someone who also programs in Java, it's just a little more natural for me to see .Equals. I do it both ways however. Just letting you know. ^_^ * - 0xDEAD10CC - Nov 26, 2013 None of these are good at all. Here is a few pointers: 1. No primes below 2. 2. 2 is the only even prime number, you shouldnt have to check even numbers other than 2 for being prime. 3. There are better algorithms out there for generating primes than just a simple looping through numbers in the x-y range strategy. Here is my prime function: Code static bool Prime(int n)But for a more efficient method of generating primes, I would do something like the following: Code static void GenSieveAtkin(int max, ref List<int> primes) |