TheQuack45

circularPrimes 2/24

Feb 24th, 2016
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. namespace circularPrimes
  9. {
  10.     class Program
  11.     {
  12.         /*static void Main(string[] args)
  13.         {
  14.             int primesCount = 0;
  15.  
  16.             Console.WriteLine("Input p: ");
  17.             int p = Int32.Parse(Console.ReadLine());
  18.  
  19.             //Console.WriteLine((!fermatIsPrime(p)).ToString());
  20.  
  21.             for (int i = 2; i < p; i += 2)
  22.             {
  23.                 if (!fermatIsPrime(i))
  24.                 {
  25.                     primesCount++;
  26.                 }
  27.             }
  28.  
  29.             Console.WriteLine(primesCount);
  30.  
  31.             Console.ReadKey();
  32.         }*/
  33.  
  34.         static void Main(string[] args)
  35.         {
  36.             /*Console.WriteLine("Number to check: ");
  37.             int p = Int32.Parse(Console.ReadLine());
  38.  
  39.             Console.WriteLine("Number is prime: " + isPrime(p));
  40.  
  41.             Console.ReadKey();*/
  42.  
  43.             int count = 2;
  44.  
  45.             Console.WriteLine("number to count primes to: ");
  46.             int n = Int32.Parse(Console.ReadLine());
  47.  
  48.             for (int i = 5; i < n; i++)
  49.             {
  50.                 if (isPrime(i))
  51.                 {
  52.                     Console.WriteLine(i);
  53.                     count++;
  54.                 }
  55.             }
  56.  
  57.             Console.WriteLine("count: " + count);
  58.  
  59.             Console.ReadKey();
  60.         }
  61.  
  62.         public static bool isPrime(int p)
  63.         {
  64.             if (p == 1 || p == 2)
  65.             {
  66.                 return true;
  67.             }
  68.             else if (p % 2 == 0 || p % 5 == 0)
  69.             {
  70.                 return false;
  71.             }
  72.             else
  73.             {
  74.                 if (fermatIsPrime(p))
  75.                 {
  76.                     // Number is possibly prime
  77.                     // Verify
  78.                     for (int i = 3; i <= Math.Truncate(Math.Sqrt(p)); i++)
  79.                     {
  80.                         // Loop as far as is necessary
  81.                         if (p % i == 0)
  82.                         {
  83.                             // Number is divisible by another number
  84.                             // Number is not prime
  85.                             return false;
  86.                         }
  87.                     }
  88.                    
  89.                     // All tests have failed
  90.                     // Number is prime
  91.                     return true;
  92.                 }
  93.                 else
  94.                 {
  95.                     // Number is not possibly prime according to Fermat's theorem
  96.                     return false;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         public static int powerMod(int b, int e, int m)
  102.         {
  103.             int x = 1;
  104.             while (e > 0)
  105.             {
  106.                 if (e % 2 == 1)
  107.                 {
  108.                     x = (x * b) % m;
  109.                     e -= 1;
  110.                 }
  111.                 else
  112.                 {
  113.                     b = (b * b) % m;
  114.                     e = e / 2;
  115.                 }
  116.             }
  117.  
  118.             return x;
  119.         }
  120.  
  121.         public static bool fermatIsPrime(int p)
  122.         {
  123.             BigInteger intermediateValue = new BigInteger();
  124.             BigInteger pBigInt = new BigInteger(p);
  125.             BigInteger a = new BigInteger();
  126.             Random randomGen = new Random();
  127.             //int a = 0;
  128.             for (int i = 0; i < p * .2; i++)
  129.             {
  130.                 // Loop for amount of times equal to 20% of the number
  131.                 //a = randomGen.Next(3, p - 1);
  132.  
  133.                 /* if (Math.Pow(a, p - 1) % p != 1)
  134.                 {
  135.                     // Number is not prime
  136.                     return false;
  137.                 } */
  138.  
  139.  
  140.  
  141.                 intermediateValue = intermediateValue.ModPow(a, pBigInt - 1, pBigInt);
  142.                
  143.                 // Number is probably prime
  144.             }
  145.  
  146.             // Number is PROBABLY prime
  147.             // Must be checked to be sure
  148.             return true;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment