umika1150
umika1150

Reputation: 71

Add sequence of number in array and print the nth number

I'm doing a Java exercise that will print out the nth number in a sequence number. I have just completed the normal sequence number in an array like 1,2,3,4,5,6,7,8,9,10,...So if the n=20, it print out 20 for this sequence of number.

Now, I would like to print the nth number in a sequence of number as below:

Start with a(0) = 0 
The next index is #1 (odd), so add 0 + 1 = 1 
The next index is #2 (even), so multiply 1 x 2 = 2 
The next index is #3 (odd), so add 2 + 3 = 5 
The next index is #4 (even), so multiply 5 x 4 = 20 
The next index is #5 (odd), so add 20 + 5 = 25 

Basically, if the index is odd, you add to the prior term. If the index is even, you multiply by the prior term.

The pattern as follows: 0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, 2127230, 2127245, 34035920, 34035937, 612646866 and so on...

The problem is, I don't know how to store those type of sequence number so that I can print the nth number. I'm stuck until:

    if ( number1 % 2 == 0)
{
    number1 = number1 * (1-number1);
}
else
{
    number1 = number1 + (1-number1);
}

Thanks in advance.

Upvotes: 1

Views: 1778

Answers (5)

Saravana
Saravana

Reputation: 12817

Try this

public static void main(String[] args) {
    long res = 0;
    for (int i = 0; i < 20; i++) {
        if (i % 2 == 0)
            res = res * i;
        else
            res = i + res;
        System.out.println(res);
    }
}

Output

0 1 2 5 20 25 150 157 1256 1265 12650 12661 151932 151945 2127230 

Upvotes: 0

Rish
Rish

Reputation: 1383

import java.lang.Math;

public class Test
{

  static long number = 0, previous = 0, limit = 100;

  public static void main(String[] args)
  {

    for (int i=1; i < limit; ++i)
    {
      System.out.print(number + " ");

      if (i % 2 != 0)
      {
          number = previous + i;
          previous = number;
      }
      else
      {
          number = previous * i;
          previous = number;
      }
    }

  }
}

Upvotes: 0

passion
passion

Reputation: 1360

Just store them in an array , get nth with index.

    long[] arr = new long[20];

    for(int i = 1 ; i < arr.length ; i ++){

        if ( i % 2 == 0)
        {
            arr[i] = i * arr[i - 1];
        }
        else
        {
            arr[i] = i + arr[i - 1];
        }
    }

Upvotes: 1

Adit A. Pillai
Adit A. Pillai

Reputation: 667

supposing that number1 is your index variable, try the following code snippet,

if ( number1 % 2 == 0){
        result= number1 * (result);
    }
    else
    {
        result= number1 + (result);
    }
    number1++;
    if(number1>=n){
     break;
    }

Basically you need to iterate until you reach n and keep storing the result of every iteration in a seperate variable called result.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521279

I think you are just missing some logic for storing state from the previous iteration:

int previous = 0;
int number = 0;
for (int i=1; i < 20; ++i) {
    System.out.print(number + " ");

    if (i % 2 != 0) {
        number = previous + i;
        previous = number;
    }
    else {
        number = previous * i;
        previous = number;
    }
}

Output:

0, 1, 2, 5, 20, 25, 150, 157, 1256, 1265, 12650, 12661, 151932, 151945, ...

Upvotes: 2

Related Questions