total1 = 0
total2 = 0

for i in range(3,52,3):
    total1 = total1 + i
    total2 += i # shorter form

print("The summation is",total1)
print("The summation is",total2)

T = 0 
current_number = 1
for i in range(9):
    T = T + current_number
    print("3 to",i,"is",current_number)
    current_number = 3 * current_number
    
print("summation is",T)

# Python has also exponent operator: **
# we can also directly use it

T = 0

for i in range(9):
    print("3 to",i,"is",3**i)
    T = T + 3 ** i
    
print("summation is",T)

T = 0
n = 2  # this value iteratively will be first halved and then added to the summation T
how_many_terms = 0

while T<=1.99:
    n = n/2 # half the value of n
    print("n = ",n)
    T = T + n # update the value of T
    how_many_terms = how_many_terms + 1
    
print("T = ",T)
print("how many terms in the summation:",how_many_terms)

# our result says that there should be 8 terms in our summation
# let's calculate the summations of the first seven and eight terms, and verify our results

T7 = 0 
n = 2 # this value iteratively will be first halved and then added to the summation

for i in range(7):
    n = n/2
    print("n =",n)
    T7 = T7 + n
    
print("the summation of the first seven terms is",T7)

T8 = 0 
n = 2 # this value iteratively will be first halved and then added to the summation

for i in range(8):
    n = n/2
    print("n =",n)
    T8 = T8 + n
    
print("the summation of the first eight terms is",T8)

print("(the summation of the first seven terms is",T7,")")

from random import randrange

r = 0
attempt = 0

while r != 3: # the loop iterates as long as r is not equal to 3
    r = randrange(10) # randomly pick a number
    attempt = attempt + 1 # increase the number of attempts by 1
    print (attempt,"->",r) # print the number of attempt and the randomly picked number
    
print("total number of attempt(s) is",attempt)

# be aware of single and double indentions

number_of_execution = 2000 # change this with 200, 2000, 20000, 200000 and reexecute this cell
total_attempts = 0  

from random import randrange

for i in range(number_of_execution): # the outer loop iterates number_of_execution times
    r = 0 
    attempt = 0
    while r != 3: # the while-loop iterates as long as r is not equal to 3
        r = randrange(10) # randomly pick a number
        attempt = attempt + 1 # increase the number of attempts by 1
    # I am out of scope of while-loop
    total_attempts = total_attempts + attempt # update the total number of attempts
    
# I am out of scope of for-loop
print(number_of_execution,"->",total_attempts/number_of_execution)

# let's use triple nested loops
for number_of_execution in [20,200,2000,20000,200000]: # we will use the same code by indenting all lines by one more level
    total_attempts = 0 
    for i in range(number_of_execution): # the middle loop iterates number_of_execution times
        r = 0 
        attempt = 0
        while r != 3: # the while-loop iterates as long as r is not equal to 3
            r = randrange(10) # randomly pick a number
            attempt = attempt + 1 # increase the number of attempts by 1
        # I am out of scope of while-loop
        total_attempts = total_attempts + attempt # update the total number of attempts
    # I am out of scope of for-loop
    print(number_of_execution,"->",total_attempts/number_of_execution)

# you can include 2 million to the list, but you should WAIT for a while to see the result
# can your computer compete with exponential growth?
# if you think "yes", please try 20 million, 200 million, and so on

