def validTriangle(a: float, b: float, c: float) -> bool:
assert a > 0 and b > 0 and c > 0, "All three side lengths must be positive"
return a < b + c and b < a + c and c < a + b
print(validTriangle(3, 4, 5))
print(validTriangle(3, 4, 15))
True False
c = 100
while c >= 1:
print(c)
c = c - 1
print("Ignition")
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Ignition
c = 1
while c <= 100:
print(c)
c = c + 1
print("Ignition at", c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Ignition at 101
c = 1
while c >= 1:
print(c)
c = c + 1
print("Ignition at", c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) Input In [9], in <cell line: 2>() 1 c = 1 2 while c >= 1: ----> 3 print(c) 4 c = c + 1 5 print("Ignition at", c) File /mnt/zfs/jupyter-p03/home/gkrichar/.ipython/profile_default/startup/cs114-setup.py:5, in print(*args, **kwargs) ----> 5 def print(*args, **kwargs): builtins.print(*args, **kwargs); sys.stdout.flush(); time.sleep(0.04) KeyboardInterrupt:
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(5))
print(firstSquareGreaterThan(9))
9 16
def factorial(n: int) -> int:
r = n
n = n - 1
while n >= 1:
#print("r is", r)
#print("n is", n)
r = r * n
n = n - 1
# Careful!
#return r
return r
print(factorial(5))
120
def sumTo(n: int) -> int:
r = n
n = n - 1
while n >= 1:
r = r + n
n = n - 1
# Careful!
#return r
return r
print(sumTo(5))
15
print(factorial(15.0))
print(factorial(15))
print(factorial(150.0))
print(factorial(150))
print(factorial(1500.0))
print(factorial(1500))
<cell>1: error: Argument 1 to "factorial" has incompatible type "float"; expected "int" [arg-type] <cell>3: error: Argument 1 to "factorial" has incompatible type "float"; expected "int" [arg-type] <cell>5: error: Argument 1 to "factorial" has incompatible type "float"; expected "int" [arg-type]
1307674368000.0 1307674368000 5.7133839564458575e+262 57133839564458545904789328652610540031895535786011264182548375833179829124845398393126574488675311145377107878746854204162666250198684504466355949195922066574942592095735778929325357290444962472405416790722118445437122269675520000000000000000000000000000000000000 inf 48119977967797748601669900935813797818348080406726138081308559411630575189001095591292230585206733851868464009619343585194052091124618166270271481881393331431627962810299844149333789044689395510487167879769325303699470467829234399263326545652860748605075746366928323606645492277541120083438086727369377887676000211405318480244354207419604864176969950581435222198851194568984095705945549589054568321792338919149442985919957734792959402499096845643020401869381175603964424333222114125974374817804242633309769804293952870034619354125014210045647664063240162007560108665290568646128342557147350985358724154623253371867470765120422073867963935775258692109753041762094343569050497470353531764481503174750911858230906998361066084787758316110585736013365377431860738572261325738233656835271947352695180865573043834027955539012765489372645042504406597752357481931532872356635411224578334040522294746402829585458478708778346379431862368824819009177091444034885941394319343910223168655869761799669075059527608502465593181398566214786801211651657222004123456498258513120359126022843038535083709796101565934859483203933443308601475813108363074118562404412420191947127585482919172173045961122122701434297870691932154082986945954748251105782181586397275820342101470457300633590139512919549474113721711616912519714191760699935509810254849967087635936181176363954224186031346682928878492872249485456690138831610135377916327940503701400290125509132140782614640495733518048670983360134097860364762638658894873174499870133559364805443430831459505987809215393353387232078177562975021460595422358573128085417162336030235138652735438053034531962620811566019896879275257163988352090874930346115518331202927263708446729394381879888839549731876978682249320628599631628662375508826209854754631984276392670919216923002770077734756077549035942976209159416211581439461484509549370357486770276807687544580164314647595031368948490282897173328013518435758700056425922638411889496527975846052717958044813737086806600171993703579485864029383208714528950303253881360812631162134750100307772634337467012820470715650810714689905121432259528505483053930402217400686061612471659630192434864094539828085677465383026128353771071152304197549798870706139893609140045659756285435787771636258253666592102151236142132724425850991205720020493660580896600891888594659612927724357866265934517615841298789154462249169688860092640284756382431746120357767933119589280468687348061788072986362788582227019465263474828590646048451070702923434422714349595857654843699542321849363652767771978314681013589442955219879702008068934096624650625769705233333462826013860098698155180331145365652453482955497979915586438474687345677874451117702250441711504844638414485210092261397271970571029038581873069951161330495772310508760528249706514238384269808639507080418298318311361373628512041716415196868334254119137139589149597210032153545941114666530498906529240798164804007394775927836045668573993316428972539932745757171947402454257142633700815922407278403640595355142075599446056337986717212316223257763412164180899532722039383244462511410346646148863397237096276822656157561194665545757017429842404840309758925618650507921043007241637877939825811059339138925526124514467627126548126795078784022672860886251974581362141782786407402896309678008909663263987018538107050886193489012497405005820727271232733728141775132722013860591169620692789290456794698409808557447756701311883266010859016027592252397754508251628808293537776536569608111330584797160694847898923196743970244451842702266403326317319092117151143971679500042590269255093130215984418097418435474300467281949798227102529873732749027992079700287275900856241172902880909546551703263202853584498085358955307673717177961902081098618729046348849060249600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
import math
def printFloat(f: float) -> None:
assert f >= 0, "Can only print positives"
remainder = f
i = int(f)
print(i, ".")
remainder = remainder - i
while remainder > 0:
remainder = remainder * 10
i = int(remainder)
print(i)
remainder = remainder - i
printFloat(math.pi)
3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 1 1 5 9 9 7 9 6 3 4 6 8 5 4 4 1 8 5 1 6 1 5 9 0 5 7 6 1 7 1 8 7 5
def factorize(n: int) -> None:
assert n > 0, "Only positive integers have factors"
f = 1
while f <= n:
if n%f == 0:
print(f)
f = f + 1
factorize(24601)
1 73 337 24601
def gcd(a: int, b: int) -> int:
assert a > 0 and b > 0, "Only positive integers have factors"
gcd = 1
candidate = 2
while candidate <= a and candidate <= b:
print("gcd and candidate:", gcd, candidate)
if a%candidate == 0 and b%candidate == 0:
gcd = candidate
candidate = candidate + 1
return gcd
print(gcd(24601, 146))
gcd and candidate: 1 2 gcd and candidate: 1 3 gcd and candidate: 1 4 gcd and candidate: 1 5 gcd and candidate: 1 6 gcd and candidate: 1 7 gcd and candidate: 1 8 gcd and candidate: 1 9 gcd and candidate: 1 10 gcd and candidate: 1 11 gcd and candidate: 1 12 gcd and candidate: 1 13 gcd and candidate: 1 14 gcd and candidate: 1 15 gcd and candidate: 1 16 gcd and candidate: 1 17 gcd and candidate: 1 18 gcd and candidate: 1 19 gcd and candidate: 1 20 gcd and candidate: 1 21 gcd and candidate: 1 22 gcd and candidate: 1 23 gcd and candidate: 1 24 gcd and candidate: 1 25 gcd and candidate: 1 26 gcd and candidate: 1 27 gcd and candidate: 1 28 gcd and candidate: 1 29 gcd and candidate: 1 30 gcd and candidate: 1 31 gcd and candidate: 1 32 gcd and candidate: 1 33 gcd and candidate: 1 34 gcd and candidate: 1 35 gcd and candidate: 1 36 gcd and candidate: 1 37 gcd and candidate: 1 38 gcd and candidate: 1 39 gcd and candidate: 1 40 gcd and candidate: 1 41 gcd and candidate: 1 42 gcd and candidate: 1 43 gcd and candidate: 1 44 gcd and candidate: 1 45 gcd and candidate: 1 46 gcd and candidate: 1 47 gcd and candidate: 1 48 gcd and candidate: 1 49 gcd and candidate: 1 50 gcd and candidate: 1 51 gcd and candidate: 1 52 gcd and candidate: 1 53 gcd and candidate: 1 54 gcd and candidate: 1 55 gcd and candidate: 1 56 gcd and candidate: 1 57 gcd and candidate: 1 58 gcd and candidate: 1 59 gcd and candidate: 1 60 gcd and candidate: 1 61 gcd and candidate: 1 62 gcd and candidate: 1 63 gcd and candidate: 1 64 gcd and candidate: 1 65 gcd and candidate: 1 66 gcd and candidate: 1 67 gcd and candidate: 1 68 gcd and candidate: 1 69 gcd and candidate: 1 70 gcd and candidate: 1 71 gcd and candidate: 1 72 gcd and candidate: 1 73 gcd and candidate: 73 74 gcd and candidate: 73 75 gcd and candidate: 73 76 gcd and candidate: 73 77 gcd and candidate: 73 78 gcd and candidate: 73 79 gcd and candidate: 73 80 gcd and candidate: 73 81 gcd and candidate: 73 82 gcd and candidate: 73 83 gcd and candidate: 73 84 gcd and candidate: 73 85 gcd and candidate: 73 86 gcd and candidate: 73 87 gcd and candidate: 73 88 gcd and candidate: 73 89 gcd and candidate: 73 90 gcd and candidate: 73 91 gcd and candidate: 73 92 gcd and candidate: 73 93 gcd and candidate: 73 94 gcd and candidate: 73 95 gcd and candidate: 73 96 gcd and candidate: 73 97 gcd and candidate: 73 98 gcd and candidate: 73 99 gcd and candidate: 73 100 gcd and candidate: 73 101 gcd and candidate: 73 102 gcd and candidate: 73 103 gcd and candidate: 73 104 gcd and candidate: 73 105 gcd and candidate: 73 106 gcd and candidate: 73 107 gcd and candidate: 73 108 gcd and candidate: 73 109 gcd and candidate: 73 110 gcd and candidate: 73 111 gcd and candidate: 73 112 gcd and candidate: 73 113 gcd and candidate: 73 114 gcd and candidate: 73 115 gcd and candidate: 73 116 gcd and candidate: 73 117 gcd and candidate: 73 118 gcd and candidate: 73 119 gcd and candidate: 73 120 gcd and candidate: 73 121 gcd and candidate: 73 122 gcd and candidate: 73 123 gcd and candidate: 73 124 gcd and candidate: 73 125 gcd and candidate: 73 126 gcd and candidate: 73 127 gcd and candidate: 73 128 gcd and candidate: 73 129 gcd and candidate: 73 130 gcd and candidate: 73 131 gcd and candidate: 73 132 gcd and candidate: 73 133 gcd and candidate: 73 134 gcd and candidate: 73 135 gcd and candidate: 73 136 gcd and candidate: 73 137 gcd and candidate: 73 138 gcd and candidate: 73 139 gcd and candidate: 73 140 gcd and candidate: 73 141 gcd and candidate: 73 142 gcd and candidate: 73 143 gcd and candidate: 73 144 gcd and candidate: 73 145 gcd and candidate: 73 146 73
def primeFactors(n: int) -> 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)
least = f
n = n // f
primeFactors(24600)
2 2 2 3 5 5 41