nht_cntt

ModuloPower

Jun 9th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. static int ModuloPower(int value, int exponent, int modulus)
  2. {
  3.     int res = 1;
  4.  
  5.     while (exponent > 0)
  6.     {
  7.         if ((exponent & 1) != 0)
  8.         {
  9.             res = (res * value) % modulus;
  10.         }
  11.         value = (value * value) % modulus;
  12.         exponent >>= 1;
  13.  
  14.     }
  15.     return res;
  16. }
  17.  
  18. static void PrintCalcModulo()
  19. {
  20.     int a, x, p;
  21.     Console.Write(" Enter number a: ");
  22.     a = Convert.ToInt32(Console.ReadLine());
  23.     Console.Write(" Enter number x: ");
  24.     x = Convert.ToInt32(Console.ReadLine());
  25.     Console.Write(" Enter number p: ");
  26.     p = Convert.ToInt32(Console.ReadLine());
  27.  
  28.     Console.WriteLine("\n {0}^{1} mod {2} = {3}", a, x, p, ModuloPower(a, x, p));
  29. }
Advertisement
Add Comment
Please, Sign In to add comment