-2

Can someone tell me how can I write the following in math notation

for x in (range(1,n)):
    var1=1
    var2=0

    var1*=x
    var2+=x
    
    Var3+=var1/var2

I tried SUM((n!)/SUM(n) but I cant seem to get the same answer.

Thank you in advance.

Update: I apologize to everyone I deceived, I just didn't want help in trying to generate an original formula for factorials because for me it was the ultimate challenge and fun. The idea was to use the fact that the below resulted in an approximation for e to find a way to solve for n! only issue is I was too naive to think that the formula for Sum applied towards Fractions... Forgive me community you are AWESOME!!!

I also adjusted this a bit from the previous one.

($\sum_{i=0}^n \frac{\sum_{i=0}^n n}{n!} $)*(3/2 -1/10**N)

num=100
var1=0
var2=1
e8= 0

for x in (range(1,num)):
    var1+=x
    var2*=x
    e8+=var1/var2

Sym=(3/2 -(1/10**num))

e8*=1/Sym
print(Sym)


print(e8) 
  • Python doesn't have an factorial operator, so you'd need to define your own function and call the function, or use a library that does factorials. Also, check what type `sum` takes. It's not a number. – Carcigenicate Jul 14 '20 at 19:01
  • I know, I am trying to write the code to math notation, I think you are thinking about it the other way around – JJ-Pysquared Jul 14 '20 at 19:03

3 Answers3

0
import math
return sum([i/j for i, j in zip([math.factorial(i) for i in range(1, n)], [(i)(i+1)/2 for i in range(1, n)])])

This is a more mathy way to do it, but is less readable. Is this good? Double check that it outputs the correct number, but it should.

Andy Delworth
  • 137
  • 1
  • 12
0

Upon exit of the loop and assuming that Var3 is initialized to 0, Var3 will equal n-1. (I bet this is not what you expect.)


If one moves the (re-)initializations out of the loop, the formula is

enter image description here

  • Correct me if Im wrong but shouldnt I be able to do the same sum formula n(n+1)/2 to the 2k!/k**2+k. When I try it I still cant get the same result(The code shouldve been to n+1) – JJ-Pysquared Jul 19 '20 at 04:23
-1

Sum(n) won't do anything for you. You want to find the sum of (a_1/b_1 + ... + a_n/b_n), where a_i is i! and b_i is the sum of all numbers from 1 to i. A simplified formula for that is n(n+1)/2. To help understand why that works, think of matching up the first and last element, second and second last, etc, until you have added up all n/2 pairs. Each pair's sum is n+1, and there are n/2 of them, so n(n+1)/2 is the total sum. Therefore, we can express the var3 as:

import math #for factorial function
var3 = 0 #im assuming it starts at 0 
for i in range(1, n):
    var3 += math.factorial(i)/((n)(n-1)/2)
Andy Delworth
  • 137
  • 1
  • 12