Пример #1
0
    public static void Main()
    {
        int    limit;
        Primes obj  = new Primes();
        bool   stan = true;

        while (stan)
        {
            obj.Reset();
            Console.WriteLine("Aby wypisać liczby pierwsze, wpisz 1");
            Console.WriteLine("Aby zakończyć program, podaj liczbę 0");

            limit = Int32.Parse(Console.ReadLine());

            if (limit == 0)
            {
                stan = false;
                continue;
            }

            limit = 10000;

            PrimeCollection pc = new PrimeCollection(limit);
            Console.WriteLine("");
            foreach (int p in pc)
            {
                Console.WriteLine(p);
            }
        }
    }
Пример #2
0
    static void Main()
    {
        PrimeCollection pc = new PrimeCollection();

        foreach (int p in pc)
        {
            Console.WriteLine(p);
        }
        var enumerator = pc.GetEnumerator();

        Console.WriteLine("current: {0}", enumerator.Current);
        int i;

        for (i = 0; i < 20; i++)
        {
            enumerator.MoveNext();
            Console.WriteLine("{1} prime: {0}", enumerator.Current, i + 1);
        }
        Console.WriteLine("\nreset enumerator\n");
        enumerator.Reset();
        for (i = 0; i < 10; i++)
        {
            enumerator.MoveNext();
            Console.WriteLine("{0} prime {1}", i + 1, enumerator.Current);
        }
    }
Пример #3
0
    static void Main(string[] args)
    {
        PrimeCollection pc = new PrimeCollection();

        foreach (int p in pc)
        {
            System.Console.WriteLine(p);
        }
    }
Пример #4
0
    public static void Main()
    {
        PrimeCollection pc = new PrimeCollection();

        foreach (int p in pc)
        {
            Console.WriteLine(p);
        }
    }
    static void Main()
    {
        PrimeCollection A = new PrimeCollection();

        foreach (int e in A)
        {
            System.Console.WriteLine(e);
        }
    }
Пример #6
0
    public static void Main()
    {
        int             max_int = Convert.ToInt32(Console.ReadLine());
        PrimeCollection X       = new PrimeCollection(max_int);

        foreach (int x in X)
        {
            Console.WriteLine("{0}", x);
        }
    }
Пример #7
0
        /// <summary>
        /// Computes the Product of the prime numbers in the given sieveRange.
        /// </summary>
        /// <param name="range">The sieveRange of the enumeration.</param>
        /// <returns>The Product of the prime numbers in the enumeration.
        /// </returns>
        public XInt GetPrimorial(PositiveRange range)
        {
            int start, size;
            var pc = new PrimeCollection(this, range);

            if (pc.GetSliceParameters(out start, out size))
            {
                return(XInt.One);
            }

            return(XMath.Product(this.primes, start, size));
        }
Пример #8
0
            /// <summary>
            /// Provides an enumerator of the current prime number collection.
            /// This enumerator is thread save.
            /// </summary>
            /// <returns>An enumerator of the current prime number collection.
            /// </returns>
            IEnumerator <int> IEnumerable <int> .GetEnumerator()
            {
                PrimeCollection result = this;

                // Make the Enumerator threadsave!
                if (0 != Interlocked.CompareExchange(ref this.state, 1, 0))
                {
                    result = new PrimeCollection(this.sieve, this.enumRange)
                    {
                        state = 1
                    };
                }

                result.next = result.start;
                return(result);
            }
Пример #9
0
        /// <summary>
        /// Computes the Product of the prime numbers in the given sieveRange
        /// primes[start]*primes[start+increment]*primes[start+2*increment]*...
        /// </summary>
        /// <param name="low">Lower bound of the sieveRange of the enumeration.</param>
        /// <param name="high">Higher bound of the sieveRange of the enumeration.</param>
        /// <returns>The Product of the prime numbers in the enumeration.</returns>
        public XInt GetPrimorial(int low, int high, int increment)
        {
            if (increment == 1)
            {
                return(this.GetPrimorial(new PositiveRange(low, high)));
            }

            var range = new PositiveRange(low, high);
            var pc = new PrimeCollection(this, range, increment);
            int start, size;

            if (pc.GetSliceParameters(out start, out size))
            {
                return(XInt.One);
            }

            return(XMath.Product(this.primes, start, size, increment));
        }
Пример #10
0
    // Funkcja Main
    public static void Main(string[] args)
    {
        PrimeCollection primes = new PrimeCollection();

        // Jeżeli użytkownik nie poda limitu liczb, to wygenerowane
        // zostanš wszystkie liczby pierwsze mieszczšce się w zmiennej
        // typu int (32 bitowej).
        int limit = Int32.MaxValue;

        // Sprawdzanie, czy użytkownik podał limit, do którego będš
        // wyznaczane kolejne liczby pierwsze.
        if (args.Length > 0)
        {
            // Sprawdzanie, czy podany argument jest poprawnš liczbš całkowitš.
            if (!Int32.TryParse(args[0], out limit))
            {
                Console.WriteLine("Given command line argument is invalid!");
                return;
            }
            // Wypisywanie komunikatu z błędem, jeżeli nie istniejš liczby
            // pierwsze mniejsze niż zadany limit.
            else if (limit <= 2)
            {
                Console.WriteLine("There are no prime numbers smaller than 2!");
                return;
            }
        }

        // Przebieganie przez kolekcję i wyznaczanie kolejnych liczb.
        // Jeżeli nowo wyznaczona liczba przekracza podany limit, to
        // pętla zostaje przerwana.
        foreach (var prime in primes)
        {
            if (prime >= limit)
            {
                break;
            }
            Console.WriteLine("{0}", prime);
        }
    }
Пример #11
0
    public static void Main()
    {
        int    limit = 0;
        Primes obj   = new Primes();
        bool   stan  = true;

        while (stan)
        {
            obj.Reset();
            Console.WriteLine("");
            Console.WriteLine("Podaj, do jakiej liczby wypisywac liczby pierwsze");
            Console.WriteLine("Zero, jesli chcesz opuscic program");
            try
            {
                limit = Int32.Parse(Console.ReadLine());
            }
            catch (OverflowException)
            {
                Console.WriteLine("Liczba za duza");
                continue;
            }
            catch (FormatException)
            {
                Console.WriteLine("Bledne dane");
                continue;
            }
            if (limit == 0)
            {
                stan = false;
                continue;
            }
            PrimeCollection pc = new PrimeCollection(limit);
            Console.WriteLine("");
            foreach (int p in pc)
            {
                Console.WriteLine(p);
            }
        }
    }