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
Can you help me with this
Fiesta28 [93]

the function is to provide sealed combustion so that the loss of gas is minimized

6 0
2 years ago
A 75- kw, 3-, Y- connected, 50-Hz 440- V cylindrical synchronous motor operates at rated condition with 0.8 p.f leading. the mot
Thepotemich [5.8K]

78950W the answer

Explanation:

A 75- kw, 3-, Y- connected, 50-Hz 440- V cylindrical synchronous motor operates at rated condition with 0.8 p.f leading. the motor efficiency excluding field and stator losses, is 95%and X=2.5ohms. calculate the mechanical power developed, the Armature current, back e.m.f, power angle and maximum or pull out torque of the motor

A 75- kw, 3-, Y- connected, 50-Hz 440- V cylindrical synchronous motor operates at rated condition with 0.8 p.f leading. the motor efficiency excluding field and stator losses, is 95%and X=2.5ohms. calculate the mechanical power developed, the Armature current, back e.m.f, power angle and maximum or pull out torque of the motor

5 0
3 years ago
CS3733: Homework/Practice 05 Suppose we would like to write a program called monitor which allows two other programs to communic
valina [46]

Answer:

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/types.h>

#include<string.h>

#include<pthread.h>

//#include<sys/wait.h>

int main(int argc, char** argv)

{

int fd1[2];

int fd2[2];

int fd3[2];

int fd4[2];

char message[] = "abcd";

char input_str[100];

pid_t p,q;

if (pipe(fd1)==-1)

{

 fprintf(stderr, "Pipe Failed" );

 return 1;

}

if (pipe(fd2)==-1)

{

 fprintf(stderr, "Pipe Failed" );

 return 1;

}

if (pipe(fd3)==-1)

{

 fprintf(stderr, "Pipe Failed" );

 return 1;

}

if (pipe(fd4)==-1)

{

 fprintf(stderr, "Pipe Failed" );

 return 1;

}

p = fork();

if (p < 0)

{

 fprintf(stderr, "fork Failed" );

return 1;

}

// child process-1

else if (p == 0)

{

 close(fd1[0]);// Close reading end of first pipe

 char concat_str[100];

 printf("\n\tEnter meaaage:"):

 scanf("%s",concat_str);

 write(fd1[1], concat_str, strlen(concat_str)+1);

 // Concatenate a fixed string with it

 int k = strlen(concat_str);

 int i;

 for (i=0; i<strlen(fixed_str); i++)

 {

  concat_str[k++] = fixed_str[i];

 }

 concat_str[k] = '\0';//string ends with '\0'

 // Close both writting ends

 close(fd1[1]);

 wait(NULL);

//.......................................................................

 close(fd2[1]);

 read(fd2[0], concat_str, 100);

 if(strcmp(concat_str,"invalid")==0)

 {

 printf("\n\tmessage not send");

 }

 else

 {

  printf("\n\tmessage send to prog_2(child_2).");

 }

 close(fd2[0]);//close reading end of pipe 2

 exit(0);

}

else

{

 close(fd1[1]);//Close writting end of first pipe

 char concat_str[100];

 read(fd1[0], concal_str, strlen(concat_str)+1);

 close(fd1[0]);

 close(fd2[0]);//Close writing end of second pipe

 if(/*check if msg is valid or not*/)

 {

  //if not then

  write(fd2[1], "invalid",sizeof(concat_str));

  return 0;

 }

 else

 {

  //if yes then

  write(fd2[1], "valid",sizeof(concat_str));

  close(fd2[1]);

  q=fork();//create chile process 2

  if(q>0)

  {

   close(fd3[0]);/*close read head offd3[] */

   write(fd3[1],concat_str,sizeof(concat_str);//write message by monitor(main process) using fd3[1]

   close(fd3[1]);

   wait(NULL);//wait till child_process_2 send ACK

   //...........................................................

   close(fd4[1]);

   read(fd4[0],concat_str,100);

   close(fd4[0]);

   if(sctcmp(concat_str,"ack")==0)

   {

    printf("Messageof child process_1 is received by child process_2");

   }

   else

   {

    printf("Messageof child process_1 is not received by child process_2");

   }

  }

  else

  {

   if(p<0)

   {

    printf("Chiile_Procrss_2 not cheated");

   }

   else

   {

     

    close(fd3[1]);//Close writing end of first pipe

    char concat_str[100];

    read(fd3[0], concal_str, strlen(concat_str)+1);

    close(fd3[0]);

    close(fd4[0]);//Close writing end of second pipe

    write(fd4[1], "ack",sizeof(concat_str));

     

   }

  }

 }

 close(fd2[1]);

}

}

8 0
4 years ago
Consider casting a concrete driveway 40 feet long, 12 feet wide and 6 in. thick. The proportions of the mix by weight are given
Akimi4 [234]

Answer:

Weight of cement = 10968 lb

Weight of sand = 18105.9 lb

Weight of gravel = 28203.55 lb

Weight of water = 5484 lb

Explanation:

Given:

Entrained air = 7.5%

Length, L = 40 ft

Width,w = 12 ft

thickness,b= 6 inch, convert to ft = 6/12 = 0.5 ft

Specific gravity of sand = 2.60

Specific gravity of gravel = 2.70

The volume will be:

40 * 12 * 0.5 = 240 ft³

We need to find the dry volume of concrete.

Dry volume = wet volume * 1.54 (concrete)

Dry volume will be = 240 * 1.54 = 360ft³

Due to the 7% entarained air content, the required volume will be:

V = 360 * (1 - 0.07)

V = 334.8 ft³

At a ratio of 1:2:3 for cement, sand, and gravel respectively, we have:

Total of ratio = 1+2+3 = 6

Their respective volume will be =

Volume of cement = \frac{1}{6}*334.8 = 55.8 ft^3

Volume of sand = \frac{2}{6}*334.8 = 111.6 ft^3

Volume of gravel = \frac{3}{6}*334.8 = 167.4 ft^3

To find the pounds needed the driveway, we have:

Weight = volume *specific gravity * density of water

Specific gravity of cement = 3.15

Weight of cement =

55.8 * 3.15 * 62.4 = 10968 pounds

Weight of sand =

111.6 * 2.60 * 62.4 = 18105.9 lb

Weight of gravel =

167.4 * 2.7 * 62.4 = 28203.55 lb

Given water to cement ratio of 0.50

Weight of water = 0.5 of weight of cement

= 1/2 * 10968 = 5484 lb

4 0
4 years ago
What is Elon Musk mad about?
boyakko [2]

Answer:

Tesla CEO Elon Musk tweeted that the company's stock was too high, and it immediately dropped in value. The tweet may have violated a deal Musk made with the SEC about his tweets and Tesla. Musk also tweeted patriotic lyrics, said his girlfriend Grimes is mad at him, and noted that their child is due on Monday

6 0
3 years ago
Other questions:
  • What is shown in the above figure?
    11·2 answers
  • Which type of door consists of thin pieces of wood, glass, or louvers placed within a framed rectangular area?
    7·2 answers
  • Jen is developing the positioning statement for a new line of sunglasses. In a meeting, the marketing team tells Jen that she ha
    9·1 answer
  • A fuel cell vehicle draws 50 kW of power at 70 mph and is 40% efficient at rated power. You are asked to size the fuel cell syst
    15·1 answer
  • What fuel do rockets use
    9·2 answers
  • A car accelerates uniformly from rest to 60 km/h in 30 s. What is its displacement during this time?
    7·1 answer
  • 3. (20 points) Suppose we wish to search a linked list of length n, where each element contains a key k along with a hash value
    7·1 answer
  • A customer complains that the heater works sometimes, but sometimes only cold air comes out while driving. Technician A says tha
    14·1 answer
  • un contenedor de 0.01m∧3 se llena con 2kg de nitrogeno a una presion de 15mpa ¿cual es la temperatura del nitrogeno?resolver uti
    8·1 answer
  • In a medical lab, Sandrine is working to isolate one element from a sample of liquid material. She uses a centrifuge, a machine
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!