Guest User

Untitled

a guest
Jun 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. int incr(int i) {
  2.   return i+1;
  3. }
  4.  
  5. int decr(int i) {
  6.   return i-1;
  7. }
  8.  
  9. int add(int a, int b) {
  10.   if (b <= 0) {
  11.     return a;
  12.   }
  13.   return add(incr(a), decr(b));
  14. }
  15.  
  16. int sub(int a, int b) {
  17.   if (b <= 0) {
  18.     return a;
  19.   }
  20.   return sub(decr(a), decr(b));
  21. }
  22.  
  23. int addTimes(int a, int b, int times) {
  24.   if (times == 0) {
  25.     return a;
  26.   }
  27.   return addTimes(add(a,b), b, decr(times));
  28. }
  29.  
  30. int mul(int a, int b) {
  31.   if (b == 1) {
  32.     return a;
  33.   } else if (b == 0) {
  34.     return 0;
  35.   }
  36.   return addTimes(0,a,b);
  37. }
  38.  
  39. int mulTimes(int a, int b, int times) {
  40.   if (times == 0) {
  41.     return 1;
  42.   }
  43.   return mulTimes(mul(a,b), b, decr(times));
  44. }
  45.  
  46. int pow(int a, int b) {
  47.   if (b == 0) {
  48.     return 1;
  49.   } else if (b == 1) {
  50.     return a;
  51.   }
  52.   return mulTimes(1, a, b);
  53. }
  54.  
  55. int main() {
  56.   cout << pow(1000,1000);
  57. }
Add Comment
Please, Sign In to add comment