Week 3 assignment 1 Getting started with python

Data Science Course Week 3 assignment 1:
Can anyone explain what’s the difference between question 3 and 4 in problem 2 ?

Please put the relevant questions directly here.

Capture
Question 3 and 4 in the image

For Q3, you need to create a function which takes 2 arguments (x, n) and returns the desired value.

For Q4, you need to create a function which takes (N, x) as arguments, and returns sum of desired values for all ‘n’ where n goes from 1 to N.
Note: Simply loop over all the values from 1 to N, and use the function you’ve made for Q3, and just do the sum for returned values.

okay is it okay to share the code here? Just want to post the code for both and see if I did it right?

Yes, now you can

Q3:

def factorial_rec(n):

  if n == 0:

    return 1

  else:

    return n * factorial_rec(n-1)

def summation(x,n):

  return (pow(x,n)/factorial_rec(n))

print(summation(3.0,3))

Q4 (first solution)

def factorial_rec(n):
  if n == 0:
    return 1
  else:
    return n * factorial_rec(n-1)
def SumOfSeries(x,n):
  total = 1
  for i in range(1,n+1):
    total = total + (pow(x,i)/factorial_rec(i))
  return total

x = int(input("Enter x value: "))
n = int(input("Enter n value: "))
print(SumOfSeries(x,n))


Q4(alternate solution)

def sumOfSeries(x,num): 
    total = 1
    last=1
    for i in range(1, num+1): 
        last *= (x/i) 
        total = total + last 
    return total
x = float(input("Enter x value: "))
num = int(input("Enter n value: "))

print(sumOfSeries(x,num))