0

This is an exercise.

A perfect number is a number whose sum of divisors without itself is equal to that number 6 is a perfect number because its divisors are: 1,2,3,6 and 1 + 2 + 3 = 6 28 is a perfect number because its divisors are: 1,2,4,7,28 and 1 + 2 + 4 + 7 = 28

Task: write the body of findNPerfectNumbers, which will find n prime perfect numbers and return them as a list

I must use this program:

import java.util.ArrayList;

public class Exercise {
    
    public static ArrayList<Integer> findNPerfectNumbers(int n) 
    {
        return new ArrayList<>();
    }

    public static void main(String[] args) 
    {
        System.out.println(findNPerfectNumbers(4));
    }
}

I create this code to resolve this problem, but I have a problem to return an ArrayList. I don't know how. It should look like this example: 6 = 1,2,3,6 ///// 28 = 1, 2, 4, 7

My idea:

import java.util.ArrayList;

public class Main
{

    public static ArrayList<Integer> findNPerfectNumbers(int n)
    {
        int sum = 0;
        ArrayList<Integer> perfectList = new ArrayList<>();
        ArrayList<Integer> factorList = new ArrayList<>();
        for (int i = 6; i < n; i++)
        {
            factorList.clear();
            for (int j = 1; j <= i / 2; j++)
            {
                if (i % j == 0)
                {
                    factorList.add(j);
                }
            }
            sum = 0;
            for (int h = 0; h < factorList.size(); h++)
            {
                sum = sum + factorList.get(h);
            }
            if (sum == i)
            {
                perfectList.add(i);
            }
         }
        return perfectList;
    }

    public static void main(String[] args)
    {
        System.out.println(findNPerfectNumbers(28));
    }
}

Anyone have an idea?

rgettman
  • 176,041
  • 30
  • 275
  • 357

2 Answers2

0

The question is as simple as to have the findNPerfectNumbers function return the first N perfect numbers.

The main part for the exercise is probably to do this as efficiently as possible. For example limiting divider check by half like you do in for (int j = 1; j <= i / 2; j++) is one of many options.

The reason your function doesn't return anything though is because your outer for loop is incorrect with the given input of 4 what you'r doing is for (int i = 6; i < 4; i++) which doesn't do any loops because 4 is smaller than 6. what you probably intended to do issomething like for (int i = 6; perfectList.size() < n; i++) which would loop aslong as you have fewer than N perfect numbers.

example working code:

import java.util.ArrayList;

public class Exercise {

    public static ArrayList<Integer> findNPerfectNumbers(int n) {
        int sum = 0;
        ArrayList<Integer> perfectList = new ArrayList<>();
        for (int i = 6; perfectList.size() < n; i++) {
            ArrayList<Integer> factorList = new ArrayList<>();
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {
                    factorList.add(j);
                }
            }

            sum = 0;
            for (Integer factor : factorList) {
                sum += factor;
            }
            if (sum == i) {
                System.out.println("Found perfect number " + i + " with factors " + factorList);
                perfectList.add(i);
            }
        }
        return perfectList;
    }

    public static void main(String[] args) {
        System.out.println(findNPerfectNumbers(4));
    }
}
Ralan
  • 651
  • 6
  • 17
  • But when i write perfect number 6 then i dont get info about this number 6 have 6 = 1,2,3,6 – WeirdProgrammer1001 Aug 12 '22 at 12:20
  • I need change this program now but idk how to write information when you put 6 or other perfect number what i can / this perfect numbers. I should get score in arraylist, you can get my code and try this, I get only final info = [], nothing more. – WeirdProgrammer1001 Aug 12 '22 at 12:21
  • @WeirdProgrammer1001 added a modified example of your code that is functional. You wil have to modify the code the run faster though as i doubt most school machines wil get an answer in a reasonable time as it is now. If you want the factor numbers in the main function you would have to modify the return type of findNPerfectNumbers to Map> but not sure if that is allowed – Ralan Aug 12 '22 at 12:39
  • As a sidenote/challenge, you should be able to get the first 8 perfect numbers within a second with decently optimized code implementing some of the theories around perfect numbers that can be found online. I think anything beyond that would prove difficult as numbers grow fast – Ralan Aug 12 '22 at 15:05
0

If number is less than 10^1500 you can use Euclid's method

public static List<Long> findPerfect(int n){
    List<Long> perfectList=new ArrayList<>();
    int x=0;
    long sum=0;
    long last;
    while(perfectList.size()!=n){
        last= (long) Math.pow(2,x);
        sum+=last;
        if(isPrime(sum))
            perfectList.add(sum*last);
        x++;
    }
    return perfectList;

}
public static boolean isPrime(long x){
    if(x==1)
        return false;
    for (int i = 2; i <= Math.sqrt(x); i++) {
        if(x%i==0)
            return false;
    }
    return true;
}