I have this sequence : 8, 9, 11, 14, 18, 23, 29, 36, 44, ... I have to write a method that calculates n numbers according to this rule and outputs them to the console. The nth number is to be returned. n is passed to the method as a parameter.
If n = 3 is passed as parameter, the output would be: 8 9 11 and the number 11 would be returned
I am trying to create a code that when I enter the number it follows this sequence and gives it back following that rule. Like for example as the last number is 44, the result would be 53. I tried what i could but i did not come to anything, so i just did this with if's but I wonder if you could help me to transform it into a practical code that does it automatically. Thanks in advance
public class Zahlenfolge {
public int berechneFolge(int n) {
int num1 = 0;
int z = 0;
if (n == 8) {
num1 = num1 + 1;
z = n + num1;
}
if (n == 9) {
num1 = num1 + 2;
z = n + num1;
}
if (n == 11) {
num1 = num1 + 3;
z = n + num1;
}
if (n == 14) {
num1 = num1 + 4;
z = n + num1;
}
if (n == 18) {
num1 = num1 + 5;
z = n + num1;
}
if (n == 23) {
num1 = num1 + 6;
z = n + num1;
}
if (n == 29) {
num1 = num1 + 7;
z = n + num1;
}
if (n == 36) {
num1 = num1 + 8;
z = n + num1;
}
if (n == 44) {
num1 = num1 + 9;
z = n + num1;
}
return z;
}
}