Teaching Kids Programming – Compute Sum of 1/(1+2+..N) Using Math and Python


Teaching Kids Programming: Videos on Data Structures and Algorithms

Let’s compute the following:

tex_68d4e9fbb622afa4dc6e432423963626 Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

Math Proof of the Equation of Sum of the First N Positive Integers

We know that the sum of the first few positive integers is:

tex_f05beecc86b2f0e2a38b2d33ed06e22e Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

Let’s write it again but in backwards order:

tex_9aefe64fc7863b431fbb5f6496ba5a86 Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

Therefore, tex_27ac7b0c3f5a175846ff787bb218a53f Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

And tex_2f0daefca769b3f573bd56ddc8452cbb Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

So we have tex_d0c18e5a1139734db95314a768560aa5 Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

Compute Sum of 1/(1+2+..N) Using Math and Python

Therefore, we can rewrite the above:

tex_68d4e9fbb622afa4dc6e432423963626 Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video
tex_0cc0cd05df532a2fbbb057be0e653adb Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video
tex_c60eb017f2377bfb3078d1e333fa7bec Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video
tex_8f49b912682335ed713c925857d93f65 Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video
tex_a1656c71461139a94460157c69142769 Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video
Thus, the intermediate terms are canceled out.
tex_a75ee93d531140eb4e3dc720a829cafe Teaching Kids Programming - Compute Sum of 1/(1+2+..N) Using Math and Python algorithms Brute Force Algorithm math python Python teaching kids programming youtube video

Thus, we can implement in Python:

1
2
def f(N):
    return 2 - 2/(N+1)
def f(N):
    return 2 - 2/(N+1)

Alternatively, we can iterate over each term and sum it up (Brute Force Algorithm):

1
2
3
4
5
def f(N):
    ans = 0
    for i in range(1, N+1):
        ans += 2.0 / i*(i+1)
    return ans
def f(N):
    ans = 0
    for i in range(1, N+1):
        ans += 2.0 / i*(i+1)
    return ans

Clearly, the first approach is O(1) constant as it just computes a math equation, and the second method is O(N) linear as it needs to process and sum up N terms.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
794 words
Last Post: Unleashing Innovation: Inside Hackathon Events at Big Internet Companies (Hackathon T-shirt from Microsoft)
Next Post: How to Transfer or Send ETH to Another Wallet on Ethereum Blockchain?

The Permanent URL is: Teaching Kids Programming – Compute Sum of 1/(1+2+..N) Using Math and Python

Leave a Reply