Advertisement
TSorbera

Untitled

Jan 19th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using Mpir.NET;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MersenneHint
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var logFactor = Math.Log10(2);
  14.             int i = 0;
  15.             var primes = GetPrimes(74000000, 75000000);
  16.             var sw = Stopwatch.StartNew();
  17.             Parallel.ForEach(primes, p =>
  18.             {
  19.                 lock(lockObj) i++;
  20.                 if (i % 100 == 0)
  21.                     Console.WriteLine(sw.Elapsed + ": " + i);
  22.                 var digits = (int)Math.Floor(p * logFactor) + 1;
  23.                 using (var tenPowerEnd = mpz_t.Ten.Power(digits - 19391387 + 1))
  24.                 using (var twoPowerMod = mpz_t.Two.PowerMod(p, tenPowerEnd))
  25.                 using (var powMinOne = twoPowerMod - 1)
  26.                 using (var tenPowerStart = mpz_t.Ten.Power(digits - 19391387 + 1 - 7))
  27.                 using (var div = powMinOne.Divide(tenPowerStart))
  28.                 {
  29.                     var mpSecStr = div.ToString();
  30.                     if (mpSecStr == "2718281")
  31.                         Console.WriteLine(p);
  32.                 }
  33.             });
  34.             Console.WriteLine("done");
  35.             Console.ReadLine();
  36.         }
  37.         static readonly object lockObj = new object();
  38.         static IList<int> GetPrimes(int from, int to)
  39.         {
  40.             var primes = new List<int>();
  41.             for (mpz_t i = new mpz_t(from).NextPrimeGMP(); i <= to; i = i.NextPrimeGMP())
  42.             {
  43.                 if (i.ToString().Contains("42") && !alreadyFactored.Contains((int)i))
  44.                     primes.Add((int)i);
  45.             }
  46.             return primes;
  47.         }
  48.  
  49.         static readonly HashSet<int> alreadyFactored = new HashSet<int> { ..... }; // pastebin doesn't like this crazy long list, fill it in yourself from http://www.mersenne.org/report_factors/
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement