Reputation: 1352
I'm working on this code, that use big numbers :
import java.math.*;
import java.util.*;
import java.lang.*;
public class main {
public static void main(String[] args){
double max = 1e+15;
List<Integer> sum = new ArrayList<Integer>();
sum.add(1);
long aux = 0;
for(long i = 1;i<max;i++){
sum.add(sum.get(i-1)+scomponi(sum.get(i-1),aux));
if(i+1==1e+7 || i+1==1e+8 || i+1==1e+9 || i+1 == 1e+10 || i+1 == 1e+10 || i+1==1e+11 || i+1==1e+12 || i+1==1e+13 || i+1==1e+14)
System.out.println(i+1+"-imo -> "+sum.get(i));
}
System.out.println(sum.get(sum.size()-1));
}
public static Long scomponi(Long num,Long tot) {
while (num > 0) {
Long digit = num % 10;
tot += digit;
num /= 10;
}
return tot;
}
}
But I'm not able to find a solution for this error :
What it means? I'm not really good in java, but this is so simple code, what's wrong?
Upvotes: 0
Views: 1069
Reputation: 14572
Like janos said, you can't use long index in a List. But from your code, you don't need the list, you only use the last value calculated.
public static void main(String[] args){
double max = 1e+15;
long last = 1;
long aux = 0;
for(long i = 1;i<max;i++){
last += scomponi(last,aux);
if(i+1==1e+7 || i+1==1e+8 || i+1==1e+9 || i+1 == 1e+10 || i+1 == 1e+10 || i+1==1e+11 || i+1==1e+12 || i+1==1e+13 || i+1==1e+14)
System.out.println(i+1+"-imo -> "+last);
}
System.out.println(last);
}
If you really want to keep every values, you could store those into a file but from what I see, you don't need those.
Of couse, the loop will take some time ;)
Upvotes: 1
Reputation: 124804
Lists in Java are int
-indexed, as you can see in the javadoc of List.get()
, and you're trying to get elements by a long
index.
If you need lists bigger than the integer range, you need another data structure that supports that, you cannot use List
.
Upvotes: 3