In [1]:
from math import sqrt

def firstSquareGreaterThan(x: int) -> int:
    r = x + 1
    while True:
        sr = sqrt(r)
        if int(sr) == sr:
            return r
        r = r + 1
        
print(firstSquareGreaterThan(4))
9
In [3]:
from math import sqrt

def firstSquareGreaterThan(x: int) -> int:
    r = x + 1
    while int(sqrt(r)) != sqrt(r):
        r = r + 1
    return r
        
print(firstSquareGreaterThan(4))
9
In [8]:
def sqrtButTerrible(n: float) -> float:
    assert n >= 0, "Imaginary numbers unsupported"
    g = n/2
    print("Initial guess:", g)
    while abs(g*g - n) > 0.0001:
        print("Guess so far:", g, "(n/g is", n/g, ")")
        g = (g + n/g) / 2
    print("Final answer:", g)
    return g

print(sqrtButTerrible(24000), sqrt(24000))
Initial guess: 12000.0
Guess so far: 12000.0 (n/g is 2.0 )
Guess so far: 6001.0 (n/g is 3.999333444425929 )
Guess so far: 3002.499666722213 (n/g is 7.993339771524592 )
Guess so far: 1505.2465032468688 (n/g is 15.944232355452193 )
Guess so far: 760.5953678011605 (n/g is 31.554228458401848 )
Guess so far: 396.0747981297812 (n/g is 60.594615242689486 )
Guess so far: 228.33470668623534 (n/g is 105.10885685450984 )
Guess so far: 166.7217817703726 (n/g is 143.95239629249775 )
Guess so far: 155.33708903143517 (n/g is 154.50270215339995 )
Guess so far: 154.91989559241756 (n/g is 154.91877210621269 )
Final answer: 154.91933384931514
154.91933384931514 154.91933384829667
In [9]:
def factorize(n: int) -> None:
    f = 1
    while f <= n:
        if n%f == 0:
            print(f)
        f = f + 1
        
factorize(24000)
1
2
3
4
5
6
8
10
12
15
16
20
24
25
30
32
40
48
50
60
64
75
80
96
100
120
125
150
160
192
200
240
250
300
320
375
400
480
500
600
750
800
960
1000
1200
1500
1600
2000
2400
3000
4000
4800
6000
8000
12000
24000
In [12]:
def factorize(n: int) -> None:
    for f in range(1, n+1):
        if n%f == 0:
            print(f)
    print(f)
        
factorize(24000)
1
2
3
4
5
6
8
10
12
15
16
20
24
25
30
32
40
48
50
60
64
75
80
96
100
120
125
150
160
192
200
240
250
300
320
375
400
480
500
600
750
800
960
1000
1200
1500
1600
2000
2400
3000
4000
4800
6000
8000
12000
24000
24000
In [14]:
for c in range(100, 0):
    print(c)
In [17]:
def countdown(frm: int, to: int) -> None:
    for ct in range(frm, to, -1):
        ct = ct * 2
        print(ct)
        to = to + 1
    print("Ignition")

countdown(10, 0)
20
18
16
14
12
10
8
6
4
2
Ignition
In [18]:
for i in range(1, 4):
    print("In for loop, i =", i)
    while i < 4:
        print("In while loop, i =", i)
        print("x")
        i = i + 1
In for loop, i = 1
In while loop, i = 1
x
In while loop, i = 2
x
In while loop, i = 3
x
In for loop, i = 2
In while loop, i = 2
x
In while loop, i = 3
x
In for loop, i = 3
In while loop, i = 3
x
In [23]:
q = abs
print(q(-3))
print(abs(-3))
print(q == abs)
w = print
w(q(-42))
w(w)
3
3
True
42
<function print at 0x7fd1f4423d00>
In [26]:
import typing

def primeFactors(n: int, cb: typing.Callable) -> None:
    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

def printSqrts(x: int) -> None:
    print(sqrt(x))
        
primeFactors(42, print)
primeFactors(42, printSqrts)
2
3
7
1.4142135623730951
1.7320508075688772
2.6457513110645907
In [31]:
from unittest.mock import Mock
debug = Mock()

def sqrtButTerrible(n: float) -> float:
    assert n >= 0, "Imaginary numbers unsupported"
    g = n/2
    debug("Initial guess:", g)
    while abs(g*g - n) > 0.0001:
        debug("Guess so far:", g, "(n/g is", n/g, ")")
        g = (g + n/g) / 2
    debug("Final answer:", g)
    return g

print(sqrtButTerrible(24000), sqrt(24000))
154.91933384931514 154.91933384829667
In [32]:
def interest(base: float, interestRate: float, numberOfPeriods: int) -> float:
    value = base
    for _ in range(numberOfPeriods):
        value = value * (1 + interestRate)
        # value = value + (value * interestRate)
    return value

print(interest(1800, 0.055, 24))
6506.261827030953
In [33]:
def leibniz(steps: int) -> float:
    pi = 0.0
    sign = 1
    for den in range(1, steps*2, 2):
        pi = pi + sign*4/den
        sign = sign * -1
    return pi

print(leibniz(100000))
3.1415826535897198