Infinite amounts in python

I heard python can do endless amounts. For example, if I want to evaluate an infinite amount:

1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... 

How do i go I am new to python. So I would appreciate it if someone could write all the code and I need to enable / import something.

For example, in wolfram alpha, if I Summation (-1)^(n-1)/(2*n-1) from n=1 to infinity , it gives an answer like 0.785395 . I want the answer to be calculated with the required accuracy, for example, as in the case of Wolfhound alpha to 6 digits.

Also, I watched this post here and tried to imitate this, but it gives me the following errors:

 `NameError: name 'Infinity' is not defined` `NameError: name 'Inf' is not defined` 

Thanks Adhvaitha

+4
source share
5 answers

While it is still finite, you can approximate this series using fractions and decimal :

 from fractions import Fraction from decimal import Decimal repetitions = 100 d = 1 r = Fraction(1, d) for n in range(repetitions): r += Fraction(1, d) - Fraction(1, d + 2) d += 4 print(Decimal(r.numerator)/Decimal(r.denominator)) 

I think this is closest to what you want to do.

+7
source

Python has unlimited precision integers, but not unlimited precision floats. There are packages you can use that provide this.

And nothing can "fill" an infinite amount, since it includes an infinite number of steps. You will need to find a closed form for the sum, and then evaluate it or accept the approximation achieved by completing the infinite sum while fulfilling the accuracy criterion.

+4
source

A little shot in the dark here ... I'm sure when you heard that Python can make infinite amounts, they meant that in Python long integers have unlimited accuracy .

Clearly, this has nothing to do with the summation of infinite series.

I am not aware of any Python facet that would make it particularly well suited for calculating such sums (or even determining convergence of sums).

You can try direct summation of terms with some reasonable stopping criteria. However, this will only work for well-executed series.

Finally, just to give you some shade of complexity as you ask for, scientific articles are published whose sole purpose is to summarize some small classes of series. The general problem that you pose is not as simple as it might seem.

+4
source
 #It may be late answer but the following works well. repetitions = 50 r = 0.0 for i in range(repetitions): ii=i+1 # because in python index start from 0 r+=((-1)**(ii-1))/(2*ii-1.0) print r #the output is r=0.780398663148, you can increase the repetitions for more accuracy 
+2
source

For some series, such as those shown, you can use an alternating series test to calculate the sum accurate to the desired error. Libraries such as Decimal , GyPy , mpmath or bigfloat , etc., can be used if your calculation affects the accuracy of the built-in floats.

A note on whole approaches :
Although the approximation of the ratio of integers is more accurate, they are completely impractical for real calculations. The reason for this is: 1) adding fractions requires the creation of equal denominators, and this basically requires multiplying the denominators, so by then the size of the numbers is approximately equal to n! (i.e. factorial ); and 2) for an exemplary series, the accuracy of m digits requires m terms. Therefore, even for six-digit accuracy, it is required that the numbers are approximately equal to 1,000,000! = 8 × 10 5,565,708 . For large numbers, this is approximately 10 10 n which quickly becomes completely impractical. Meanwhile, the decimal solution, designed for 6 or 7 or even 40 digits, is trivial.

For example, when solving night strollers, the time and digits of the digits in the denominator or numerator I get:

  nt n_digits_in_denominator 10 0.0003 14 100 0.0167 170 1000 5.5027 1727 10000 ???? ???? (gave up after waiting one hour) 

And it becomes impractical only for 4 digits of accuracy.

So, if you want to accurately calculate the final and small number of terms and express the final result as a ratio, then whole solutions will be a good choice, but if you want to express the final result as a decimal, you'd better just stick to decimal places.

+1
source

All Articles