Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Using while loop as we don't know the number input
- # increasing method 1 + 2 + 3 + 4 + ...
- def find_sum(n):
- x = 1 #start with 1
- sum = 0
- while x <= n: #until x reaches the number n
- sum += x
- x +=1
- return sum
- # shortcut method using formula
- def find_sum(n):
- return n*(n+1)/2
- # decreasing method ... + 4 + 3 + 2 + 1
- def find_sum(n):
- sum = 0
- while n > 0: # until n reaches 1
- sum +=n
- n -= 1 # decrement of n by 1
- return sum
- print(find_sum(15))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement