I found out the maximum of adding numbers(with recursion) to themselves is 21666. To understand what I mean: We have number 5 and we have to add all numbers below including 5 to themselves. The result will be 15. For a loop (with int) the maximum is the int max. But with an recursive method it seems the max is 21666. Why is it like that?
public class Rekursion {
public static void main(String[] args) {
int number = 21666;
System.out.println("Result with a loop: " + add(number));
System.out.println("Result with recursion: " + recursion(number));
}
public static int add(int x) {
int sum = 0;
for (int i = 0; i <= x; i++) {
sum += i;
}
return sum;
}
public static int recursion(int x) {
if (x==1){
return 1;
}
else{
return recursion(x-1)+x;
}
}
}