Advertisement
Guest User

Лаба с модулем

a guest
Apr 24th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. namespace Отнять_и_поделить__по_модулю_
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.          
  11.             var timer = System.Diagnostics.Stopwatch.StartNew();
  12.  
  13.             Console.WriteLine("Результат ModExp: {0}", ModExp(7, 9999,1000));//1 задание
  14.             timer.Stop();
  15.             Console.WriteLine("Время выполнения алгоритма: {0}", timer.Elapsed);
  16.             timer.Reset();
  17.  
  18.             timer.Start();
  19.             Console.WriteLine("Результат MinNum: {0}", MinNum(60)); ;//2 задание
  20.             timer.Stop();
  21.             Console.WriteLine("Время выполнения алгоритма: {0}", timer.Elapsed);
  22.  
  23.             Console.ReadKey();
  24.  
  25.         }
  26.  
  27.         static ulong ModExp(ulong x, ulong y, ulong N)
  28.         {
  29.             if (y == 0)
  30.                 return 1 ;
  31.             ulong z = ModExp(x, y / 2, N);
  32.             if (y % 2 == 0)
  33.  
  34.                 return z * z % N;
  35.             else
  36.                 return x * z * z % N;
  37.         }
  38.  
  39.         static BigInteger ModExp(BigInteger x, ulong y, BigInteger N)
  40.         {
  41.             if (y == 0)
  42.                 return 1;
  43.             BigInteger z = ModExp(x, y / 2, N);
  44.             if (y % 2 == 0)
  45.  
  46.                 return z * z % N;
  47.             else
  48.                 return x * z * z % N;
  49.         }
  50.  
  51.         static BigInteger MinNum(ulong n)
  52.         {
  53.             const int step = 3 * 4 * 5;
  54.             BigInteger number = new BigInteger(Math.Pow(10,n-1) + (step - ModExp(10, n - 1, step)));
  55.             BigInteger thirty = new BigInteger(Math.Pow(10, 30));
  56.             return ModExp(number, 1, thirty);          
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement