How to Find If Two Numbers Are Amicable in Python

In this tutorial, we will learn how to program "How to Find If Two Numbers Are Amicable in Python." The objective is to determine whether the two given numbers are amicable or not. This tutorial will guide you step by step through the entire process of checking and calculating amicable numbers. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively, helping you strengthen your problem-solving abilities and enhance your Python coding skills.

This topic is straightforward to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of finding the amicable between two number. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def sum_of_divisors(n):
  2.     """Return the sum of proper divisors of n (excluding n itself)."""
  3.     total = 1 if n > 1 else 0  # 1 is always a divisor (except for n=1)
  4.     for i in range(2, int(n**0.5) + 1):
  5.         if n % i == 0:
  6.             total += i
  7.             if i != n // i:  # avoid adding square root twice
  8.                 total += n // i
  9.     return total
  10.  
  11. while True:
  12.     print("\n=============== Find If Two Numbers Are Amicable ===============\n")
  13.  
  14.     try:
  15.         x = int(input('Enter number 1: '))
  16.         y = int(input('Enter number 2: '))
  17.  
  18.         if x <= 0 or y <= 0:
  19.             print("Please enter positive integers only.")
  20.             continue
  21.  
  22.         if sum_of_divisors(x) == y and sum_of_divisors(y) == x:
  23.             print(f"{x} and {y} are Amicable! 🎉")
  24.         else:
  25.             print(f"{x} and {y} are Not Amicable.")
  26.     except ValueError:
  27.         print("Invalid input. Please enter integers only.")
  28.         continue
  29.  
  30.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  31.     if opt == 'no':
  32.         print("Exiting program...")
  33.         break
  34.     elif opt != 'yes':
  35.         print("Invalid choice. Exiting program...")
  36.         break
This Python program checks whether two given numbers are amicable numbers. It uses the `sum_of_divisors()` function to calculate the sum of proper divisors of a number (excluding the number itself). For each pair of numbers entered, the program verifies if the sum of divisors of the first number equals the second number and vice versa. If both conditions are satisfied, the numbers are declared *amicable*; otherwise, they are not. The program runs inside a loop, prompting the user to input two positive integers, handling invalid or negative inputs, and asking whether they want to try again or exit.

Output:

There you have it we successfully created How to Find If Two Numbers Are Amicable in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials