Question

#8 Write an iterative method that calculates the SUM of all integers between 1 and a...

#8 Write an iterative method that calculates the SUM of all integers between 1 and a given integer N (input into the method).  Write a corresponding recursive solution. (return answer, don’t print)

#8 Solutions:

6      publicstaticintIterative(intn){
7    intsum = 0;
8    for(inti = 1 ; i<n; i++){
9        sum = sum + i;
10    }
11    returnsum;
12  }

Recursive Solution

6      publicstaticintIterative(intn){
7
8    if(n == 0) return0;
9    return1 + Iterative(n-1);
10 }

Please answer below question:
Looking at your solution(s) in #8 above – it’s easy to see that as n gets larger, the solution is takes longer to solve (or increases the chance of “overflow”).  Is this the best we can do? (is this the most efficient solution)? If so, explain why.  If not, give your solution (and make sure you explain why it is more efficient).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

publicstaticintIterative(intn){

if(n == 0) return0;
return n + Iterative(n-1);
}

The solution is efficient, reason being there are two ways of finding sum recursively. Either add from 1 to n or from n to 1 in the first case we pass n so it's not possible to generate the stop condition as we wouldn't know where to stop. So the only solution is add from n to 0.

Only change in the code is return n + Iterative(n-1) instead of 1. If 1 is written it'll add 1 n times so doesn't serve our purpose. This is the only efficient solution.

Add a comment
Know the answer?
Add Answer to:
#8 Write an iterative method that calculates the SUM of all integers between 1 and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates...

    java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates the sum of all Integer values in a given ArrayList. For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer. Return null if the list is empty or null. Think about the base case. When should the method terminate/end? Under what condition should your method stop making recursive calls and return your...

  • C# Create a “Main” method that contains a 1-Dimensional ArrayList of integers. Randomly fill the ArrayList...

    C# Create a “Main” method that contains a 1-Dimensional ArrayList of integers. Randomly fill the ArrayList with at least 10 integers using a recursive method. Then create another method that calculates the sum of that ArrayList using recursion as well. Only use TWO (2) parameters, at most, in these two recursive methods. Finally, print the array and the sum of ArrayList. Sample Output: ArrayList contents: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 The sum of the ArrayList...

  • Write a method, name it sumNums. This method should compute and return the sum of integers...

    Write a method, name it sumNums. This method should compute and return the sum of integers from n1 to n2. For example, sum of integers sumNums(1,10) will sum the sequence 1+2+3+4+5+6+7+8+9+10

  • 1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci...

    1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N,...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • The Abo series is the following: 0, 1, 2, 4, 3, 6, 5, 5, 4, 8,...

    The Abo series is the following: 0, 1, 2, 4, 3, 6, 5, 5, 4, 8, 7, 7, 6, 7, 6, 6, 5, 10... This series is defined in the following manner: Abo(n) = 0 for n <= 0 Abo(1) = 1 Abo(n) = 1 + Abo(n/2) if n > 1 is even Abo(n) = 2 + Abo((n+1)/2) if n > 1 is odd Write a recursive method that computes Abo(n). Write a program that prints the first 20 Abo...

  • java Write methods for 2d 1. a method to calculate the sum of a 2d double...

    java Write methods for 2d 1. a method to calculate the sum of a 2d double array 2. a method to calculate the sum of each row of a 2d double array 3. a method to calculate the sum of each column of a 2d double array 4. a method to calculate the average of a 2d double array 5. a method to calculate the average of each row of a 2d double array 6. a method to calculate the...

  • computer architecture The sum of the two 32 bit integers may not be representable in 32 bits. In this case, we say that an overflow has occurred. Write MIPS instructions that adds two numbers stor...

    computer architecture The sum of the two 32 bit integers may not be representable in 32 bits. In this case, we say that an overflow has occurred. Write MIPS instructions that adds two numbers stored in registers Ss1 and Ss2, stores the sum in register $s3, and sets register Sto to 1 if an overflow occurs and to 0 otherwise. 5. (16pts) 6. Show the IEEE 754 binary representation of the number -7.425 in a single and double 7. If...

  • HINT 1)-Write a method to accept n integer number between 0 and 100 and return the...

    HINT 1)-Write a method to accept n integer number between 0 and 100 and return the average. 2)-Write a method to convert an the letter grade as follow. integer number between 0 and 100 to latter grade A to F and return. 100-90->A 89-80->B 79-70->C 69-60->D 00-59->F 3)-Write a method to compute GPA for following letter grade and return the GPA value. A =>4.0 B ->3.0 C->20 D->1.0 Hint for option 1 1 import java.util.Scanner 2 public class ProjPart2 3...

  • (a) Write a C program to print a list of all integers between 1 and 1,000...

    (a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called "output.dat". Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT