Consider the two versions of the function computing the combinations. Add a global variable counter and increment it by one at the top of both functions. This will count the number of function calls required to complete the calculations in each case. Try the two versions of the function for a few pairs of numbers n and m and print out the value of the counter after each of them. Don't forget to reset the counter to 0 before each call. Comment on the observed difference between the numbers of function calls.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Consider the two versions of the function computing the combinations. Add a global variable counter and increment it by one at the top of both functions. This will count the number of function calls required to complete the calculations in each case. Try the two versions of the function for a few pairs of numbers n and m and print out the value of the counter after each of them. Don't forget to reset the counter to 0 before each call. Comment on the observed difference between the numbers of function calls.

Combinations
C(n, m) = n!/(m! (n-m)!)
C(n, m) = C(n-1, m) + C(n-1, m-1)
(defun comb (nm)
(cond
((= nm) 1)
((= m 0) 1)
((= m 1) n)
(t (+ (comb (- n 1) m)
(comb (- n 1) (- m 1))))))
(comb 3 2); 3
(comb 5 2); 10
Improved version with DP:
(defun el10 (nm)
(+ (* n 10) m)) ; 10*n+m
(setq C (make-vector 100 mil))
(defun comb1 (nm)
(let ((res 0))
(if (setq res (elt C (el10 n m))) res
(setq res
(cond
((= nm) 1)
((= m 0) 0)
((= m 1) n)
(t (+ (comb1 (- n 1) m)
(- n 1)
(comb1
(- m 1))))))
(aset C (el10 nm) res))))
(comb1 7 3) ; 35
(comb1 10 5); 252
Transcribed Image Text:Combinations C(n, m) = n!/(m! (n-m)!) C(n, m) = C(n-1, m) + C(n-1, m-1) (defun comb (nm) (cond ((= nm) 1) ((= m 0) 1) ((= m 1) n) (t (+ (comb (- n 1) m) (comb (- n 1) (- m 1)))))) (comb 3 2); 3 (comb 5 2); 10 Improved version with DP: (defun el10 (nm) (+ (* n 10) m)) ; 10*n+m (setq C (make-vector 100 mil)) (defun comb1 (nm) (let ((res 0)) (if (setq res (elt C (el10 n m))) res (setq res (cond ((= nm) 1) ((= m 0) 0) ((= m 1) n) (t (+ (comb1 (- n 1) m) (- n 1) (comb1 (- m 1)))))) (aset C (el10 nm) res)))) (comb1 7 3) ; 35 (comb1 10 5); 252
Expert Solution
steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education