Home > Back-end >  Least common multiple
Least common multiple

Time:09-29

Strives for the least common multiple
Excuse me, where's bosses are wrong

CodePudding user response:

The algorithm did not
Such as 9 and 7
J=63
Then j j/7??

CodePudding user response:

 
#include
using namespace std;
Int main () {
Int k=0;
Long long n, sum;//used here long long integer, due to the condition of the large data, int type may result in data overflow
Cin> n;
If (n<3) {//if the output is less than three, take less than three number, this number is the least common multiple of the input is either 1 or 2
Cout} else {//when is greater than the number of three, take the least common multiple of large Numbers, the basic thought is to take the number of the three biggest, then multiply to common multiple
If (n % 2==0) {//but there are two special cases
{if (n % 3==0)
Sum=(n - 1) * (n - 2) * (n - 3);//one of biggest can be divided exactly by 2, can be divided exactly by 3 again, largest began, in turn, minus one
}
Sum=n * (n - 1) * (n - 3);//one of the biggest potential energy be divided exactly by 2, then pick up to three, the smallest a need to get a
} else {
Sum=n * (n - 1) * (n - 2);//does not belong to the above two situations, in the last three multiplication, directly take least common multiple
}
Cout}
}

CodePudding user response:

Algorithm is absolutely wrong! Will use the most simple cycle? I collect the first, such as computer tell you at the meeting

CodePudding user response:

[code=c + +] # include & lt; Iostream>
using namespace std;
Int main ()
{
Int n, m;
Int ans.
Cin & gt;> N & gt;> m;
int i;
If (n & gt; M)
I=n;
The else
I=m;
While (true)
{
If (I % n==0 & amp; & I % m==0) {
Ans=I;
break;
}
i++;
}
Cout & lt; return 0;
} [/code]

CodePudding user response:

Your idea is, first to find the greatest common divisor, and then divided by the product of two Numbers, I also often use
For reference only
 
#include
using namespace std;
Int the GCD (int a, int b) {
int c;
//do not need to exchange who big small
While (a % b) {
C=a % b;
a=b;
B=c.
}
Return the c;
}

Int main () {
Int a, b, j, p;
Cin> a> b;
J=a * b;
P=GCD (a, b);
Coutreturn 0;
}
  • Related