-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtry.cpp
More file actions
118 lines (96 loc) · 1.97 KB
/
try.cpp
File metadata and controls
118 lines (96 loc) · 1.97 KB
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
#include <bits/stdc++.h>
using namespace std;
int f1=7;
int f2=3;
int f1_counter=0;
int f2_counter=0;
int divide_check(int a,int b){
if (a==0){
return 1;
}
else if(a<0){
return 0;
}
else{
return divide_check(a-b,b);
}
}
int hcf_by_subs(int a,int b){
for (int i = b; i>=1; i--)
{
if (divide_check(a,i)==1){
if(divide_check(b,i)==1){
return i;
break;
}
}
}
return 1;
}
int multiply(int x,int y){
if(y == 0){
return 0;
}
if(y > 0 ){
return (x + multiply(x, y - 1));
}
if(y < 0 ){
return -1*multiply(x, -y);
}
}
int and_check(int a,int b){
if (a==1){
if(b==1){
return 1;
}
}
if(b==1){
if(a==1){
return 1;
}
}
else
{
return 0;
}
}
int hcf(int a,int b){
if(b==0){
return a;
}
if(a<f1){
return hcf_by_subs(a,b);
}
if (and_check(divide_check(a,f1),divide_check(b,f1))){
f1_counter++;
f2_counter*2;
return hcf(a/f1,b/f1);
}
if (and_check(divide_check(a,f2),divide_check(b,f2))){
f2_counter++;
f1_counter++;
return hcf(a/f2,b/f2);
}
else{
int k= hcf_by_subs(a,b);
if(f1_counter>0){
k=multiply(k,multiply(f1,f1_counter));
cout<<k<<endl;
}
if(f2_counter>0){
k=multiply(k,multiply(f2,f2_counter));
cout<<k<<endl;
}
return k;
}
return 1;
}
int main() {
int n,m;
cout<<"Enter 1st no.: ";
cin>>n;
cout<<"\nEnter 2nd no.: ";
cin>>m;
cout<<hcf(max(n,m),min(n,m));
return 0;
}