-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinding HCF in different ways
65 lines (44 loc) · 1.12 KB
/
Finding HCF in different ways
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
# way 1
def compute_hcf(num1, num2):
if num1 == 0:
return num2
else:
return (compute_hcf(num2 % num1, num1))
# way2
def hcf(n1, n2):
if (n1 > n2):
small = 2
else:
small = n1
# using Ternary operator
# for i in ((n1 if n1 > n2 else n2), 0, -1):
# if n1 % i == 0 and n2 % i == 0:
# break
for i in range(small + 1, 0, -1):
if n1 % i == 0 and n2 % i == 0:
break
return i
# way 3
def gcd(n1, n2):
while (n2 != 0):
n1, n2 = n2, n1 % n2
return n1
# way 4
def gcd1(n1, n2):
return n1 if n2 == 0 else gcd1(n1, n2 % n1)
# way 5
def hcf2(n1, n2):
while (n1 != n2):
n1, n2 = (n1-n2, n2) if n1 > n2 else (n2, n2-n1)
# if (n1 > n2):
# n1 -= n2
# if (n2 > n1):
# n2 -= n1
return n1
# way 6
def hcf3(n1, n2):
n1, n2 = (n1-n2, n2) if n1 > n2 else (n2, n2-n1)
return n1 if n1 == n2 else hcf3(n1, n2)
num1 = 17
num2 = 0
print(f"The H.C.F of {num1} and {num2} is {compute_hcf(num1,num2)}")