Skip to content

Commit cf5c2ba

Browse files
committed
add mod, gcd, and lcm
1 parent ac9ff76 commit cf5c2ba

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/runtime/math.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,30 @@ def pow(x: f64, y: f64) -> f64:
109109
Return `x` raised to the power `y`.
110110
"""
111111
return x**y
112+
113+
114+
def mod(a: i32, b: i32) -> i32:
115+
"""
116+
Returns a%b
117+
"""
118+
return a - (a//b)*b
119+
120+
121+
def gcd(a: i32, b: i32) -> i32:
122+
"""
123+
Returns greatest common divisor of `a` and `b`
124+
"""
125+
temp: i32
126+
while b != 0:
127+
a = mod(a, b)
128+
temp = a
129+
a = b
130+
b = temp
131+
return a
132+
133+
134+
def lcm(a: i32, b: i32) -> i32:
135+
"""
136+
Returns least common multiple of `a` and `b`
137+
"""
138+
return (a*b)//gcd(a, b)

0 commit comments

Comments
 (0)