Example #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);
        }
Example #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);
        }
Example #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;                         // 中止
                    }
                }
            }
        }
Example #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)));
        }
        public static void Factorization(BigInteger value, string outFile)
        {
            List <BigInteger> dest = new List <BigInteger>();

            if (value < 2)
            {
                dest.Add(value);
                goto endFunc;
            }

            Queue <BigInteger> q = new Queue <BigInteger>();

            q.Enqueue(value);

            int valueFirstScale = BigIntegerUtils.GetByteArrayLength(value);             // レポート用

            while (1 <= q.Count)
            {
                BigInteger v = q.Dequeue();

                if (PrimeUtils.IsPrime_M(v))
                {
                    dest.Add(v);

                    // レポート
                    {
                        BigInteger tv = value;

                        foreach (BigInteger td in dest)
                        {
                            tv /= td;
                        }

                        Common.Report(1.0 - BigIntegerUtils.GetByteArrayLength(tv) * 1.0 / valueFirstScale, tv);
                    }
                }
                else
                {
                    try
                    {
                        BigInteger f;

                        try
                        {
                            f = FindFactor(v);
                        }
                        catch (Cancelled)
                        {
                            dest.Add(v);
                            dest.AddRange(q.ToArray());

                            break;
                        }

                        if (f <= 1)
                        {
                            throw null;                             // bugged !!!
                        }
                        if (v <= f)
                        {
                            throw null;                             // bugged !!!
                        }
                        if (v % f != 0)
                        {
                            throw null;                             // bugged !!!
                        }
                        q.Enqueue(f);
                        q.Enqueue(v / f);
                    }
                    catch (FF_Retired)
                    {
                        q.Enqueue(v);
                    }
                }
            }

            dest.Sort((a, b) =>
            {
                if (a < b)
                {
                    return(-1);
                }

                if (b < a)
                {
                    return(1);
                }

                return(0);
            });

endFunc:
            File.WriteAllLines(outFile, dest.Select(v => Common.ToString(v)), Encoding.ASCII);
        }
Example #6
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("不明なコマンド引数");
        }