private static (bool success, double ms) GetMsOfFastAlg(BigInteger n) { var multithreadLenstra = new MultithreadLenstra <ProjectiveEdwardsLenstra>(); var stopWatch = new Stopwatch(); stopWatch.Start(); var divider = multithreadLenstra.LenstraMultiThreadFastResult(n, Environment.ProcessorCount); stopWatch.Stop(); return(divider.HasValue, stopWatch.ElapsedMilliseconds); }
/// <summary> Факторизация числа на простые делители </summary> /// <param name="n">Факторизуемое число</param> /// <typeparam name="TLenstra">Сервис, реализующий алгоритм Ленстры</typeparam> /// <returns>Словарь, содержащий разложение по степеням</returns> public static Dictionary <BigInteger, int> Factorize <TLenstra>(BigInteger n) where TLenstra : ILenstra, new() { _pR = new Dictionary <BigInteger, int>(); if (IsPrimaryMillerRabin(n)) { _pR.Add(n, 1); return(_pR); } foreach (var i in new BigInteger[] { 2, 3, 5, 7, 11 }) { if (n % i == 0) { _pR.Add(i, 1); n /= i; } } foreach (var i in new BigInteger[] { 2, 3, 5, 7, 11 }) { while (n % i == 0) { _pR[i]++; n /= i; } } if (IsPrimaryMillerRabin(n)) { _pR.Add(n, 1); return(_pR); } try { var multithreadLenstra = new MultithreadLenstra <TLenstra>(); FactorizeInner(n, multithreadLenstra); } catch (Exception e) { Console.WriteLine("Проблема при факторизации числа "); Console.WriteLine(n); Console.WriteLine(e); throw; } return(_pR); }
private static void Test8() { Console.WriteLine("AffineEdwardsLenstra results: "); var n = BigInteger.Parse("73928303") * BigInteger.Parse("73928293"); var multThreadLenstra = new MultithreadLenstra <AffineEdwardsLenstra>(); var stopWatch = new Stopwatch(); stopWatch.Start(); var res = multThreadLenstra.LenstraMultiThreadFastResult(n, 160); stopWatch.Stop(); Console.WriteLine(res); Console.WriteLine("Elapsed ms: " + stopWatch.ElapsedMilliseconds); }
private static void ProcessFiles <TLenstra>(List <string> semiprimesFiles, string dataDir, string version) where TLenstra : ILenstra, new() { var mtl = new MultithreadLenstra <TLenstra>(); if (version == ContinuationInfo.LenstraVersion && !string.IsNullOrWhiteSpace(ContinuationInfo.ProcessingFileName)) { semiprimesFiles = semiprimesFiles .SkipWhile(fileName => fileName != ContinuationInfo.ProcessingFileName) .ToList(); } var lenstraVersion = typeof(TLenstra).Name; foreach (var semiprimesFile in semiprimesFiles) { File.WriteAllText(ContinuationInfoStorePath, lenstraVersion + "|" + semiprimesFile); var fileName = Path.GetFileName(semiprimesFile); var path = Path.Combine(dataDir, lenstraVersion, fileName ?? throw new FileNotFoundException()); var semiprimes = File.ReadAllLines(semiprimesFile).Take(CountForDimension).Select(BigInteger.Parse) .ToArray(); if (version == ContinuationInfo.LenstraVersion && ContinuationInfo.ProcessingFileName == semiprimesFile) { semiprimes = semiprimes.Skip(ContinuationInfo.LastProcessedLine).ToArray(); } double k = 0; foreach (var semiprime in semiprimes) { Console.Write("\rHandling " + semiprime + ". Processed " + (k / semiprimes.Length).ToString("P") + " of " + fileName); k++; var results = mtl.LenstraMultiThreadResults(semiprime, CurvesCountForSingleNumber); var infos = results .Select(result => $"{result.TargetNumber}|{result.Divider}|{result.WastedTime.Ticks}") .ToArray(); File.AppendAllLines(path, infos); } } }
private static void FactorizeInner <TLenstra>(BigInteger n, MultithreadLenstra <TLenstra> multithreadLenstra) where TLenstra : ILenstra, new() { if (n == BigInteger.One) { return; } if (IsPrimaryMillerRabin(n)) { _pR.Add(n, 1); return; } var result = multithreadLenstra.LenstraMultiThreadFastResult(n, 160); if (result != null) { var divider = result.Value; if (IsPrimaryMillerRabin(divider)) { _pR.Add(divider, 1); n /= divider; while (n % divider == 0) { _pR[divider]++; n /= divider; } FactorizeInner(n, multithreadLenstra); } else { FactorizeInner(n / divider, multithreadLenstra); FactorizeInner(divider, multithreadLenstra); } } }