Skip to content

Commit ae505cc

Browse files
authored
Create 18 July LCM Triplet (#843)
2 parents e05df3b + 6c997d2 commit ae505cc

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

18 July LCM Triplet

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Solution {
2+
3+
public:
4+
5+
int gcd(int a, int b)
6+
7+
{
8+
9+
if (a == 0)
10+
11+
return b;
12+
13+
return gcd(b % a, a);
14+
15+
}
16+
17+
18+
19+
long long lcmTriplets(long long N) {
20+
21+
long long y, x = N, f, s, c = 0;
22+
23+
if(N == 2 || N == 1)
24+
25+
return N;
26+
27+
if(N&1)
28+
29+
return (N)*(N-1)*(N-2);
30+
31+
else {
32+
33+
x -= 2; y=x;
34+
35+
while(1 && c < 1)
36+
37+
{
38+
39+
if(gcd(x, N) == 1) {
40+
41+
f = x*N*(N-1);
42+
43+
++c;
44+
45+
};
46+
47+
--x;
48+
49+
}c=0; N=N-1; --y;
50+
51+
while(1 && c < 1 ) {
52+
53+
if(gcd(y, N) == 1) {
54+
55+
s = y*N*(N-1);
56+
57+
++c;}
58+
59+
--y;
60+
61+
}
62+
63+
64+
65+
return max(f, s);
66+
67+
}
68+
69+
}
70+
71+
};

0 commit comments

Comments
 (0)