Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Быстрое_возведение_в_степень
- {
- class Program
- {
- static void Main(string[] args)
- {
- int first = 0;
- var startTime = Stopwatch.StartNew();
- first = pow(1000, 1000000,999);
- startTime.Stop();
- var executingTime = startTime.Elapsed;
- Console.WriteLine($"обычное возведение в степень {executingTime}");
- Console.WriteLine(first);
- int second = 0;
- var startTime1 = Stopwatch.StartNew();
- second = fastPow(1000, 1000000,999);
- startTime1.Stop();
- var executingTime1 = startTime1.Elapsed;
- Console.WriteLine(second);
- Console.WriteLine($"быстрое возведение в степень {executingTime1}");
- }
- static int pow(int num, int pow, int mod)
- {
- int res =1;
- while (pow-- > 0) res = (res* num) % mod;
- return res;
- }
- static int fastPow(int num ,int pow, int mod)
- {
- int res = 1;
- while(pow > 0)
- {
- if (pow % 2 == 1) res =(res* num) % mod;
- num = (num * num)%mod;
- pow >>= 1;
- }
- return res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement