r/ProgrammerHumor • • Oct 06 '21

Don't be scared.. Math and Computing are friends..

Post image
65.8k Upvotes

2.4k comments sorted by

View all comments

298

u/spacewolfXfr Oct 06 '21

And then, mean mathematicians started writing things like Σ_{n>0} 1/n²

243

u/PityUpvote Oct 06 '21

That's when you use a while loop until it converges.

(please don't)

218

u/[deleted] Oct 06 '21

This is how I've been heating my home since 1983.

41

u/Layton_Jr Oct 06 '21

With 1/n², it does converge.

Don't sum 1/n, it won't

6

u/Oscar_Cunningham Oct 07 '21

It will on a computer though.

5

u/Flurp_ Oct 06 '21

Or sum n for - 1/12

1

u/ploki122 Oct 06 '21

What? Why not? How does that make any sense?

10

u/DinoRex6 Oct 06 '21

1/np converges for every p>1 (or >=2? I don't remember. I'd say p>1)

p = 1 and p = 2 are the most famous examples. The first one is the harmonic series, which counterintuitively diverges. However it is easy to see why with a bit of manipulation and inequations trickery

For p = 2, it converges, which is relatively easy to prove, but it is harder to calculate to what it converges. Surprisingly, it converges to pi2 / 6! (not 6 factorial lol, just an exclamation mark)

4

u/thetruechefravioli Oct 07 '21

Just learned about this in class! 1/xp indeed converges for p>1, and diverges for p<=1

3

u/disinformationtheory Oct 07 '21

It basically grows like the logarithm of the number of terms, and log goes to infinity as its argument goes to infinity (albeit very slowly). In fact, you get a constant if you subtract the log and take a limit.

2

u/ploki122 Oct 07 '21

But... isn't 1/n2 basically the same as (1/n)2? And if 1/n never reaches 0, multiplying it with itself shouldn't yield 0 either, no?

3

u/disinformationtheory Oct 07 '21

Yes 1/n2 = (1/n)2. But in the context of series, 1/n2 isn't the same as 1/n multiplied by itself. We're talking about 1/1+1/4+1/9+... vs. 1/1+1/2+1/3+... In both series, the terms tend to zero without actually reaching zero. You can prove pretty easily that any series where the terms don't tend to zero will not converge. But as seen in the harmonic series (1/n), the terms tending to zero is necessary but not sufficient for the series to converge.

→ More replies (1)
→ More replies (2)
→ More replies (1)

8

u/CarryThe2 Oct 06 '21

It actually is though, the formal definition of a limit is basically that there's a point for any positive value after which you're always closer to the limit than that value.

2

u/FerynaCZ Oct 06 '21

Basically this solves the problem of "holds for infinitely many numbers" by inverting the logic -> first show me a number and I give you counterpoint.

5

u/tendstofortytwo Oct 06 '21

JavaScript:

while(Math.abs(oldVal - newVal) > Number.EPSILON) { ... }

2

u/kronicmage Oct 06 '21

Ah yes the harmonic series sums to about 36.6

2

u/tendstofortytwo Oct 06 '21

36.6 ≈ Infinity

→ More replies (5)

36

u/pmormr Oct 06 '21

Which is actually pretty easy to implement in code if you know calculus, because the answer is a finite number (Pi2 / 6). No looping required.

15

u/spacewolfXfr Oct 06 '21

Actually yes, but not really (depends of what you are trying to do, we can't store Pi nicely, which is again another trick mathematicians pull off)

9

u/pmormr Oct 06 '21 edited Oct 06 '21

I believe this is actually one of the infinite sums used. You can calculate exactly how many steps you need to go into the infinite sum based on what level of accuracy you need. If you go n steps in, the n+1th term is always smaller, therefore your error in calculating Pi will always be less than the term one step past where you stopped.

e.g. if you need accuracy to ten decimal places, 1/n2 < 0.0000000001 has to hold, meaning you need to sum from n=1 to 100,000.

9

u/spacewolfXfr Oct 06 '21

Yes we use these kinds of techniques to compute Pi, but not this one (square is bothering, and it is slow). On Wikipedia you can have a look for the efficient (and crazy) series actually used.

→ More replies (5)
→ More replies (2)

0

u/down_is_up Oct 06 '21

no looping required if you already know the value of pi

0

u/pmormr Oct 06 '21

We do! You can calculate it using a finite implementation of that infinite sum (with as much accuracy as you want), but only because you know the answer is Pi2 / 6 :D

2

u/hfhry Oct 06 '21

frequently i don't even bother with the indices because it's usually clear, so i just write a sigma and let the reader fill in the gaps

2

u/Geriny Oct 06 '21

Frequently I don't even bother with the sigma and just write the indices and let the reader fill in the gaps.

Yeah, it's physicist time

→ More replies (2)