简体   繁体   中英

Error: Incompatible types when attempting to return an arraylist

I am trying to solve Problem 12 on Project Euler. I have an idea of how to complete this problem, however I encountered an error. I've searched how to use arraylist from different questions however I am still facing problems.

import java.util.ArrayList;

public class Level_12 {
/*
The sequence of triangle numbers is generated by adding the natural numbers. 
So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 

The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?
*/

public static ArrayList<Long> check(long num) {
    ArrayList<Long> divisors = new ArrayList<Long>();
    for (long o = 1; o <= Math.sqrt(num); o++)
     if (num % o == 0) {
         divisors.add(o);
         System.out.println(o + " is a current divisor of " + num);
     }
     for (Long m : divisors) {
     return m;
     }
} 

public static void main(String[] args) {
    long triangle = 0; //Triangle number
    long total = 500; //Total divisors
    long currenttotal = 0; //Amount of divisors
    long i = 0; //Just to itterate 

    while (currenttotal <= total) {
        if (check(i) > currenttotal) { //Finding if the 
            triangle = i;
            if (currenttotal == total) break;
        }
        i++;
    }
    System.out.println("The value of the first triangle number to have over 500 divisors is " + triangle);

  }
} 

EDIT:

Fixed with final code as

import java.util.ArrayList;

public class Level_12 {
/*
The sequence of triangle numbers is generated by adding the natural numbers. 
So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 

The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?
*/

public static ArrayList<Long> check(long num) {
    ArrayList<Long> divisors = new ArrayList<Long>();
    for (long o = 1; o <= Math.sqrt(num); o++)
     if (num % o == 0) {
         divisors.add(o);
         System.out.println(o + " is a current divisor of " + num);
     }
     return divisors;
     }


public static void main(String[] args) {
    long triangle = 0; //Triangle number
    long total = 500; //Total divisors
    long currenttotal = 0; //Amount of divisors
    long i = 0; //Just to itterate 

    while (currenttotal <= total) {
        if (check(i).size() > currenttotal) { //Finding if the amount of divisors is larger than current divisors
            triangle = i;
            if (currenttotal == total) break;
        }
        i++;
}
    System.out.println("The value of the first triangle number to have over 500 divisors is " + triangle);

  }
}

您在check()方法中返回Long而不是ArrayList<Long>

Returning Long while the return type of method is ArrayList<Long> .So update the return type as below :

public static Long check(long num) {
    ArrayList<Long> divisors = new ArrayList<Long>();
    for (long o = 1; o <= Math.sqrt(num); o++)
     if (num % o == 0) {
         divisors.add(o);
         System.out.println(o + " is a current divisor of " + num);
     }
     for (Long m : divisors) {
     return m;
     }
} 

The culprit is this :

  for (Long m : divisors) {
    return m;
  }

In this you are returning a Long value whereas the method signature specifies returning ArrayList

   public static **ArrayList<Long>** check(long num)

You need to either change the method signature or return the appropriate value depending on what you want to accomplish.

You are only putting half the divisors of m into the list. If o divides m , then m/o divides m too.

/**
 * Return a list of the divisors of an integer.  If it is a perfect square, the square
 * root will be repeated. The list will not be in order.
 * @param num The number whose divisors to find.
 * @return A list of the number's positive divisors.
 */
public List<Long> divisors(long num) {
    final List<Long> divisors = new ArrayList<>();
    final long limit = Math.sqrt(num);
    for (long o = 1L; o <= limit; o++)
    if (num % o == 0) {
        divisors.add(o);
        divisors.add(num / o);
    }
    return divisors;
}

/**
 * Return the nth triangular number.
 * @param n The index of the desired triangular number.
 * @return The n-th triangular number 1 + 2 + ... + n
 */
public long triangle(long n) {
    return n * (n + 1) / 2;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM