Advertisement
SteelGolem

finding primes up to 65535 part deux

Jul 30th, 2020
1,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static List<int> primes;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             primes = new List<int>();
  14.  
  15.             // build our primes list
  16.             int max = 65535;
  17.             for (int num = 2; num < max; num++)
  18.             {
  19.                 bool notPrime = false;
  20.                 for (int p = 0; p < primes.Count; p++)
  21.                     if (num % primes[p] == 0) { notPrime = true; break; }
  22.                 if (notPrime) continue;
  23.  
  24.                 primes.Add(num);
  25.                 Console.WriteLine(num + " is a prime.");
  26.             }
  27.  
  28.             Console.WriteLine("there were " + primes.Count + " primes up to " + max + ".");
  29.  
  30.             Console.WriteLine("IsPrime(9001) == " + IsPrime(9001));
  31.  
  32.             Console.ReadKey();
  33.         }
  34.  
  35.         static bool IsPrime(int value)
  36.         {
  37.             return primes.Contains(value);
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement