yakovmonarh

Асинхронный расчет факториала

Oct 28th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Threading.Tasks;
  6.  
  7. namespace AsyncFactorial
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             while (true)
  14.             {
  15.                 Console.WriteLine("Введите целое число:");
  16.                 int outNumber;
  17.                 if(int.TryParse(Console.ReadLine(), out outNumber))
  18.                 {
  19.                     Stopwatch sw = new Stopwatch();
  20.                     sw.Start();
  21.                    
  22.                     Task<BigInteger> taskFact = new Task<BigInteger>(()=>FactEven(outNumber));
  23.                     taskFact.Start();
  24.    
  25.                     Task<BigInteger> taskFact1 = new Task<BigInteger>(() => FactUnEven(outNumber));
  26.                     taskFact1.Start();
  27.    
  28.                     BigInteger x = taskFact.Result * taskFact1.Result;
  29.    
  30.                     sw.Stop();
  31.                     TimeSpan timeSpan = sw.Elapsed;
  32.    
  33.                     Console.WriteLine(timeSpan.ToString());
  34.                     Console.WriteLine(Environment.NewLine);
  35.                 }
  36.                 else
  37.                 {
  38.                     Console.WriteLine("Неверный вод числа");
  39.                     continue;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         static BigInteger FactEven(int numberFactorial)
  45.         {
  46.             BigInteger resultFact = 2;
  47.             for(int i = 4; i <= numberFactorial; i += 2)
  48.             {
  49.                 resultFact *= i;
  50.             }
  51.             return resultFact;
  52.         }
  53.  
  54.         static BigInteger FactUnEven(int numberFactorial)
  55.         {
  56.             BigInteger resultFact = 1;
  57.             for (int i = 3; i <= numberFactorial; i += 2)
  58.             {
  59.                 resultFact *= i;
  60.             }
  61.             return resultFact;
  62.         }
  63.     }
  64. }
Add Comment
Please, Sign In to add comment