Computer Science, asked by anindyaadhikari13, 1 day ago

Topic: Recursion

Use recursion to display the series.

1 0 1 0 0 1 0 0 0 1 0 0 0 0 .... N terms.


#Challenge ​

Answers

Answered by BrainlyProgrammer
22

 \tt  \underline{ \: \underline{ Challenge \: Accepted} \: }

Series:-

 \tt \: 1010010001...nth \: term

Logic:-

  • Refer to attachment 1.
  • where I is the position, d is the difference in position when 1 occurs and c is the position when 1 occurs.
  • Condition: When value of i and c are equal, 1 should be printed otherwise, 0.

Language: Not mentioned.

( To be done in Java)

Approach :-

import java.util.*;

public class Challenge {

int c=1,d=2,i=1;

void printChallenge(int n){

if(n!=0){

if(i++==c){

System.out.print(1);

c+=d++;

}

else

System.out.print(0);

printChallenge(n-1);

}

}

public static void main (String ar[]){

Challenge accepted = new Challenge ();

Scanner sc=new Scanner (System.in);

System.out.println("Enter a number");

accepted.printChallenge(sc.nextInt());

}

}

Explanation:-

  • It is better to describe this series as nested series. As there is another series inside it.
  • Refer to Attachment 1. It is found that when 1 occurs, the numbers (value of i) are 1,3,6,10,15 which is itself another series which have a difference of natural numbers beginning from 2,3,4,5...
  • As mentioned in the question we have to use Recursive Function which means a function which calls itself.
  • So lets proceed with the program explanation.
  • Initially, c=1,d=2,i=1 (Attachment 1)
  • The control will first execute main() where it create an object of class and Scanner class respectively and then accepts input which is then passed to printChallenge(int)
  • The control then checks if n is 0? No, it will then execute the inner if otherwise execute the respective else statement.
  • If the condition was true, it then proceed to condition of the LOGIC. If again the condition is true, it prints 1 and updates value of c,d and i.
  • Otherwise, It will print 0 and call itself with new value of n (This time, n-1)
  • And so on, it execute unless n becomes 0.
  • Wohoo! We got the series! 101001000100001...!

• Sample I/O attached.

Attachments:

anindyaadhikari13: Good that you solved it. Not a difficult question at all.
Answered by atrs7391
8

import java.util.Scanner;

class Series {

   int term = 1, counter = 0, limit = 1;

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter limit - ");

       (new Series()).printSeries(sc.nextInt());

   }

   void printSeries(int n) {

       if (n != 0) {

           System.out.print(term + " ");

           if (term == 1)

               term--;

           if (counter == limit) {

               counter = 0;

               term++;

               limit++;

           }

           if (term == 0)

               counter++;

           printSeries(n - 1);

       }

   }

}


anindyaadhikari13: Perfect.
Similar questions