//这里的n是指你当前最大的数是多少,一般设为maxn
vector<int> g_primeVector = { 2, 3, 5, 7, 11 }; // 素数表
bool UpdatePrimeVec() // 更新素数表
{
if (g_primeVector.back() >= maxn){
return false;
}
for (int num = g_primeVector.back() + 1; num <= maxn; num++)
{
for (auto prime : g_primeVector)
{
if (num % prime == 0) {
break;
}
if (prime > sqrt(num)) {
g_primeVector.push_back(num);
break;
}
}
}
return true;
}
vector<int> Mutiple(vector<int> num1, int num2)//大数乘法
{
vector<int> res;
int c = 0;
for (auto i = num1.begin(); i != num1.end(); i++) {
int tmp = *i * num2 + c;
c = tmp / 10;
tmp = tmp % 10;
res.push_back(tmp);
}
while (c) {
res.push_back(c % 10);
c /= 10;
}
return res;
}
//这里的arr[],指的是你需要求的n个数,
vector<int> GetLeastComMult(int arr[], int n) //获取最小公倍数
{
vector<int> leastComMult(1, 1);
UpdatePrimeVec(); //这里对于素数表直接取maxn
vector<int> multTimes(g_primeVector.size(), 0);
for (int i = 1; i <= n; i++)
{
int num = arr[i];
for (int j = 0; j < g_primeVector.size() && num > 0; j++)
{
int time = 0;
while ((num % g_primeVector[j] == 0) && (num > 0))
{
time++;
num /= g_primeVector[j];
}
if (multTimes[j] < time) {
multTimes[j] = time;
}
}
}
for (int n = 0; n < multTimes.size(); n++)
{
while (multTimes[n]--)
{
leastComMult = Mutiple(leastComMult, g_primeVector[n]);
}
}
return leastComMult;
}
//对你的得到的大数进行输出
void CoutVec(vector<int> numPlus)// 输出大数
{
for (int i = numPlus.size() - 1; i >= 0; i--)
{
printf("%d", numPlus[i]);
}printf("\n");
}
07-26
160
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-29
1972
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)