示例#1
0
        private BigInteger GetHigherPrime(BigInteger value)
        {
            if (value < Consts.BI2P64)
            {
                ulong ret = Prime53.GetHigherPrime(Common.ToULong(value));

                if (ret != 0)
                {
                    return(ret);
                }

                //value = Consts.BI2P64 - 1;
                value = Consts.BI2P64;                 // 2^64 is not prime
            }
            for (; ;)
            //while (value < Consts.BI2P4096_1) // 2^4096-1 is not prime
            {
                value++;

                if (PrimeUtils.IsPrime(value))
                {
                    return(value);
                }

                if (Ground.IsStopped())
                {
                    break;
                }
            }
            return(0);
        }
示例#2
0
        private static BigInteger GetPrimeCount_BIBI(BigInteger minval, BigInteger maxval, Func <double, double> rateFltr)
        {
            BigInteger count = 0;

            for (BigInteger value = minval; ; value++)
            {
                if (PrimeUtils.IsPrime(value))
                {
                    count++;

                    {
                        int    permil = (int)(((value - minval) * 1000) / (maxval - minval));
                        double rate   = permil / 1000.0;

                        Common.Report(rateFltr(rate), value);
                    }

                    GC.Collect();
                }
                if (value == maxval)
                {
                    break;
                }

                if (Ground.IsStopped())
                {
                    break;                     // 中止
                }
            }
            return(count);
        }
示例#3
0
        private static void FindPrimes_BIBI(BigInteger minval, BigInteger maxval, string outFile, Func <double, double> rateFltr)
        {
            using (FileStream writer = new FileStream(outFile, FileMode.Append, FileAccess.Write))
            {
                for (BigInteger value = minval; ; value++)
                {
                    if (PrimeUtils.IsPrime(value))
                    {
                        FileTools.Write(writer, Encoding.ASCII.GetBytes(Common.ToString(value)));
                        writer.WriteByte(0x0a);                         // '\n'

                        {
                            int    permil = (int)(((value - minval) * 1000) / (maxval - minval));
                            double rate   = permil / 1000.0;

                            Common.Report(rateFltr(rate), value);
                        }

                        GC.Collect();
                    }
                    if (value == maxval)
                    {
                        break;
                    }

                    if (Ground.IsStopped())
                    {
                        FileTools.Write(writer, Encoding.ASCII.GetBytes("ABORTED - Prime4096\n"));
                        break;                         // 中止
                    }
                }
            }
        }
示例#4
0
        private BigInteger GetLowerPrime(BigInteger value)
        {
            while (Consts.BI2P64 <= value)
            {
                value--;

                if (PrimeUtils.IsPrime(value))
                {
                    return(value);
                }

                if (Ground.IsStopped())
                {
                    return(0);
                }
            }
            return(Prime53.GetLowerPrime(Common.ToULong(value)));
        }
示例#5
0
        private void Main3(ArgsReader ar)
        {
            Console.WriteLine("Prime4096_MillerRabin_K: " + Ground.MillerRabin_K);             // test

            if (ar.ArgIs("/S"))
            {
                Ground.Stop();
                return;
            }
            if (ar.ArgIs("/P"))
            {
                string sn      = ar.NextArg();
                string outFile = ar.NextArg();

                Console.WriteLine("IsPrime_sn: " + sn);

                File.WriteAllText(outFile, PrimeUtils.IsPrime(Common.ToBigInteger(sn)) ? "P" : "N", Encoding.ASCII);
                return;
            }
            if (ar.ArgIs("/F"))
            {
                string sn      = ar.NextArg();
                string outFile = ar.NextArg();

                Console.WriteLine("Factorization_sn: " + sn);

                FactorizationUtils.Factorization(Common.ToBigInteger(sn), outFile);
                return;
            }
            if (ar.ArgIs("/L"))
            {
                string sn      = ar.NextArg();
                string outFile = ar.NextArg();

                Console.WriteLine("GetLowerPrime_sn: " + sn);

                File.WriteAllText(outFile, Common.ToString(
                                      GetLowerPrime(
                                          Common.ToBigInteger(sn)
                                          )
                                      ),
                                  Encoding.ASCII
                                  );
                return;
            }
            if (ar.ArgIs("/H"))
            {
                string sn      = ar.NextArg();
                string outFile = ar.NextArg();

                Console.WriteLine("GetHigherPrime_sn: " + sn);

                File.WriteAllText(outFile, Common.ToString(
                                      GetHigherPrime(
                                          Common.ToBigInteger(sn)
                                          )
                                      ),
                                  Encoding.ASCII
                                  );
                return;
            }
            if (ar.ArgIs("/R"))
            {
                string sn1     = ar.NextArg();
                string sn2     = ar.NextArg();
                string outFile = ar.NextArg();

                Console.WriteLine("FindPrimes_sn1: " + sn1);
                Console.WriteLine("FindPrimes_sn2: " + sn2);

                FindPrimesUtils.FindPrimes(
                    Common.ToBigInteger(sn1),
                    Common.ToBigInteger(sn2),
                    outFile
                    );
                return;
            }
            if (ar.ArgIs("/C"))
            {
                string sn1     = ar.NextArg();
                string sn2     = ar.NextArg();
                string outFile = ar.NextArg();

                Console.WriteLine("GetPrimeCount_sn1: " + sn1);
                Console.WriteLine("GetPrimeCount_sn2: " + sn2);

                File.WriteAllText(outFile, Common.ToString(
                                      FindPrimesUtils.GetPrimeCount(
                                          Common.ToBigInteger(sn1),
                                          Common.ToBigInteger(sn2)
                                          )
                                      ),
                                  Encoding.ASCII
                                  );
                return;
            }
            throw new ArgumentException("不明なコマンド引数");
        }