D_L3

recursion

Jan 25th, 2024
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. bool isOdd(int num) {
  9.     return num % 2 == 1;
  10. }
  11. int recursion(int x, int n, int m) {
  12.     if (m < 0)
  13.         return 0;
  14.  
  15.     if (n == 0) {
  16.         return 1;
  17.     }
  18.  
  19.     if (n > 0 && !isOdd(n)) {
  20.         return ((x % m) * recursion(x, n - 1, m)) % m;
  21.     }
  22.  
  23.     if (n > 0 && isOdd(n)) {
  24.         int f = recursion(x, n / 2, m);
  25.         return (f * f) % m;
  26.     }
  27.    
  28. }
  29.  
  30. int main() {
  31.    
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment