76.3k views
0 votes
Below we've attempted to write a recursive formula that will calculate the nth triangular number. 1 def tri(n): return n + tri(n - 1) What will be the current result of using this function? It will correctly calculate the nth triangular number OIt will instead calculate the n 1th triangular number. It will instead calculate the n +1th triangular number. O It will cause a RuntimeError due to infinite recursion. It will cause a NameError due to attempting to call try) from within try Problem 5.2.2, Part 2 of 3 1.0/1.0 point (graded) Why does the issue or error in the problem above occur? O No issue occurs; the function correctly calculates the nth triangular number. O The recursive function has no base case, so it never stops calling copies of itself. O The recursive function has no base case, so it never calls a copy of itself. OThe recursive function incorrectly calls itself with n -1 as an argument instead of n O The recursive function incorrectly calls itself with n 1 as an argument instead of n1. OThe variable n - 1 is not defined when referenced on line 2 Which of the following revisions to the function tri) would fix the observed issues? No revision necessary; the original function works. 1| def tri(n): if n == 1: return 1 4else: 5 l return n tri (n-1) 1| def tri(n): 2 if n return n +tri(n-1) 4lse: return 1 1| def tri(n): if n > 1 return 1 4lse return n + tri(n-1) 1| def tri(n): 21f n>1: return n tri(n-1) 4 else: return1

1 Answer

2 votes

Clearer Question:

Below we've attempted to write a recursive formula that will calculate the nth triangular number.

1| def tri(n):

2| return n + tri(n - 1)

What will be the current result of using this function?

a) It will correctly calculate the nth triangular number.

b) It will instead calculate the n 1th triangular number.

c) It will instead calculate the n +1th triangular number.

d) It will cause a RuntimeError due to infinite recursion.

e) It will cause a NameError due to attempting to call try) from within try Problem

Problem 5.2.2, Part 2 of 3

1.0/1.0 point (graded)

Why does the issue or error in the problem above occur?

a) No issue occurs; the function correctly calculates the nth triangular number.

b) The recursive function has no base case, so it never stops calling copies of itself.

c) The recursive function has no base case, so it never calls a copy of itself.

d) The recursive function incorrectly calls itself with n -1 as an argument instead of n O The recursive function incorrectly calls itself with n 1 as an argument instead of n1.

e) The variable n - 1 is not defined when referenced on line 2

Which of the following revisions to the function tri() would fix the observed issues?

a) No revision necessary; the original function works.

(b)

1| def tri(n):

2| if n == 1;

3| return 1 ;

4| else:

5| return n tri (n-1)

(c)

1| def tri(n):

2| if n == 1:

3| return n +tri(n-1)

4l else:

5| return 1

(d)

1| def tri(n):

2| if n > 1

3| return 1

4l else:

5| return n + tri(n-1)

(e)

1| def tri(n):

2| 1f n>1:

3| return n tri(n-1)

4| else:

5| return 1

Answer:

For number 1 the answer is d

It will cause a RuntimeError due to infinite recursion.

For number 2 the answer is b

The recursive function has no base case, so it never stops calling copies of itself.

For number 3 the answer is (b)

(b)

1| def tri(n):

2| if n == 1;

3| return 1 ;

4| else:

5| return n tri (n-1)

Step-by-step explanation:

The answer to No 1 is

It will cause a RuntimeError due to infinite recursion.

What this means is that during the execution of the program error occurs because the program has no base case so the program will keep calling itself for infinite hence the RuntimeError caused by infinite call of the program by itself

The answer to No 2 is

The recursive function has no base case, so it never stops calling copies of itself.

This is related to what we just discussed above when there is no base case (This base case is like a condition which when meet the recursive function stops calling itself ).The function keeps calling itself.

The answer to No 3 is

1| def tri(n):

2| if n == 1;

3| return 1 ;

4| else:

5| return n tri (n-1)

To understand this answer we to understand what a triangular number is

Triangular numbers:

as shown in the image here, are a pattern of numbers that form equilateral triangles. Each subsequent number in the sequence adds a new row of dots to the triangle.

It is important to note that in this case, n equals the term in the sequence. So, n equals 1 is the first term, n equals 5 is the fifth term, n equals 256 is the 256th term. Believe it or not, we can actually use this n to figure out how many dots are in its corresponding triangle (i.e. its triangular number).

So we see that the smallest number the function should return is 1 and this gives us a fact to set up our base case condition as.

2| if n == 1; //This our base case

3| return 1 ;

Below we've attempted to write a recursive formula that will calculate the nth triangular-example-1
User Nanook
by
4.5k points