Bonus Practice Problems(Amicable Numbers (Id-2337))

IF THE QUESTION HAS ANY ERRORS PLEASE WRITE IT AS A COMMENT BECAUSE I DID NOT HAVE THIS QUESTION FOR THE BONUS PPS AND COULD NOT TEST ALL THE TEST CASES AND HAVE ONLY CHECKED THE TEST CASES GIVEN IN THE QUESTION

CSE1701 Amicable Numbers (Id-2337)


Two numbers are said to be amicable if the sum of proper divisors of one number plus 1, is equal to the other number. All divisors of a number other than 1 and itself, are called proper divisors. For example, the numbers 220 and 284 are amicable as the sum of proper divisors of 220 (i.e.) 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110 is equal to 284 and sum of proper divisors of 284 (i.e.) 1, 2, 4, 71 and 142 is 220. Given two numbers, write an algorithm and the subsequent Python code to Print Yes if the given two numbers are amicable. Print ‘No’ if the given numbers are not amicable. Check for boundary conditions and print 'Invalid input' for wrong input. Write appropriate functions for accomplishing the task.

Input Format

First line contains the first number

Next line contains the second number

Output Format

Print either Yes or No

Input:
First line contains the first number

Next line contains the second number
Processing:
def prop_div_sum(n):
    total = 0
    for i in range(2,n):
        if n%i==0:
            total += i
    return total

n = int(input())
m = int(input())
if (prop_div_sum(n)+1) == m and (prop_div_sum(m)+1) == n:
    print('Yes')
else:
    print('No')
Output:
Print either Yes or No

Program:
def prop_div_sum(n):
    total = 0
    for i in range(2,n):
        if n%i==0:
            total += i
    return total

n = int(input())
m = int(input())
if (prop_div_sum(n)+1) == m or (prop_div_sum(m)+1) == n:
    print('Yes')
else:
    print('No')

Pseudocode:
Step1. Get the numbers 
Step2. Calculate the sum of the proper divisors for both the numbers
Step3. Check if the numbers are amicable based on the conditions
Step4. End

3 comments:

  1. code is not working
    it shows:
    Code did not pass the execution
    Input:
    2
    3
    Expected Output:
    No
    Your Program Output:
    Invalid input

    ReplyDelete
  2. The code is updated now...give me a sample test case which gives the output: Invalid input...i haven't added that part to the code because i am not able to understand the boundary conditions

    ReplyDelete
  3. Use "or" instead of "and", it will work.

    ReplyDelete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...