Question

I am trying to figure this out. How to write a java program that finds the amicable pairs under a certain value, i.e 10000, so it has to compare all numbers from 0 to that limit within each other, and find out which are the amicable pairs. Then i must have the output as a 2 column matrix.

I sort out the formula of calculating the proper divisors of a number and then sum it up.

But i can not go further with the for-loop that will compare the numbers together, and the final output which will give me the result as a 2 column matrix.

So far I am by the sum of factors or dividers, and this is what I have

public class Amicable {

    public static int sumfactors(int n) {

        int sum=0;
            for(int div=1; div<=n; div++)
            {
                if(n%div==0)
                {
                    sum +=div;
                }

            }
            return sum-n;           
    }
}
Was it helpful?

Solution

First of all, you can improve the performance of your method:

private static int sumFactors(int n)
{
    int sum = 0;
    for (int div=1; div<=n/2; div++)
    {
        if (n%div == 0)
        {
            sum += div;
        }
    }
    return sum;
}

Then, you can add the method below to your class:

private static int[][] getMatrix(int limit)
{
    int[] array = new int[limit];
    for (int i=2; i<limit; i++)
        array[i] = sumFactors(i);

    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    for (int i=2; i<limit; i++)
    {
        int j = array[i];
        if (j < i && i == array[j])
            map.put(i,j);
         // Check 'j < i' in order to:
         // 1. Avoid an illegal index when 'j >= limit'
         // 2. Avoid the insertion of the equivalent pair [j,i]
         // 3. Avoid the insertion of perfect numbers such as [6,6]
    }

    int[][] matrix = new int[map.size()][2];
    int index = 0;
    for (int key : map.keySet())
    {
        matrix[index][0] = key;
        matrix[index][1] = map.get(key);
        index++;
    }
    return matrix;
}

Finally, you can call it from your main method (for example):

public static void main(String[] args)
{
    int[][] matrix = getMatrix(10000);
    for (int i=0; i<matrix.length; i++)
        System.out.println(matrix[i][0]+" "+matrix[i][1]);
}

OTHER TIPS

I have provided a solution below that will find the amicable numbers till a limit and return as the 2D array. The code here,

public static int[][] findAmicableNumbers(int N) {

    int checked = 0;

    Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();

    for (int i = 1; i <= N; i++) {

        int u = sumOfDivisors(i);

        if (i == u) {
            continue;
        }

        int v = sumOfDivisors(u);

        if (i != checked && i == v) {

            checked = u;
            map.put(i, u);
        }
    }

    int[][] result = new int[map.size()][2];

    int index = 0;
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {

        result[index][0] = entry.getKey();
        result[index][1] = entry.getValue();

        index++;
    }

    return result;
}


public static int sumOfDivisors(int N) {

    int sum = 1;

    for (int i = 2; i * i <= N; i++) {

        if (N % i == 0) {

            sum += i;

            if (N / i != i) {
                sum += N / i;
            }
        }
    }

    return sum;
}


public static void main(String[] args) {

    int[][] res = findAmicableNumbers(10000);

    for (int[] a : res) {
        System.out.println(Arrays.toString(a));
    }
}

The prints the result,

[220, 284]
[1184, 1210]
[2620, 2924]
[5020, 5564]
[6232, 6368]

This is a simple solution that should work

public static long ambicable(long c) {
int res=0;
    for(long i=1;i<c;i++){
        if(c%i==0) {
            res+=i;
        }
    }
    return res;
}
public static long pair(long a) {
    long c=0;
    for(long i=1;i<10000;i++) {
        if(i==ambicable(ambicable(i))&& i!=ambicable(i)) {
            c+=i;
        }
    }
    return c;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top