In [1]:
from math import sqrt

def firstSquareGreaterThan(x: int) -> int:
    """
    Returns the first square number greater than x.
    """
    r = x + 1
    while True:
        sr = sqrt(r)
        if int(sr) == sr:
            return r
        r = r + 1
        
print(firstSquareGreaterThan(4200))
4225
In [3]:
def isSquare(n: int) -> bool:
    """
    Returns True if n is a square number.
    """
    sn = sqrt(n)
    return int(sn) == sn

def firstSquareGreaterThan(x: int) -> int:
    """
    Returns the first square number greater than x.
    """
    r = x + 1
    #while int(sqrt(r)) != sqrt(r):
    while not isSquare(r):
        r = r + 1
    return r

print(firstSquareGreaterThan(4200))
4225
In [6]:
def sqrtButTerrible(n: float) -> float:
    """
    Returns the (positive) square root of n.
    """
    assert n >= 0, "Imaginary numbers unsupported"
    g = n/2
    while abs(g*g - n) > 0.0000001:
        print("Guess so far is", g)
        g = (g + n/g) / 2
    return g

print("sqrt of 2 is", sqrtButTerrible(2), "(", sqrt(2), ")")
print("sqrt of 9 is", sqrtButTerrible(9), "(", sqrt(9), ")")
Guess so far is 1.0
Guess so far is 1.5
Guess so far is 1.4166666666666665
Guess so far is 1.4142156862745097
sqrt of 2 is 1.4142135623746899 ( 1.4142135623730951 )
Guess so far is 4.5
Guess so far is 3.25
Guess so far is 3.0096153846153846
Guess so far is 3.000015360039322
sqrt of 9 is 3.0000000000393214 ( 3.0 )
In [8]:
def factorize(n: int) -> None:
    """
    Prints the factors of n.
    """
    assert n > 0, "Only positive integers supported"
    for f in range(1, n+1):
        if n%f == 0:
            print(f)
            
factorize(4200)
1
2
3
4
5
6
7
8
10
12
14
15
20
21
24
25
28
30
35
40
42
50
56
60
70
75
84
100
105
120
140
150
168
175
200
210
280
300
350
420
525
600
700
840
1050
1400
2100
4200
In [9]:
def factorize(n: int) -> None:
    """
    Prints the factors of n.
    """
    assert n > 0, "Only positive integers supported"
    f = 1
    while f <= n:
        if n%f == 0:
            print(f)
        f = f + 1
        
factorize(4200)
1
2
3
4
5
6
7
8
10
12
14
15
20
21
24
25
28
30
35
40
42
50
56
60
70
75
84
100
105
120
140
150
168
175
200
210
280
300
350
420
525
600
700
840
1050
1400
2100
4200
In [18]:
def countdown(fr: int, to: int) -> None:
    """
    Prints a countdown from fr to to.
    """
    for ct in range(fr, to, -1):
        ct = ct + 1
        print(ct)
    print("Ignition")
    
countdown(5, 0)
6
5
4
3
2
Ignition
In [19]:
for i in range(1, 4):
    while i < 4:
        print("x")
        i = i + 1
x
x
x
x
x
x
In [23]:
q = abs
print(q(-30))
print(abs(-30))
print(q == abs)
w = print
w(q(-42))
30
30
True
42
In [27]:
import typing

def primeFactors(n: int, cb: typing.Callable) -> None:
    """
    Calls cb with each of the prime factors of n.
    """
    assert n > 0, "Only positive integers have factors"
    least = 2
    while n > 1:
        f = least
        while f < n and n%f != 0:
            f = f + 1
        # print(f)
        cb(f)
        least = f
        n = n // f
        
#primeFactors(4200, print)

def printEvenIfTwo(n: int) -> None:
    """
    Prints “It's even” if n is 2.
    """
    #print("n is", n)
    if n == 2:
        print("It's even")
        
primeFactors(4200, printEvenIfTwo)
It's even
It's even
It's even
In [35]:
from unittest.mock import Mock

debug = Mock()

def sqrtButTerrible(n: float) -> float:
    """
    Returns the (positive) square root of n.
    """
    assert n >= 0, "Imaginary numbers unsupported"
    g = n/2
    while abs(g*g - n) > 0.0000001:
        debug("Guess so far is", g)
        g = (g + n/g) / 2
    debug("Final result is", g)
    return g

print("sqrt of 2 is", sqrtButTerrible(2), "(", sqrt(2), ")")
print("sqrt of 9 is", sqrtButTerrible(9), "(", sqrt(9), ")")
Guess so far is 1.0
Guess so far is 1.5
Guess so far is 1.4166666666666665
Guess so far is 1.4142156862745097
Final result is 1.4142135623746899
sqrt of 2 is 1.4142135623746899 ( 1.4142135623730951 )
Guess so far is 4.5
Guess so far is 3.25
Guess so far is 3.0096153846153846
Guess so far is 3.000015360039322
Final result is 3.0000000000393214
sqrt of 9 is 3.0000000000393214 ( 3.0 )
In [36]:
# quantity = quantity * (1+interest)

def compoundInterest(quantity: float, interest: float, periods: int) -> float:
    """
    Returns the quantity after compounding interest periods-many times.
    """
    for _ in range(periods):
        debug("Quantity is now", quantity)
        quantity = quantity * (1+interest)
    return quantity

print(compoundInterest(6000, 0.0012, 24))
Quantity is now 6000
Quantity is now 6007.200000000001
Quantity is now 6014.4086400000015
Quantity is now 6021.625930368002
Quantity is now 6028.851881484444
Quantity is now 6036.086503742225
Quantity is now 6043.329807546716
Quantity is now 6050.581803315773
Quantity is now 6057.842501479752
Quantity is now 6065.111912481529
Quantity is now 6072.390046776507
Quantity is now 6079.676914832639
Quantity is now 6086.972527130439
Quantity is now 6094.276894162996
Quantity is now 6101.590026435992
Quantity is now 6108.911934467716
Quantity is now 6116.242628789078
Quantity is now 6123.582119943625
Quantity is now 6130.930418487558
Quantity is now 6138.287534989744
Quantity is now 6145.653480031732
Quantity is now 6153.028264207771
Quantity is now 6160.411898124821
Quantity is now 6167.8043924025715
6175.205757673455
In [40]:
# pi = 4/1 - 4/3 + 4/5 - 4/7 ...

def leibnizPi(steps: int) -> float:
    """
    Returns an approximation of pi using the Leibniz formula to steps-many steps.
    """
    # range(1, steps*2+2, 2)
    den = 1
    sign = 1
    res = 0.0
    for _ in range(steps):
        res = res + sign*4/den
        sign = sign * -1
        den = den + 2
    return res

print(leibnizPi(4))
print(leibnizPi(40))
print(leibnizPi(400))
2.8952380952380956
3.116596556793833
3.1390926574960143