Ejemplo n.º 1
0
        static IntSet getRow(int row)
        {
            IntSet newIntset = new IntSet(board[row]);

            newIntset.Excl(0);
            return(newIntset);
        }
Ejemplo n.º 2
0
    public static void Main(string [] args)
    {
        const int Max = 32000;
        IntSet    uncrossed = new IntSet(Max);         // the sieve //bool[] uncrossed = new bool[Max];
        int       i, n, k, it, iterations, primes = 0; // counters

        IO.Write("How many iterations? ");
        iterations = IO.ReadInt();
        bool display = iterations == 1;

        IO.Write("Supply largest number to be tested ");
        n = IO.ReadInt();
        if (n > Max)
        {
            IO.Write("n too large, sorry");
            System.Environment.Exit(1);
        }
        IO.WriteLine("Prime numbers between 2 and " + n);
        IO.WriteLine("-----------------------------------");
        for (it = 1; it <= iterations; it++)
        {
            primes = 0;
            for (i = 2; i <= n; i++)                // clear sieve
            {
                uncrossed.Incl(i);                  //uncrossed[k] = true;
            }
            for (i = 2; i <= n; i++)                // the passes over the sieve
            {
                if (uncrossed.Contains(i))          //uncrossed[i]
                {
                    if (display && primes % 8 == 0) // ensure line not too long
                    {
                        IO.WriteLine();
                    }
                    primes++;
                    if (display)
                    {
                        IO.Write(i, 6);
                    }
                    k = i;                     // now cross out multiples of i
                    do
                    {
                        uncrossed.Excl(k); //uncrossed[k] = false;
                        k += i;
                    } while (k <= n);
                }
            }
            if (display)
            {
                IO.WriteLine();
            }
        }
        IO.Write(primes + " primes");
    } // main
Ejemplo n.º 3
0
    public static void Main(string[] args)
    {
        const int Max = 32000;
        IntSet    uncrossed = new IntSet(Max);
        int       i, limit, k, it, iterations, primes = 0; // counters

        IO.Write("How many iterations? ");
        iterations = IO.ReadInt();
        bool display = true;

        IO.Write("Supply largest number to be tested ");
        limit = IO.ReadInt();
        if (limit > Max)
        {
            IO.Write("limit too large, sorry");
            System.Environment.Exit(1);
        }
        for (it = 1; it <= iterations; it++)
        {
            primes = 0;
            for (i = 2; i <= limit; i++)
            {
                // clear sieve
                uncrossed.Incl(i);
            }
            for (i = 2; i <= limit; i++)
            {               // the passes over the sieve
                if (uncrossed.Contains(i) == true)
                {
                    primes++;
                    k = i;                               // now cross out multiples of i
                    do
                    {
                        uncrossed.Excl(k);
                        k += i;
                    } while (k <= limit);
                }
            }
        }
        uncrossed.Incl(1);
        IntSet primeSet = new IntSet(uncrossed);

        IO.WriteLine("here");

        for (i = 2; i <= limit; i++)                 // clear sieve
        {
            uncrossed.Incl(i);
        }
        for (i = 3; i <= limit; i += 2)              // exclude reference to odd integers
        {
            uncrossed.Excl(i);
        }
        for (i = 4; i <= limit; i += 2)
        {               // the passes over the sieve in even numbers greater than 4
            if (uncrossed.Contains(i) == true)
            {
                for (int a = 1; a <= limit; a++)
                {
                    for (int b = 1; b <= limit; b++)
                    {
                        if (primeSet.Contains(a) && primeSet.Contains(b))
                        {
                            IO.WriteLine(a);
                            IO.WriteLine(b);
                            IO.WriteLine(i);
                            if (a + b == i)
                            {
                                IO.WriteLine("N = A + B: -> " + i.ToString() + " = " + a.ToString() + " + " + b.ToString());
                            }
                        }
                    }
                }
            }
            if (display)
            {
                IO.WriteLine();
            }
        } // main
    }