-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.sum_of_numbers_given_in_range.py
More file actions
35 lines (29 loc) · 1.19 KB
/
4.sum_of_numbers_given_in_range.py
File metadata and controls
35 lines (29 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Find the Sum of Numbers in a given Range in Python
# Method 1: Using Brute Force
# In this method we’ll use loops like for, while and do while to sum all the numbers that lay in the intervals of the given input integers.
def sum_of_natural(x,y):
sum=0
for i in range(x,y+1):
sum+=i
return sum
x=int(input('enter the starting number: '))
y=int(input('enter the ending number: '))
print(sum_of_natural(x,y))
# Method 2: Using the Formula
# In this method we’ll use formula mentioned below to find the sum of all the numbers that lay in the interval given by the input variable.
def sum_of_range(x,y):
a=(((y*(y+1))//2)-(x*(x+1)//2)+x)
print(a)
x=int(input('enter the starting number: '))
y=int(input('enter the ending number: '))
sum_of_range(x,y)
# Method 3: Using Recursion
# In this method we’ll use recursion to find the sum of all the numbers that lay in the interval given by the input variable. To know more about recursion, refer to Recursion in Python.
def sum_of_range(sum,x,y):
if x>y:
return sum
return x+sum_of_range(sum,x+1,y)
sum=0
x=int(input('enter the starting number: '))
y=int(input('enter the ending number: '))
print(sum_of_range(sum,x,y))