Advertisement
OgreVorbis

Primes C#

Nov 10th, 2021 (edited)
1,518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 11/10/2021
  5.  * Time: 6:28 AM
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Diagnostics;
  11.  
  12. namespace SimplePrimes
  13. {
  14.     class Program
  15.     {
  16.         public static void Main(string[] args)
  17.         {
  18.             //bool[] nums;
  19.             byte[] nums;
  20.             int n, m, lim;
  21.             Stopwatch elapsed;
  22.             string yorn;
  23.            
  24.             Console.WriteLine("This program will search for prime numbers using a simple algorithm . . .");
  25.             Console.Write("Enter max limit # and press ENTER (leave blank for default): ");
  26.             lim = Convert.ToInt32("0" + Console.ReadLine());  // WE HAVE TO PUT THE 0 IN FRONT TO FORCE STUPID C# TO NOT HAVE ERROR IF INPUT IS BLANK...
  27.             // C# SHOULD RETURN 0 FOR A BLANK STRING AND ONLY EXCEPTION ON NULL STRING, BUT I DIDN'T DESIGN THIS :P
  28.             if (lim == 0)
  29.                 lim = 100000000;
  30.            
  31.             elapsed = new Stopwatch();
  32.             elapsed.Start();
  33.             //nums = new bool[lim + 1];
  34.             nums = new byte[lim + 1];
  35.            
  36.             for (n = 2; n < Math.Sqrt(lim); n++)
  37.             {
  38.                 if (nums[n] == 0)
  39.                 {
  40.                     m = n * n;
  41.                     while (m <= lim)
  42.                     {
  43.                         nums[m] = 1;
  44.                         m += n;
  45.                     }
  46.                 }
  47.             }
  48.            
  49.             elapsed.Stop();
  50.             Console.WriteLine("It took " + (elapsed.ElapsedMilliseconds / 1000.0).ToString() + " seconds to complete.");
  51.             Console.Write("Do you want to print the results (Y/N)? ");
  52.             yorn = Console.ReadLine();
  53.            
  54.             if (yorn == "Y" || yorn == "y")
  55.             {
  56.                 m = 0;
  57.                 for (n = 2; n <= lim; n++)
  58.                 {
  59.                     if(nums[n] == 0)
  60.                     {
  61.                         Console.WriteLine(n.ToString());
  62.                         m++;
  63.                     }
  64.                 }
  65.                 Console.WriteLine("Total primes = " + m.ToString());
  66.                 Console.WriteLine("");
  67.                 Console.Write("Press any key to end . . . ");
  68.                 Console.ReadKey(true);
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement