Question

In: Computer Science

[10 marks] (TestSumSeries.java) Write a recursive method that sums up the following series: s(i) = 21+...

  1. [10 marks] (TestSumSeries.java) Write a recursive method that sums up the following series:

s(i) = 21+ 24+47+410+…+ i+23i+1+i+13i+1

i = 0, 1, 2, 3, …

When i is even, the term is i+23i+1

When i is odd, the term is i+13i+1

In the main method, display the s(i) for i = 0, 1, 2, 3, 4, 5

Solutions

Expert Solution

Implement the program as follows:

  1. define the class TestSumSeries
  2. define a static method CalcSum() that accepts integer, i
  3. if i = 0, then return 1
  4. if i is even, then recursively call CalcSum() and return i+23i+1 + CalcSum(i-1)
  5. if i is odd, then recursively call CalcSum() and return i+13i+1 + CalcSum(i-1)
  6. define main() method
  7. for i varies from 0 to 5
  8. print s(i) for i = 0, 1, 2, 3, 4, 5

Program: TestSumSeries.java


public class TestSumSeries{                                                                     /* define the class TestSumSeries */
        public static int CalcSum(int i){                                                       /* define a static method CalcSum() that accepts integer, i */
                if(i==0){                                                                                               /* if i = 0, then return 1 */
                        return 1;
                }
                else if(i%2==0){                                                                                /* if i is even, then recursively call CalcSum() and return i+23i+1 + CalcSum(i-1) */
                        return i+23*i+1 + CalcSum(i-1);
                }
                else{                                                                                                   /* if i is odd, then recursively call CalcSum() and return i+13i+1 + CalcSum(i-1) */
                        return i+13*i+1 + CalcSum(i-1);
                }
        }
        public static void main(String[] args){                                         /* define main() method */
                for(int i=0;i<6;i++){                                                                        /* for i varies from 0 to 5 */
                        System.out.println("s(" + i + ") = " + CalcSum(i)); /* print s(i) for i = 0, 1, 2, 3, 4, 5 */
                }
        }
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

(TestSumSeries.java) Write a recursive method that sums up the following series: s(i) = 1/1+ 3/3+3/5+5/7+⋯+ (i+1)/(2i+1)+(i+2)/(2i+1)...
(TestSumSeries.java) Write a recursive method that sums up the following series: s(i) = 1/1+ 3/3+3/5+5/7+⋯+ (i+1)/(2i+1)+(i+2)/(2i+1) i = 0, 1, 2, 3, … When i is even, the term is (i+1)/(2i+1) When i is odd, the term is (i+2)/(2i+1) In the main method, display the s(i) for i = 0, 1, 2, 3, 4, 5
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
3. Consider the following recursive algorithm for computing the sum of the following series: S(n) =...
3. Consider the following recursive algorithm for computing the sum of the following series: S(n) = 1/1! + 2/2! + . . . + n/n!. ALGORITHM S (n) //Input: A positive integer n // Procedure: fact(n) returns the factorial of the number passed as parameter //Output: The sum of the series: S(n) = 1/1! + 2/2! + . . . + n/n! if n = 1 return 1 else return S(n − 1) + n/fact(n) Set up and solve a...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive...
Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive method to be tail-recursive. public int f8( int[] arr, int index ) { if ( index == -1 ) return 0; if (arr[index] == 2) return 1 + f8( arr, index - 1 ); return f8( arr, index - 1); }
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
Write a recursive method that displays an integer value reversely on the console using the following...
Write a recursive method that displays an integer value reversely on the console using the following header: public static void reverseDisplay(int value) For example, reverseDisplay(12345) displays 54321. Write a test program that prompts the user to enter an integer, invokes the method above, and displays its reversal.
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int)...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int) of the longest String in a List of Strings. For each recursive call, remove the first String from the list and return the greater of the length of the String you removed or the result of calling the method on the remainder of the list. The base case should be that the size of the list is 0. Write a driver to verify that...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT