Python, add sum of series with factorial

Viewed 279

I need to write a python code for the following problem:

-Write functions to ask the user for a positive integer n and and integer x and display the sum of the series:

1+1⁄1!+2⁄2!+3⁄3!+⋯+⁄!

Requirement:

  1. Input validation.
  2. Write a function to calculate factorials for a number.
  3. Write a function to calculate the sum of the series.
  4. import library are not allowed.

so basically, it needs to ask the user for positive int (n) and positive int (x) to solve 1+1⁄1!+2⁄2!+3⁄3!+⋯+⁄! and then display the sum. it should be two equations I think and no math libraries.

This is the code I have so far:

n = int(input('Enter a positive integer: '))
x = int(input('Enter another positive integer: '))
#  n >= 0
fact(x)

I am stuck on how to continue past the user input, should i use a for loop or?

n = int(input('Enter a positive integer: '))
# x = int(input('Enter another positive integer: '))
answer = 1
for i in range (n,0,-1):
    result = answer*i
print(n, answer)

so, i figured out how to do just the factorial, but now how do i the rest of the equation?

3 Answers

It seems what you want is:

def fact(k): # k!
    out = 1
    for i in range(1, k+1):
        out *= i
    return out

result = 1
for i in range(1, n+1):
    result += x**i / fact(i)

Write functions to ask the user for a positive integer n and and integer x and ...

What you need is a function, so, for the first step, I strongly encourage you to avoid using the print function. It seems, in addition to the problem you mentioned, there are some logical errors too. Since the problem has required you to write functions, I intended to solve it using these functions as much as possible. What I have come up with is what follows:

# 4. No importing was intended

# 2. Write a function to calculate factorials for a number.
def fact(n):
  mult = 1
  for i in range(1, n+1):
    mult *= i
  return mult

# 3. Write a function to calculate factorials for a number.
def sum_series(s):
  temp = 0
  for value in s:
    temp += value
  return temp

def main():
  n = input('Enter a positive integer: ')
  x = input('Enter another positive integer: ')

  # 1. Input validation
  if not n.isnumeric():
    return False
  if not x.isnumeric():
    return False
  n = int(n)
  x = int(x)

  tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append((x**i)/fact(i))
  return sum_series(tempSeries)

Example

So, if we gave 2 as n and 3 as x we would have something like:

1 + (3^1)/1 + (3^2)/2 = 8.5

Let's check the function:

main()

Output

Enter a positive integer: 2
Enter another positive integer: 3
8.5

I am stuck on how to continue past the user input, should i use a for loop or?

You need to validate input from the user and ask to write the proper one, so you can use the function here:

def ask_user(): # Define function that asks to type variables
    n = input('Enter a positive integer for n: ')
    
    while True:
        if n[0] == "-" and n[1::].isnumeric(): 
        # If the variable is not positive number, ask to type positive
            n = input("Please, provide positive integer: ")
        elif not n.isnumeric():
        # If the variable is not number at all
            n = input("Please, provide number for n: ")
        else:
            break
    
    x = input('Enter a positive integer for x: ')
    
    while True:
        if x[0] == "-" and x[1::].isnumeric():
            x = input("Please, provide positive integer: ")
        elif not x.isnumeric():
            x = input("Please, provide number for x: ")
        else:
            break
    

    return int(n), int(x)

n, x = ask_user()
print(n)
print(x)

It gives the next output:

Enter a positive integer for n: haha
Please, provide number for n: -5
Please, provide positive integer: 5
Enter a positive integer for x: blabla
Please, provide number for x: -99
Please, provide positive integer: 2
5
2

About equation part

You need factorial function:

def factorial(n):
  result = 1
  for i in range(1, n+1):
    result *= i
  return result

(X!^N)/N

I need more data about it, but if your equation is next:

(X!^N)/N

Then you can use the function here as mentioned by Amirhossein Kiani, but a little bit redesigned:

tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append((factorial(x)**i)/i)
  return sum_series(tempSeries)

(X^(N)/N)!

tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append(factorial((x**i)/i))
  return sum_series(tempSeries)

(X^N)!/N

tempSeries = [1]
  for i in range (1,n+1):
      tempSeries.append(factorial(x**i)/i)
  return sum_series(tempSeries)

And so on...

Related