Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Linq;
- namespace calc
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Calculando primos...");
- // long start = 10000000;
- // long end = 50000000;
- long start = 10000;
- long end = 150000;
- var timer = Stopwatch.StartNew();
- var primos = new ConcurrentBag<long>();
- while(true)
- {
- Parallel.For(start, end, i => {
- var times = 0;
- for(var j = i; j >= 1; j--)
- {
- var mod = i % j;
- if(mod == 0)
- {
- times++;
- }
- }
- if(times == 2)
- {
- primos.Add(i);
- }
- });
- timer.Stop();
- System.Console.WriteLine("{1} - Total de primos: {0} em {2} entre {3} e {4}.", primos.Count, DateTime.Now, timer.Elapsed, start, end);
- primos = new ConcurrentBag<long>();
- timer.Restart();
- }
- // Parallel.For(start, end, i => {
- // var times = 0;
- // for(var j = i; j >= 1; j--)
- // {
- // var mod = i % j;
- // if(mod == 0)
- // {
- // times++;
- // }
- // }
- // if(times == 2)
- // {
- // // System.Console.WriteLine(i);
- // primos.Add(i);
- // }
- // if (timer.Elapsed.TotalMinutes >= 1)
- // {
- // timer.Stop();
- // var s = primos.ToList();
- // foreach(var primo in s.OrderBy(x => x))
- // {
- // System.Console.WriteLine(primo);
- // }
- // System.Console.WriteLine("Total de primos: {0} - {1}", primos.Count, DateTime.Now);
- // primos = new ConcurrentBag<long>();
- // timer.Restart();
- // }
- // });
- // for (var i = start; i < end; i++)
- // {
- // var times = 0;
- // for(var j = i; j >= 1; j--)
- // {
- // var mod = i % j;
- // if(mod == 0)
- // {
- // times++;
- // }
- // }
- // if(times == 2)
- // {
- // // System.Console.WriteLine(i);
- // primos.Add(i);
- // }
- // if (timer.Elapsed.TotalMinutes >= 1)
- // {
- // timer.Stop();
- // foreach(var primo in primos)
- // {
- // System.Console.WriteLine(primo);
- // }
- // System.Console.WriteLine("Total de primos: {0}", primos.Count);
- // primos.Clear();
- // timer.Restart();
- // }
- // }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement