Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 22.3, Problem 22.3.4CP
Program Plan Intro
The following are the steps which need to followed to compute the sum of two number such as “n1” and “n2”,
Step 1:Initialize the variable “sum” as 0.
Step 2:Next use the for loop to itertae the numbers from “n1” to “n2”.
Step 3:Then sum up the values and produce the result.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
let n = 1*3*5*....*197*199 (the product of first 100 odd numbers)
find the last 2 digits of n
Exercise: Find the function s(n) that indicates the number of sums performed by the
following segment of an algorithm:
for i = 2 to n+1 do
for j = 1 to i+2 do
p = p + n +j
This function uses a curious mix of iteration and recursion:
function F(n)
if n < 1
t <- O
return 1
for i <- 0 to n
for j <- i to n
t<- t+j
return t + F(n-1)
The number of basic operations (additions and subtractions)
performed is:
○ O(n)
Ⓒ (n²)
(n² log n)
Ⓒ (n³)
Ө
Ⓒ (n4)
Chapter 22 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 22.2 - Prob. 22.2.1CPCh. 22.2 - What is the order of each of the following...Ch. 22.3 - Count the number of iterations in the following...Ch. 22.3 - How many stars are displayed in the following code...Ch. 22.3 - Prob. 22.3.3CPCh. 22.3 - Prob. 22.3.4CPCh. 22.3 - Example 7 in Section 22.3 assumes n = 2k. Revise...Ch. 22.4 - Prob. 22.4.1CPCh. 22.4 - Prob. 22.4.2CPCh. 22.4 - Prob. 22.4.3CP
Ch. 22.4 - Prob. 22.4.4CPCh. 22.4 - Prob. 22.4.5CPCh. 22.4 - Prob. 22.4.6CPCh. 22.5 - Prob. 22.5.1CPCh. 22.5 - Why is the recursive Fibonacci algorithm...Ch. 22.6 - Prob. 22.6.1CPCh. 22.7 - Prob. 22.7.1CPCh. 22.7 - Prob. 22.7.2CPCh. 22.8 - Prob. 22.8.1CPCh. 22.8 - What is the difference between divide-and-conquer...Ch. 22.8 - Prob. 22.8.3CPCh. 22.9 - Prob. 22.9.1CPCh. 22.9 - Prob. 22.9.2CPCh. 22.10 - Prob. 22.10.1CPCh. 22.10 - Prob. 22.10.2CPCh. 22.10 - Prob. 22.10.3CPCh. 22 - Program to display maximum consecutive...Ch. 22 - (Maximum increasingly ordered subsequence) Write a...Ch. 22 - (Pattern matching) Write an 0(n) time program that...Ch. 22 - (Pattern matching) Write a program that prompts...Ch. 22 - (Same-number subsequence) Write an O(n) time...Ch. 22 - (Execution time for GCD) Write a program that...Ch. 22 - (Geometry: gift-wrapping algorithm for finding a...Ch. 22 - (Geometry: Grahams algorithm for finding a convex...Ch. 22 - Prob. 22.13PECh. 22 - (Execution time for prime numbers) Write a program...Ch. 22 - (Geometry: noncrossed polygon) Write a program...Ch. 22 - (Linear search animation) Write a program that...Ch. 22 - (Binary search animation) Write a program that...Ch. 22 - (Find the smallest number) Write a method that...Ch. 22 - (Game: Sudoku) Revise Programming Exercise 22.21...Ch. 22 - (Bin packing with smallest object first) The bin...Ch. 22 - Prob. 22.27PE
Knowledge Booster
Similar questions
- Indicate the run time complexity: for(inti = 1; i < 555; i++) for(int j = 1; j< n; j*=2) y++; O None of the choices O(n*) O(n lg n) O(n) O(lg n)arrow_forwardWrite an algorithm that asks the user to enter a positive integer n and prints the sum of the divisors of n.arrow_forwardThe following is an addition problem in binary notation. Each letter represents a unique binary digit. Which letter represents 1 and which represents 0? Design an algorithm for solving problems like this. YXX+XYX=XYYY I can solve this problem but i am having trouble with creating an algorithm to solve this.arrow_forward
- Function f grows no slower than function g. O f (n) = O(g(n) O f (n) = 2(g(n)) O f (n) = o(g(n)) O f(n) = O(g(n))arrow_forwardIn this problem, you will write different programs to x^N, where x ∈ R, n ∈ N. b) Devise a recursive algorithm to compute x^n , using the fact that x^n= x ⋅ x^n−1 .arrow_forwardThe following recursive procedure computes the n-th Fibonacci number. Fib(n) 1 ifn<= 1 2 return n 3 else return Fib(n - 1) + Fib(n - 2) Use memoization technique to develop an efficient version of Fib(n). Give your solution in pseudocode, similar to what is given above.arrow_forward
- Assume that n is a positive integer. for k:= 3 to n for j :=1 to 6n x := a[k] = b[j] next j next k (a) Compute the actual number of elementary operations (additions, subtractions, multiplications, divisions, and comparisons) that are performed when the algorithm segment is executed. For simplicity, however, count only comparisons that occur within if-then statements; ignore those implied by for-next loops. (b) what is the order of the algorithm? 0(nb) where b =arrow_forwardAssume n is the number of inputs and that the log function is base 2. Treat this piece of code as a pseudocode. Give the big-Oh characterization, in terms of n for the following algorithm: for i in range(n*logn): for j in range(i): for k range(1000): print('I did it!')arrow_forwardThe first examples of recursion are the mathematical functions factorial and fibonacci. These functions are defined for non-negative integers using the following recursive formulas:factorial(0) = 1factorial(N) = N*factorial(N-1) for N > 0fibonacci(0) = 1fibonacci(1) = 1fibonacci(N) = fibonacci(N-1) + fibonacci(N-2) for N > 1Write recursive functions to compute factorial(N) and fibonacci(N) for a given non-negative integer N, and write a main() routine to test your functions.(In fact, factorial and fibonacci are really not very good examples of recursion, since the most natural way to compute them is to use simple for loops. Furthermore, fibonacci is a particularly bad example, since the natural recursive approach to computing this function is extremely inefficient.)arrow_forward
- Suppose the runtime efficiency of an algorithm is presented by the function f(n) = 10n + 10². Which of the following statements are true? Indicate every statement that is true. A. The algorithm is O(n log n) B. The algorithm is O(n) and O(log n). C. The algorithm is O(log n) and 80(n). D. The algorithm is (n) and (log n). E. All the options above are false.arrow_forwardif n = 1 S(n) S(n//2) S(n – 1) if n is even; // is Python integer division if n is odd and n >1 Does Master Theorem apply to the recursive function S? Answer by writing either "yes" or "no" There is no need for extra explanations in this part.arrow_forwardThe following function f uses recursion: def f(n): if n <= 1 return n else return f(n-1) + f(n-2) 5 Let n be a valid input, i.e., a natural number. Which of the following functions returns the same result but without recursion? a) def f(n): a <- 0 b <- 1 if n = 0 return a elsif n = 1 return b else for i in 1..n c <- a + b a <- b b <- c return b f(n): a <- 0 i <- n while i > 0 a <- a + i + (i-1) return a f(n): arr[0] <- 0 arr[1] <- 1 if n <= 1 return arr[n] else for i in 2..n arr[i] <- arr[i-1] + arr[i-2] return arr[n] f(n): arr[0..n] <- [0, ..., n] if n <= 1 return arr[n] else a <- 0 for i in 0..n a <- a + arr[i] return aarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education