
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
![Implement two methods (using iterative and recursive
approaches) to compute the sum of the reciprocals of the first n
natural numbers. Mathematically, it can be denoted as follows:
Sum(n) = 1 + 1 2+1 3+ ... + 1 n (1) Example 1: Input: [1]
Output: [1.0] Explanation: n= 1, so sum(1)=1.0 Example 2:
Input: [2] Output: [1.5] Explanation: n
sum(2)=1+1/2=1.5 O Iterative Approach: public static float
sum iterative (int n) { } 1 0 Recursive Approach: Hint for
recursive implementation: The prototype for the recursive
approach should be as follows: Sum(n) = ( 1.0 if n== 1 Sum(n
- 1) + 1/n if n >1 (2) public static float sum recursive (int n) {
-
= 2, so](https://content.bartleby.com/qna-images/question/47bba70b-d35c-4545-add1-e8862c4403b1/af48b64b-784c-459e-b904-6f794fafb0a3/8dmjb0c_thumbnail.png)
Transcribed Image Text:Implement two methods (using iterative and recursive
approaches) to compute the sum of the reciprocals of the first n
natural numbers. Mathematically, it can be denoted as follows:
Sum(n) = 1 + 1 2+1 3+ ... + 1 n (1) Example 1: Input: [1]
Output: [1.0] Explanation: n= 1, so sum(1)=1.0 Example 2:
Input: [2] Output: [1.5] Explanation: n
sum(2)=1+1/2=1.5 O Iterative Approach: public static float
sum iterative (int n) { } 1 0 Recursive Approach: Hint for
recursive implementation: The prototype for the recursive
approach should be as follows: Sum(n) = ( 1.0 if n== 1 Sum(n
- 1) + 1/n if n >1 (2) public static float sum recursive (int n) {
-
= 2, so
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images

Knowledge Booster
Similar questions
- Write a recursive function in Java that accepts an integer as input and returns 1 + 1/2 + 3 + 1/4 + ...(n or 1/n). The answer is the sum of the odd integers from 1 to n plus the sum of the reciprocals of the even integers.arrow_forwardpython 3 Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line. When the input is: Julia Lucas Mia then the output is (must match the below ordering): Julia Lucas Mia Julia Mia Lucas Lucas Julia Mia Lucas Mia Julia Mia Julia Lucas Mia Lucas Julia question: is it any way that i can use ('if' statement as base case, and 'else' statement as recursive case) in the code below? thanks. code: def all_permutations(permList, nameList):# TODO: Implement method to create and output all permutations of the list of names.def createPermutationsList(nameList):f = len(nameList) if f == 0:return [] if f == 1:return [nameList] permList = [] for i in range(f):newList = nameList[i]remaining = nameList[:i] + nameList[i+1:]for p in createPermutationsList(remaining):permList.append([newList] + p)…arrow_forwardUsing recursion, write a Java program that takes an input ‘n’ (a number) from a user to calculate and print out the Fibonacci using the following modified definition: F(N) = 1 if n = 1 or n = 2 = F((n+1)/2)2 + F((n-1/2)2 if n is odd = F(n/2 + 1)2 – F(n/2 – 1)2 if n is even Your solution must implement recursion to receive points for this question.arrow_forward
- Write a java program that uses a recursive algorithm to print all the valid (properly closed and open brackets) combinations of a number of parentheses taken from the user. For example your program should behave similar to the below: Enter a number your input is : 3 output :((())), ( () () ), (()) (), () (()), () () ()arrow_forwardWrite a program that uses a recursive call to find the integer logb of a number. Where logb returns the integer log of a number in a designated base. For example, the integer base 10 log of 1234 is 3, and the integer base 2 log of 1234 is 10. This is a relatively easy calculation. You simply repeatedly divide the number by the base using integer division until the quotient is less than the base and count the number of completed divisions. 1234/10=123 (1) 123/10 12 (2) 12/10 = 1 (3) 1234/2= 617 (1) 617/2 = 308 (2) 308/2 = 154 (3) 154/2 = 77 (4) 77/2 = 38 (5) 38/2 = 19 (6) 19/2 = 9 (7) 9/2=4 (8) 4/2=2(9) 2/2 = 1 (10)arrow_forwardHow can I apply this python code wherein I have an input n. Then create a list from 2, n and remove the multiples of n. Example: Input: 10 Output: 2, 3, 4, 5, 6, 7, 8, 9 def createList(n): #Base Case/s #TODO: Add conditions here for your base case/s #if <condition> : #return <value> #Recursive Case/s #TODO: Add conditions here for your recursive case/s #else: #return <operation and recursive call> #remove the line after this once you've completed all the TODO for this function return [] def removeMultiples(x, arr): #Base Case/s #TODO: Add conditions here for your base case/s #if <condition> : #return <value> #Recursive Case/s #TODO: Add conditions here for your recursive case/s #else: #return <operation and recursive call> #remove the line after this once you've completed all the TODO for this function return []arrow_forward
- Write a recursive method oddSum that takes a positive odd integer n and returns the sum of odd integers from 1 to n.arrow_forwardPLZ help with the following: In Java Write a program using recursion to display all valid (i.e. properly open and closed) combinations of n-pairs of parentheses.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY

Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON

Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education

Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY