File tree Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -71,13 +71,17 @@ def test_gcd():
71
71
assert i == 2
72
72
i = gcd (21 , 14 )
73
73
assert i == 7
74
+ i = gcd (21 , - 12 )
75
+ assert i == 3
74
76
75
77
def test_lcm ():
76
78
i : i32
77
79
i = lcm (10 , 4 )
78
80
assert i == 20
79
81
i = lcm (21 , 14 )
80
82
assert i == 42
83
+ i = lcm (21 , - 12 )
84
+ assert i == 84
81
85
82
86
83
87
test_factorial_1 ()
Original file line number Diff line number Diff line change @@ -123,6 +123,10 @@ def gcd(a: i32, b: i32) -> i32:
123
123
Returns greatest common divisor of `a` and `b`
124
124
"""
125
125
temp : i32
126
+ if a < 0 :
127
+ a = - a
128
+ if b < 0 :
129
+ b = - b
126
130
while b != 0 :
127
131
a = mod (a , b )
128
132
temp = a
@@ -135,4 +139,10 @@ def lcm(a: i32, b: i32) -> i32:
135
139
"""
136
140
Returns least common multiple of `a` and `b`
137
141
"""
142
+ if a < 0 :
143
+ a = - a
144
+ if b < 0 :
145
+ b = - b
146
+ if a * b == 0 :
147
+ return 0
138
148
return (a * b )// gcd (a , b )
You can’t perform that action at this time.
0 commit comments