Advertisement
s4ros

steps.py

Dec 1st, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # https://challengerocket.com/step-counting
  2.  
  3. class App():
  4.     def factorial(self, n):
  5.         result = 1
  6.         while n > 1:
  7.             result *= n
  8.             n -= 1
  9.         return result
  10.  
  11.     def newton(self, n, k):
  12.         return self.factorial(n)/(self.factorial(k)*(self.factorial(n-k)))
  13.  
  14.     def newtons_count(self, steps):
  15.         return ((steps//2) - 1) if steps > 3 else 0
  16.  
  17.     def solve(self, steps):
  18.         result = steps
  19.         k = self.newtons_count(steps)
  20.         if k:
  21.             newton_n = steps - 2
  22.             newton_k = 2
  23.             for i in range(k):
  24.                 result += self.newton(newton_n, newton_k)
  25.                 newton_n -= 1
  26.                 newton_k += 1
  27.         return int(result)
  28.  
  29.  
  30. if __name__ == "__main__":
  31.     app = App()
  32.     print(app.solve(1))
  33.     print(app.solve(2))
  34.     print(app.solve(3))
  35.     print(app.solve(4))
  36.     print(app.solve(5))
  37.     print(app.solve(6))
  38.     print(app.solve(7))
  39.     print(app.solve(8))
  40.     print(app.solve(9))
  41.     print(app.solve(10))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement