Advertisement
ralphdc09

its102.md.wk.11.fn.13.number02

Nov 26th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. class PythonLabActivity:
  2.     def pow(self, x, n):
  3.         if x == 0 or x == 1 or n == 1:
  4.             return x
  5.  
  6.         if x == -1:
  7.             if n % 2 == 0:
  8.                 return 1
  9.             else:
  10.                 return -1
  11.         if n == 0:
  12.             return 1
  13.         if n < 0:
  14.             return 1 / self.pow(x, -n)
  15.         val = self.pow(x, n // 2)
  16.         if n % 2 == 0:
  17.             return val * val
  18.         return val * val * x
  19.  
  20.  
  21. print(PythonLabActivity().pow(5, 4))
  22. print(PythonLabActivity().pow(4, -3))
  23. print(PythonLabActivity().pow(10, 2))
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement