# Check if sum of divisors of two numbers are same in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have two numbers p and q. We have to check whether the sum of all divisors of these tow numbers are same or not.

So, if the input is like p = 559, q = 703, then the output will be True the divisors of 559 is 1, 13, 43 and 703 is 1, 19, 37. The sum of the divisors are 57.

To solve this, we will follow these steps −

• Define a function divSum() . This will take n
• total := 1
• i := 2
• while i * i <= n, do
• if n divisible by i, then
• total := total + i + the floor of (n / i)
• i := i + 1
• return total
• From the main method return true when divSum(p) is same as divSum(q), otherwise false

Let us see the following implementation to get better understanding −

## Example Code

Live Demo

from math import floor

def divSum(n):
total = 1
i = 2
while i * i <= n:
if n % i == 0:
total += i + floor(n / i)
i += 1

return total

def solve(p, q):
return divSum(p) == divSum(q)

p = 559
q = 703
print(solve(p, q))

## Input

559, 703


## Output

True
Updated on 15-Jan-2021 06:21:13

Advertisements