-4

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;
    }
}
Ben Wainwright
  • 2,367
  • 10
  • 26

4 Answers4

3

The formula is: 1/2 (16 - n + n^2)

So for n = 3 it is 1/2(16 - 3 + 3^2) = 1/2(13 + 9) = 1/2 * 22 = 11

Just use this formula in your method and you won't need any loops and if statements.

If you really want solution with loop, you can do it in that way:

public int berechneFolge(int n) {
   int value = 8;
   for(int i = 1; i < n; i++) {
      value += i;
   }
   return value;
}
Matt
  • 1,184
  • 1
  • 11
  • 28
1

Maybe you could consider using a for loop:

class Zahlenfolge {
    public int berechneFolge(int n) {
        if (n <= 0) {
            throw new IllegalArgumentException("n must be positive");
        }
        int num = 8;
        for (int i = 1; i < n; i++) {
            System.out.println(num);
            num += i;
        }
        return num;
    }
}

class Main {
    public static void main(String[] args) {
        Zahlenfolge z = new Zahlenfolge();
        System.out.println(z.berechneFolge(3));
    }
}

Output:

8
9
11
Sash Sinha
  • 11,515
  • 3
  • 18
  • 35
0

I think you should just do a bit of research first (aka Google). ie

"java number sequence while loop code example"

What you should be looking at is a while loop, have a look at this article:

https://www.geeksforgeeks.org/java-while-loop-with-examples/

Edit: I like your expression Matt, but the OP wanted to output each number to the console.

0

if you want to use for loop this will work for you :

int result = 8;

if(n == 0) return 0;
else{
  for(int i = 1; i < n; i++){
    result += i;
return result;
  }
}
Mons
  • 1
  • 3