-
Notifications
You must be signed in to change notification settings - Fork 54
/
lowestLCMandsum.cpp
65 lines (55 loc) · 1.41 KB
/
lowestLCMandsum.cpp
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
/ C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if number is
// prime or not
bool prime(int n)
{
// As 1 is neither prime
// nor composite return false
if (n == 1)
return false;
// Check if it is divided by any
// number then it is not prime,
// return false
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
// Check if n is not divided
// by any number then it is
// prime and hence return true
return true;
}
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
void minDivisior(int n)
{
// Check if the number is prime
if (prime(n)) {
cout << 1 << " " << n - 1;
}
// Now, if it is not prime then
// find the least divisior
else {
for (int i = 2; i * i <= n; i++) {
// Check if divides n then
// it is a factor
if (n % i == 0) {
// Required output is
// a = n/i & b = n/i*(n-1)
cout << n / i << " "
<< n / i * (i - 1);
break;
}
}
}
}
// Driver Code
int main()
{
int N = 4;
// Function call
minDivisior(N);
return 0;
}