Friendly Pair Or Not (amicable Or Not) In Java
A friendly pair, also known as an amicable pair, consists of two different positive integers such that the sum of the proper divisors of each number (divisors excluding the number itself) equals the other number. In this article, you will learn how to determine if two given numbers form a friendly pair in Java.
Problem Statement
The problem is to write a Java program that takes two positive integers as input and checks if they are a friendly pair. This involves calculating the sum of proper divisors for each number and then comparing these sums.
Example
If the numbers are 220 and 284: - Proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. Their sum is 284. - Proper divisors of 284 are 1, 2, 4, 71, 142. Their sum is 220. Since the sum of proper divisors of 220 is 284, and the sum of proper divisors of 284 is 220, 220 and 284 form a friendly pair.
Background & Knowledge Prerequisites
To understand this article, you should have a basic understanding of:
- Java syntax (variables, loops, conditional statements)
- Methods and function calls
- Input/output operations using
Scanner
Use Cases or Case Studies
- Number Theory Research: Identifying and studying properties of numbers.
- Educational Tools: Creating programs to demonstrate mathematical concepts.
- Algorithm Practice: A common problem for practicing basic algorithmic thinking and loop structures.
- Recreational Mathematics: Exploring interesting number relationships.
Solution Approaches
We will implement a single, straightforward approach to solve this problem.
Approach 1: Direct Calculation of Proper Divisor Sum
This approach involves creating a helper method to calculate the sum of proper divisors for any given number. Then, we use this method to check the friendly pair condition.
One-line summary
Calculate the sum of proper divisors for both numbers and compare them.Code example
// Friendly Pair Checker
import java.util.Scanner;
// Main class containing the entry point of the program
public class Main {
// Method to calculate the sum of proper divisors of a number
public static int sumProperDivisors(int num) {
if (num <= 1) {
return 0; // Proper divisors are not defined for numbers <= 1, or sum is 0
}
int sum = 1; // 1 is always a proper divisor
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i * i != num) { // If i*i == num, i is counted only once
sum += num / i;
}
}
}
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get input from the user
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Step 2: Validate input
if (num1 <= 0 || num2 <= 0) {
System.out.println("Numbers must be positive integers.");
scanner.close();
return;
}
// Step 3: Calculate the sum of proper divisors for both numbers
int sumDivisors1 = sumProperDivisors(num1);
int sumDivisors2 = sumProperDivisors(num2);
// Step 4: Check if they form a friendly pair
// A friendly pair consists of two DIFFERENT numbers
if (num1 != num2 && sumDivisors1 == num2 && sumDivisors2 == num1) {
System.out.println(num1 + " and " + num2 + " form a friendly pair.");
} else {
System.out.println(num1 + " and " + num2 + " do not form a friendly pair.");
}
scanner.close();
}
}
Sample output
Enter the first number: 220
Enter the second number: 284
220 and 284 form a friendly pair.
Enter the first number: 10
Enter the second number: 15
10 and 15 do not form a friendly pair.
Stepwise explanation for clarity
sumProperDivisors(int num)Method:
- This method calculates the sum of all proper divisors of a given number
num. - It initializes
sumto 1 because 1 is a proper divisor of every positive integer (except 1 itself, but theif (num <= 1)handles that). - It iterates from
i = 2up to the square root ofnum. This optimization avoids checking divisors beyond the square root, as any divisorigreater thansqrt(num)will have a corresponding divisornum/ithat is less thansqrt(num)and would have already been found. - If
idividesnumevenly (num % i == 0), theniis a divisor. - We add
itosum. - We also check if
i * i != num. If it's not equal, it meansnum / iis a different divisor, so we addnum / itosumas well. Ifi * i == num,iis the square root, andnum / iwould beiitself, so we avoid adding it twice. - The method returns the calculated
sum.
mainMethod:
- A
Scannerobject is created to read input from the user. - The program prompts the user to enter two positive integers,
num1andnum2. - Input validation checks if the numbers are positive. If not, an error message is printed, and the program exits.
- The
sumProperDivisorsmethod is called for bothnum1andnum2to get their respective sums of proper divisors (sumDivisors1andsumDivisors2).