How to resolve the algorithm Amicable pairs step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Amicable pairs step by step in the Java programming language

Table of Contents

Problem Statement

Two integers

N

{\displaystyle N}

and

M

{\displaystyle M}

are said to be amicable pairs if

N ≠ M

{\displaystyle N\neq M}

and the sum of the proper divisors of

N

{\displaystyle N}

(

s u m

(

p r o p D i v s

( N ) )

{\displaystyle \mathrm {sum} (\mathrm {propDivs} (N))}

)

= M

{\displaystyle =M}

as well as

s u m

(

p r o p D i v s

( M ) )

N

{\displaystyle \mathrm {sum} (\mathrm {propDivs} (M))=N}

.

1184 and 1210 are an amicable pair, with proper divisors:

Calculate and show here the Amicable pairs below 20,000; (there are eight).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Amicable pairs step by step in the Java programming language

This Java program finds amicable pairs up to a specified limit.

Amicable pairs are two numbers such that the sum of the proper divisors of the first number is equal to the second number, and vice versa.

For example, 220 and 284 are amicable pairs because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, and 220, which sum to 284, and the proper divisors of 284 are 1, 2, 4, 71, 142, and 284, which sum to 220.

The program works by first calculating the sum of the proper divisors of each number up to a specified limit using the properDivsSum method.

It then uses a map to store the sum of the proper divisors for each number.

Finally, it iterates over the numbers up to the limit and checks if the sum of the proper divisors of a number is greater than the number itself, is less than or equal to the limit, and is equal to the number for which the sum of the proper divisors was calculated in the previous step.

If all of these conditions are met, the program prints the two numbers as an amicable pair.

Source code in the java programming language

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

public class AmicablePairs {

    public static void main(String[] args) {
        int limit = 20_000;

        Map<Long, Long> map = LongStream.rangeClosed(1, limit)
                .parallel()
                .boxed()
                .collect(Collectors.toMap(Function.identity(), AmicablePairs::properDivsSum));

        LongStream.rangeClosed(1, limit)
                .forEach(n -> {
                    long m = map.get(n);
                    if (m > n && m <= limit && map.get(m) == n)
                        System.out.printf("%s %s %n", n, m);
                });
    }

    public static Long properDivsSum(long n) {
        return LongStream.rangeClosed(1, (n + 1) / 2).filter(i -> n % i == 0).sum();
    }
}


  

You may also check:How to resolve the algorithm Gauss-Jordan matrix inversion step by step in the Rust programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the jq programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the min programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the Prolog programming language
You may also check:How to resolve the algorithm Arrays step by step in the Apex programming language