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.
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
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
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
I have the following question: Write a recursive function to find the Nth element from the...
I have the following question: Write a recursive function to find the Nth element from the top of a stack. For example, if N is 3 the function should return the third element in the stack. Use the following header: template <class Type> Type getNth( stack<Type> & s, int n) Although your function may push and pop values from the stack, it must eventually leave the stack in its original state. Your function may NOT use a help stack or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT