The nth harmonic number is defined non-recursively as: 1 +1/2 + 1/3 + 1/4 + ... + 1/n. Come up with a recursive definition and use it to guide you to write a function definition for a double -valued function named harmonic that accepts an int parameters n and recursively calculates and returns the nth harmonic number. this is for myprogramminglab.com this is what i have double harmonic(int n) { int sum =0; if (n>0) { sum=sum+(1/(harmonic( n-1))); } return sum; }

Respuesta :

Answer:

See the code below.

Explanation:

The nth armonic number is obtained from the following induction process:

[tex]a_1 = 1[/tex]

[tex]a_2 = 1+\frac{1}{2}=a_1 +1[/tex]

[tex]a_3 = 1+\frac{1}{2}+\frac{1}{3}=a_2 +1[/tex]

And for the the n term we have this:

[tex]a_{n-1}=1+\frac{1}{2}+\frac{1}{3}+....+\frac{1}{n-1}[/tex]

[tex] a_n = 1+\frac{1}{2}+\frac{1}{3}+......+\frac{1}{n}=a_{n-1}+\frac{1}{n}[/tex]

In order to create a code for the ne term we can use the following code using python:

# Code to find the nth armonic

# Function to find n-th Harmonic Number  

def armonicseries(n) :  

   # a1 = 1  

   harmonic = 1

   # We need to satisfy the following formulas:  

   # an = a1 + a2 + a3 ... +..... +an-1 + an-1 + 1/n  

   for i in range(2, n + 1) :  

       harmonic += 1 / i  

   return harmonic  

##############################

And then with the following instructions we find the solution for any number n.

   n = 3 # thats the number of n that we want to find

   print(round(armonicseries(n),5))

Recursive functions are functions that execute itself from within.

The harmonic sum function in Python, where comments are used to explain each line is as follows:

#This defines the function

def harmonic_sum(n):

   #This returns 1, if n is less than 2

 if n < 2:

   return 1

 #If otherwise,

 else:

     #This calculates the harmonic sum, recursively

   return 1 / n + (harmonic_sum(n - 1))

Read more about recursive functions at:

https://brainly.com/question/15898095