Advertisement
Guest User

SieveOfEratosthenes

a guest
Dec 20th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. //Write a program that finds all prime numbers in the range [1...10 000 000]. Use the sieve of Eratosthenes algorithm (find it in Wikipedia).
  5.  
  6. class SieveOfEratosthenes
  7. {
  8.     static void Main()
  9.     {
  10.         List<int> Primes = new List<int>();
  11.  
  12.         for (int i = 0; i <= 10000000; i++)
  13.         {
  14.             if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0)
  15.             {
  16.                 Primes.Add(i);
  17.             }
  18.         }
  19.         Console.WriteLine("Print all Primes between 1 and 10 000 000 in array ({0} prime numbers):",Primes.Count);
  20.         for (int i = 0; i < Primes.Count; i++)
  21.         {
  22.            Console.WriteLine(Primes[i]);        
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement