Advertisement
SteelGolem

finding primes up to 65535

Jul 30th, 2020
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // 1 2 3 are primes
  12.             int numPrimes = 3;
  13.  
  14.             for (int num = 4; num < 65535; num++)
  15.             {
  16.                 if (IsPrime(num)) { Console.WriteLine(num + " is a prime number."); numPrimes++; }
  17.             }
  18.             Console.WriteLine("there were " + numPrimes + " primes.");
  19.             Console.ReadKey();
  20.         }
  21.  
  22.         static bool IsPrime(int value)
  23.         {
  24.             for (int i = 2; i < value - 1; i++) if (value % i == 0) return false;
  25.  
  26.             return true;
  27.         }
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement