1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ray Of Light [21]
4 years ago
8

Q3: Summation Write a recursive implementation of summation, which takes a positive integer n and a function term. It applies te

rm to every number from 1 to n including n and returns the sum of the results. # Question 3 def summation(n, term) : ""Return the sum of the first n terms in the sequence defined by term. Implement using recursion! >>> summation(5, lambda x: x * x * x) # 1^3 + 2^3 + 3^3 + 4^3 + 5^3 225 >>> summation(9, lambda x: x + 1) # 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 54 >>> summation(5, lambda x: 2**x) # 241 + 2^2 + 2^3 + 2^4 + 245 62 >>> # Do not use while for loops!

Engineering
1 answer:
harina [27]4 years ago
6 0

Answer:

Here is the recursive function summation:

def summation(n, term):      

   if n == 1:  

       return term(n)

   else:

       return term(n) + summation(n - 1, term)

Explanation:

The function summation() has two arguments where n is a positive integer and term is a function term. term has the lambda function which is a small function having an argument and an expression e.g lambda b: b+20

So the summation() function is a recursive function which returns sum of the first n terms in the sequence defined by term ( a lambda function).

If you want to check if this function works, you can call this function by passing values to it like given in the question.

summation(5, lambda x: 2**x)

Here the value of n is 5 and the term is a lambda function x: 2**x

If you want to see the results of this function on output screen then use:

print(summation(5, lambda x: 2**x))

The print() function will print the results on screen.

This returns the sum of first 5 terms in sequence defined in the function x: 2**x

In recursive methods there are two cases: base case and recursive case. Base case is the stopping case which means that the recursion will stop when the base case/ base condition evaluates to true. The recursive case is when the function keeps calling itself so the recursive function keepsexecuting until the base case becomes true.

Here the base case is if n == 1:  So the recursive function calling itself until the value of n becomes 1.  

Recursive case is:

       return term(n) + summation(n - 1, term)

For the above example with n= 5 and term = x:2**x the recursions starts from n and adds all the terms of the series one by one and the value of n keeps decrementing by 1 at every recursive call.

When the value of n is equal to 1 the base case gets true and the recursion ends and the result of the sum is displayed in output.

This is how the summation() function works for the above function call:

2^1 + 2^2 + 2^3 + 2^4 + 2^5

n is 5 So this term function is called recursively 5 times and at every recursive call its value decreases by 1. Here the term function is used to compute 2 raise to power n. So in first recursive call the 2 raise to the power 5 is computed, then 5 is decremented and then in second recursive call to summation(), 2 raise to the power 4 is calculated, in third recursive call  to summation(), 2 raise to the power 3 is calculated, in fourth recursive call  to summation(), 2 raise to the power 2 is calculated, in fifth recursive call  to summation(), 2 raise to the power 1 is calculated, then the base condition is reached as n==1. So the recursion stops and the sum of the above computed power function results is returned which is 62.

2^1 + 2^2 + 2^3 + 2^4 + 2^5 = 62

The screen shot of recursive function along with the output of explained examples is attached.

You might be interested in
Find the rate of heat transfer through a 6 mm thick glass window with a cross-sectional area of 0.8 m2 if the inside temperature
kiruha [24]

Answer:

6.9

Explanation:

I had the same question lol your welcomr if itd not right in sorry

3 0
3 years ago
Are designed to make it easier for employees to get health and safety Information about
iren [92.7K]

Answer:

what the options

Explanation:

4 0
3 years ago
A water supply agency is planning to add two reservoirs to its system. Water will flow from Reservoir A to Reservoir B via a 10,
NikAS [45]

Attached is the solution to the above question.

3 0
3 years ago
Technician A says that mechanical shifting controls can wear out over time. Technician B says that vacuum control rubber diaphra
diamong [38]

Based on the information, both technician A and technician B are correct.

<h3>How to depict the information?</h3>

From the information given, Technician A says that mechanical shifting controls can wear out over time.

Technician B says that vacuum control rubber diaphragms can deteriorate over time.

In this case, both technicians are correct as the information depicted is true.

Learn more about technicians on:

brainly.com/question/1548867

#SPJ12

8 0
2 years ago
(a) Differentiate between heat treatment of ferrous and non-ferrous alloys (b) With your understanding of material's thermal pro
liubo4ka [24]

Answer:

In ferrous metal iron present but on the other hand in the non ferrous material iron does not present.That is why there is a different heat treatment process for ferrous and nonferrous materials.

Ferrous materials contains iron is the main constitute.Like steel ,cast iron ,wrought iron .Steel and cast iron are  the alloy element of iron ans carbon.Wrought iron is the purest from of iron.

Heat treatment process for  ferrous materials :

1.Normalizing

2.Annealing

3.Quenching

4.Surface hardening

Heat treatment process for non ferrous materials :

Mostly annealing process is used for non ferrous materials.After annealing non ferrous will become soft.

When two metal plates are joined then they form a bimetallic structure.The bimetallic structure is used to find the relationship of thermal temperature and the mechanical displacement.

The use of bimetallic structure -In clock ,thermometers ,engines.

7 0
3 years ago
Other questions:
  • Water flows through two smooth pipes with the same diameter and length as shown below. ipe is twice that through the first-pipe.
    12·1 answer
  • A mysterious device found in a forgotten laboratory accumulates charge at a rate specified by the expression gm = 9 - 10tC from
    13·1 answer
  • Water circulates throughout a house in a hot water heating system. If the water is pumped at a speed of 0.50m/s through a 4.0-cm
    5·1 answer
  • Nitrogen gas flows through a long, constant-diameter adiabatic pipe. It enters at 100 psia and 120°F and leaves at 50 psia and 7
    14·2 answers
  • A rectangular concrete beam has dimensions b=16 in. and h=30 in. The location of the Gr. 60 reinforcing bars, which are placed a
    15·1 answer
  • A series R-L circuit is given. Circuit is connected to an AC voltage generator. a) Derive equations for magnitude and phase of c
    13·1 answer
  • A converging - diverging frictionless nozzle is used to accelerate an airstream emanating from a large chamber. The nozzle has a
    15·2 answers
  • Wet steam at 15 bar is throttled adiabatically in a steady-flow process to 2 bar. The resulting stream has a temperature of 130°
    7·1 answer
  • To understand the concept of moment of a force and how to calculate it using a scalar formulation.
    9·1 answer
  • Technician A says the compressor is the dividing line of the refrigeration system, low- to high-side. Technician B says the expa
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!