this algorithm has been my lit. secret... I've never been able to see
if there is another that beat it.

CPU: Intel Pentiun 4 2.26GHz
Cores: 1
RAM: 768MB
OS: Windows XP Professional SP3
.Net: 3.5
Number of primes in 10 seconds: 168876
Highest Prime found: 1078673

It's deterministic, it gets all the primes, no skips, all in order.
Note: running without looking the console to get its real speed, if
you are looking it, it falls a lot. [Hey the DateTime.Now call also
takes time!!!]

stats watching the console:
Number of primes in 10 seconds: 59082 (34% aprox)
Highest Prime found: 361219

I'm pretty sure I can optimize it, but I haven't had the need.
I'm soooo... nostalgic to give this away... ok, here I go!

using System;
using System.Diagnostics;

namespace Primes
{
    class Program
    {
        const int seconds = 10;
        [DebuggerNonUserCode()]
        static bool IsPrim(double Num)
        {
            if (Num == 2 ||Num == 3) return true;
                if (Num % 2 == 0 || Num % 3 == 0) return false;
            double J, H = Math.Ceiling(Math.Sqrt(Num));
                for (J = 5; J <= H; J+=6) if (Num % J == 0 || Num % J+2 == 0)
return false;
                return true;
        }
        [DebuggerNonUserCode()]
        static void Main()
        {
            DateTime x, y = DateTime.Now;
            double a = 1, b = 0;
            do
            {
                if (IsPrim(a))
                {
                    Console.WriteLine(a);
                    b++;
                }
                a++;
                x = DateTime.Now;
            } while ((x - y).TotalSeconds < seconds);
            Console.WriteLine("Done");
            Console.WriteLine("{0} primes founds in {1} seconds", b,
seconds);
            Console.ReadKey();
        }
    }
}

@Cerberus:
Sieve of Eratosthenes isn't appropiate for this, sorry.

Reply via email to