Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Numerics;
- namespace circularPrimes
- {
- class Program
- {
- /*static void Main(string[] args)
- {
- int primesCount = 0;
- Console.WriteLine("Input p: ");
- int p = Int32.Parse(Console.ReadLine());
- //Console.WriteLine((!fermatIsPrime(p)).ToString());
- for (int i = 2; i < p; i += 2)
- {
- if (!fermatIsPrime(i))
- {
- primesCount++;
- }
- }
- Console.WriteLine(primesCount);
- Console.ReadKey();
- }*/
- static void Main(string[] args)
- {
- /*Console.WriteLine("Number to check: ");
- int p = Int32.Parse(Console.ReadLine());
- Console.WriteLine("Number is prime: " + isPrime(p));
- Console.ReadKey();*/
- int count = 2;
- Console.WriteLine("number to count primes to: ");
- int n = Int32.Parse(Console.ReadLine());
- for (int i = 5; i < n; i++)
- {
- if (isPrime(i))
- {
- Console.WriteLine(i);
- count++;
- }
- }
- Console.WriteLine("count: " + count);
- Console.ReadKey();
- }
- public static bool isPrime(int p)
- {
- if (p == 1 || p == 2)
- {
- return true;
- }
- else if (p % 2 == 0 || p % 5 == 0)
- {
- return false;
- }
- else
- {
- if (fermatIsPrime(p))
- {
- // Number is possibly prime
- // Verify
- for (int i = 3; i <= Math.Truncate(Math.Sqrt(p)); i++)
- {
- // Loop as far as is necessary
- if (p % i == 0)
- {
- // Number is divisible by another number
- // Number is not prime
- return false;
- }
- }
- // All tests have failed
- // Number is prime
- return true;
- }
- else
- {
- // Number is not possibly prime according to Fermat's theorem
- return false;
- }
- }
- }
- public static int powerMod(int b, int e, int m)
- {
- int x = 1;
- while (e > 0)
- {
- if (e % 2 == 1)
- {
- x = (x * b) % m;
- e -= 1;
- }
- else
- {
- b = (b * b) % m;
- e = e / 2;
- }
- }
- return x;
- }
- public static bool fermatIsPrime(int p)
- {
- BigInteger intermediateValue = new BigInteger();
- BigInteger pBigInt = new BigInteger(p);
- BigInteger a = new BigInteger();
- Random randomGen = new Random();
- //int a = 0;
- for (int i = 0; i < p * .2; i++)
- {
- // Loop for amount of times equal to 20% of the number
- //a = randomGen.Next(3, p - 1);
- /* if (Math.Pow(a, p - 1) % p != 1)
- {
- // Number is not prime
- return false;
- } */
- intermediateValue = intermediateValue.ModPow(a, pBigInt - 1, pBigInt);
- // Number is probably prime
- }
- // Number is PROBABLY prime
- // Must be checked to be sure
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment