DEV Community

Sivakumar Mathiyalagan
Sivakumar Mathiyalagan

Posted on

Exercise python

Two numbers are said to be amicable numbers if the sum of the factors of one number (except the number itself) gives the other number.
The numbers 220 and 284 are amicable, since the sum of the factors of 220 (except 220)

Check whether 1184 and 1210 are amicable numbers.

num1 = 1184
num2 = 1210

def sumOfFactor (num):
    divisor = 2
    sum = 1
    while divisor <= num//2:
        if num%divisor == 0:
            sum = sum + divisor
        divisor+=1
    return sum


if sumOfFactor(num1) == sumOfFactor(num2):
    print (sumOfFactor(num1), "is amicable to",sumOfFactor(num2))
else:
    print (sumOfFactor(num1), "is not amicable to",sumOfFactor(num2))
Enter fullscreen mode Exit fullscreen mode

A book seller has 175 English books, 245 Science books and 385 Mathematics books. He wants to sell the books in a box, subject-wise in equal numbers. What will be the greatest number of the boxes required? Also find the number of books for each subject
in a box.

english = 175
science = 245
maths = 385
hcf = 0
divisor = 2

while divisor < maths//2:
    if(english%divisor==0 and science%divisor==0 and maths%divisor==0):
        hcf = divisor
    divisor+=1

englishBooks = english // hcf
scienceBooks = science // hcf
mathsBooks = maths // hcf

print("no of boxes required is", hcf, "and no of books in each box to be", "English-books",englishBooks, "Science-books",scienceBooks,"Maths-books", mathsBooks)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)