Write a Python program to calculate the aliquot sum of an given integer


This Python program calculates the Aliquot sum of a given positive integer val.

  • The program first checks if the input val is an integer using the isinstance() function. If val is not an integer, the program prints "Input must be an integer" to the console and exits. If val is an integer, the program proceeds to the next step.
  • The program then checks if the input val is positive by comparing it with zero. If val is less than or equal to zero, the program prints "Input must be positive" to the console and exits.
  • If val is a positive integer, the program proceeds to calculate its Aliquot sum. The Aliquot sum of a positive integer is the sum of its proper divisors, i.e., all the divisors of val except for val itself.
  • The program calculates the Aliquot sum of val by iterating over all divisors divisor of val using a for loop and adding divisor to a running total if val is divisible by divisor. The program uses the expression val // 2 + 1 as the upper bound of the loop because a divisor greater than val // 2 cannot be a proper divisor of val.
  • Finally, the program prints the original number val and its Aliquot sum to the console using the print() function and appropriate messages, "Original Number: " and "Aliquot Sum: ".

Source Code

val=12
if not isinstance(val, int):
	print("Input must be an integer")
if val <= 0:
	print("Input must be positive")
res = sum(divisor for divisor in range(1, val // 2 + 1) if val % divisor == 0)
print("Original Number : ",val)
print("Aliquot Sum : ",res)

Output

Original Number :  12
Aliquot Sum :  16

Example Programs