1

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.

3
  • Have you looked at OEIS? There's sample code at oeis.org/A077138 Commented Oct 10, 2016 at 4:30
  • @vielmetti I haven't. I google up but there's nothing that can help. Thank you for the link. I will take a look at it. Commented Oct 10, 2016 at 4:36
  • Try to use array to store the result Commented Oct 10, 2016 at 4:51

5 Answers 5

2

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, ...
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried this one but if I change i < 1000, the number after 20 got negative number.
@umika1150 This is probably because of integer truncation, i.e. you are exceeding the range available for integers. Use long type instead to avoid this.
I've tried it. But to get the 1000th number I need to use BigInteger since the number is too large. Thank you for your answer
1

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];
        }
    }

2 Comments

I change the code to use BigInteger because the nth number may be more than 100. It may need until 1000th number. I try using array to store it but failed. it should just change the long[] to BigInteger arr = new BigInteger[1000]; right?
@umika1150 BigInteger can not be multiplied or added using * /+ , you need use its method multiply or add.
1

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.

Comments

0
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;
      }
    }

  }
}

Comments

0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.