Ejemplo n.º 1
0
 /// <summary>
 /// 创建新的 Prime 对象。
 /// </summary>
 /// <param name="id">Id 属性的初始值。</param>
 public static Prime CreatePrime(global::System.Int64 id)
 {
     Prime prime = new Prime();
     prime.Id = id;
     return prime;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 用于向 Prime EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet&lt;T&gt; 属性的 .Add 方法。
 /// </summary>
 public void AddToPrime(Prime prime)
 {
     base.AddObject("Prime", prime);
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            BigInteger maxFound;
            Int64      foundCount = 0;

            using (var pdb = new PrimeDB())
            {
                if (!pdb.Primes.Any())
                {
                    maxFound = 2;
                }
                else
                {
                    var maxId = pdb.Primes.Max(p => p.Id);
                    var v     = pdb.Primes.Single(p => p.Id == maxId);
                    maxFound   = BigInteger.Parse(v.PrimeNumber) + 1;
                    foundCount = v.Id;
                }
            }

            var max = BigInteger.Parse(new string('9', 9999));
            var sw  = new Stopwatch();
            var a   = new ConcurrentBag <BigInteger>();

            const int gap = 1000000;

            sw.Start();
            for (var i = maxFound; i < max; i += gap)
            {
                var sortedList = new List <BigInteger>();
                var last       = maxFound;
                ParallelFor(i, i + gap, j =>
                {
                    if (j.IsProbablePrime())
                    {
                        a.Add(j);
                    }
                });

                sortedList.AddRange(a);
                sortedList.Sort();
                Prime p;
                using (var pdb = new PrimeDB())
                {
                    pdb.BeginTransaction();
                    foreach (var bigInteger in sortedList)
                    {
                        p = new Prime
                        {
                            Id          = ++foundCount,
                            PrimeNumber = bigInteger.ToString()
                        };

                        if (p.Id % 10000 == 0)
                        {
                            pdb.Insert(p);
                        }

                        if (bigInteger - last > gap)
                        {
                            sw.Stop();
                            Console.WriteLine("{0} -第{1}个- -{2}位- [{3}]", sw.Elapsed, p.Id, p.PrimeNumber.Length,
                                              p.PrimeNumber);
                            sw.Reset();
                            sw.Start();
                            last = bigInteger;
                        }
                    }

                    pdb.CommitTransaction();
                }

                sortedList.Clear();
                while (!a.IsEmpty)
                {
                    BigInteger someItem;
                    a.TryTake(out someItem);
                }

                Thread.Sleep(500);
            }

            Console.WriteLine(sw.Elapsed);
            Console.WriteLine(a.Count);
            Console.ReadLine();
        }