0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8 This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)

Consider the below series :

0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8

  • This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order
  • Every even terms is derived from the previous  term using the formula (x/2)

Write a program to find the nth term in this series.

  1. The value n in a positive integer that should be read from STDIN the nth term that is calculated by the program should be written to STDOUT.
  2. Other than the value of the nth term no other characters /strings or message should be written to STDOUT.

For example

if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.

53 comments on “0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8 This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)”


  • Praneeth Reddy

    import java.lang.*;
    import java.util.Scanner;

    class SeriesOddandEven {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n;
    System.out.println(“Enter n value”);
    n = s.nextInt();
    if (n % 2 != 0) {
    System.out.println(n – 1);
    } else {
    System.out.println((n – 2) / 2);
    }

    }

    }


  • Thanveer

    code in python:

    x, y = 0, 0
    combseries = {}

    N = int(input())
    if N > 20000:
    print(“enter value less than 20000”)
    else:
    for i in range(N):
    if i % 2 == 0:
    combseries[i] = x
    x = x + 2
    elif i % 2 != 0:
    k = int(combseries[i-1])
    combseries[i] = int(k / 2)

    mylist = list(combseries.values())
    print(mylist[N – 1])


  • Vootukur

    #include

    int main(void) {
    // your code goes here

    int a,i,n;
    scanf(“%d”,&n);
    if(n>20000)
    {
    printf(“0”);
    }

    else
    {
    for(i=1;i<=n;i++)
    {
    if(i==1||i==2)
    {
    a=0;
    printf("%d\t",a);
    }
    else if(i%2!=0)
    {
    a= (i/2)*2;
    printf("%d\t",a);
    }
    else if(i%2==0)
    {
    a=(i/2)-1;
    printf("%d\t",a);
    }
    }
    }

    return 0;
    }


  • abhishek

    #include
    #include
    int main()
    {
    int i, n, a=0, b=0, c;

    printf(“enter”);
    scanf(“%d”,&n);

    for(i=1;i1)
    a = a + 2;
    b = a/2;
    }

    else
    {
    c = b/2;
    }
    }
    if(n%2!=0)
    {
    printf(“%d”,b);
    }
    else
    {
    printf(“%d”,c);
    }
    }


  • Gaurav

    user_input = int(input(“Input – “))

    if user_input % 2 == 1:
    user_input = user_input//2
    print(user_input*2)
    else:
    user_input = (user_input-1) // 2
    print(user_input * 1)


  • Aniket

    num = int(input(“Enter the number: “))

    if (num>20000):
    print(“N/A”)
    else:
    if (num%2 ==0):
    num = num//2
    print(num-1)
    else:
    num = num//2+1
    print(2**(num-1))


  • Arno

    import sys

    def nth_term(num):
    a,b = 0, 0
    lst = []
    for i in range(0, 20):
    if i % 2 != 0:
    a = b / 2
    lst.extend([int(a)])
    else:
    b = i
    lst.extend([b])
    return lst
    if __name__ == “__main__”:
    n = len(sys.argv)
    if n > 1:
    num = int(sys.argv[1])
    res = nth_term(num)
    print(res[num-1])
    else:
    print(“no arguments passed”)


  • Arun

    #No loops

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    if(n%2 != 0){
    System.out.println(2*((n-1)/2));
    return;
    }else{
    System.out.println(2*((n-1)/4));
    }
    }


  • Greshma

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println(“Enter the limit: “);
    int n = scanner.nextInt();
    System.out.print(“0 0 “);
    for (int i = 2; i < n; i++) {
    if (i % 2 == 0)
    System.out.print(i+" ");
    else
    System.out.print(i/2+ " ");
    }
    }


  • Kusal

    n = int(input(“n: “))
    if n<= 20000:
    for i in range(0,n):
    if i == 0 or i == 1:
    s = 0
    elif i % 2 == 0:
    s = i
    else:
    s = (i – 1)/2
    print(int(s))