Question: Problem 3 : Below are two functions that calculates the sum of the first n integers using python. In other words, ? ? ( n

Problem 3: Below are two functions that calculates the sum of the first n
integers using python. In other words, ??(n)=1+2+3+dots+n.
def }\operatorname{sum}1(n)
if (}n==1)\mathrm{ :
return 1
else:
return n+ sum1(n-1)
def sum2(n):
return n*(n+1)/2
Answer the questions below:
(a) Choose 5 different numbers for n. Verify that sum 1(n) equals sum 2(n) for
these 5 numbers.
(b) Which of these two functions do you think is generally more efficient for
n>100. Why do you think this is so?
(c) Do you think sum1(1) or sum 2(1) is more efficient? Why?
(A) What happens if you call sum1(0)?
(e) Let's define the open sentance:
{:P(n):suml1(n)isavalidcalculation.
i. What is the smallest ninN such that P(n) is true? Let's label this
case P(b), where b is this smallest such number. In other words,
what is b?
true statements?
iii. Is the following statement true or false: P(b)=>P(b+1)?
iv. Assume you could use ??2(n)? to validate whether sum1(n) is correct
for large number of n. How would you rewrite the open statement
P(n)?
v. Can you rewrite sum1(n) so that it calls sum 2(n)?
Problem 3 : Below are two functions that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!