最小公倍数(求满足1-M和 N+1 - M 两个序列的公倍数相同,给定N 求M)

我们都会求两个数的最小公倍数,现在的问题和多个数的最小公倍数有关。a和b都是正整数(a<b),用lcm(a,…,b)代表[a,b]区间内的所有数字的最小公倍数,输入一个正整数N,求最小的M(M>N),满足: lcm(1,…,M)=lcm(N+1,…,M)。

Input

输入只有一个数字N(2<=N<=100000)。

Output

输出满足条件的最小M。

Example 1

Input

3

Output

6

Example 2

Input

4

Output

8

Explanation

lcm(1,2,3,4,5,6) = lcm(4,5,6) = 60. lcm(1,2,3,4,5,6,7,8) = lcm(4,5,6,7,8) = 840.

解题思路:
对于素数,其有两条性质:

  1. 初等数学基本定理:任一大于1的自然数,要么本身是质数,要么可以分解为几个质数之积,且这种分解是唯一的。
  2. 在一般领域,对正整数n,如果用2到 之间的所有整数去除,均无法整除,则n为质数。

基于上述两点认识,可以粗略地得到下述思路:
只要M大于等于 1-N中最大素数的两倍,就可以满足上述条件(4除外)

#include<stdio.h>
#include<math.h>


int judgement(int m);

int main()
{
	int N,M;
	scanf("%d", &N);
	if (N != 4)
	{
		int i, judg;
		for (i = N; i >= 2; i--)
		{
			judg = judgement(i);//判断是否为素数
			if (judg == 1)
				break;
			if (judg == 0)
				continue;
		}

		M = 2 * i;
		printf("%d\n", M);
	}
	else
		printf("%d\n", 8);

	return 0;
}

int judgement(int m)
{
	int head, tail,temp, i;
	float judge;
	head =(int)sqrt(m);
	tail = 2;
	for (i = tail; i <= head; i++)
	{
		judge = 1.0* m / i;
		temp = (int)judge;

		if ((judge - temp) <= 0.000001)
		{
			return 0;
		}
	}
	return 1;
}

猜你喜欢

转载自blog.csdn.net/qq_36907160/article/details/82842453
今日推荐