Maximum least common multiple

Problem Description

Given a positive integer N, choose three numbers from 1 to N, what is the maximum least common multiple of them.
input format

Enter a positive integer N.
Output Format
Output an integer representing the least common multiple you found.
Sample Input
9
Sample Output
504
Data Size and Convention

1 <= N <= 106。

Solve:

NO.1
I just saw the title, and I have no idea how to start. The initial idea is to find their least common multiple by traversing the combination of all 3 numbers, and then select the maximum value from it.
But this idea was immediately passed by myself, for no other reason, the amount of data was too large, and the method used to find the least common divisor would use recursion (the greatest common multiple of a, b = a * b * gcd(a , b), gcd is used to find the least common divisor), it will definitely run out of time. Without a good idea, I looked at solutions online.

The best solution is to first simplify the problem mathematically, and then supplement it with code.
The mathematical analysis idea is as follows:

Idea : If the three numbers n, n-1 and n-2 are relatively prime in pairs, then the result is the product of these three numbers.

According to the knowledge of number theory: any two adjacent natural numbers greater than 1 are coprime.

  • When n is odd, both n and n-2 are odd, and n-1 is even, so the common divisor of the three of them is definitely not 2. Because these three numbers are continuous, no number greater than 2 can be the common divisor of them or any two of them, and the result is the product of the three of them.
  • When n is an even number, n*(n-1) (n-2) will definitely not work, because both n and n-2 are even numbers, then only n-2 can be changed to n-3, that is, n (n-1 )*(n-3), if these three numbers are relatively prime in pairs, then it must be the result. But because the difference between n and n-3 is 3, when one of the numbers is divisible by 3, the other must also be possible; and when one of them is not, the other must not be; because n is an even number, n-3 It's an odd number, so 2 can't be their common factor. For numbers greater than 3, it is definitely impossible to become the common divisor of these three numbers or any two of them. So just judge 3 again:

  • If n is divisible by 3, then n*(n-1) (n-3) will definitely fail, because n and n-3 have a common divisor of 3, and the result must be small. Then you can only continue to judge the next one, that is, n (n-1) (n-4) and then n-4 is even, so you can't continue to the next n (n-1) (n-5)=n^3 -6 *n^2 + 5*n , and if this is possible, then its value must be less than (n-1) (n-2) (n-3) = n^3-6*n^2+11n-6( It is true for n>1), and (n-1) (n-2) (n-3) is a satisfactory one from the previous odd conclusion. Therefore, there is no need to judge until n-5, and the answer is (n-1) (n-2)*(n-3);

  • If n is not divisible by 3, then the result is n*(n-1)*(n-3), because neither n nor n-3 is divisible by 3, it does not matter whether n-1 is divisible by 3 or not, and for All other numbers are impossible, the above has been proved.

Reference link

The solution code is as follows:

import java.math.BigInteger;
import java.util.Scanner;

/** 
 * @author 作者 : Cactus
 * @version 创建时间:2018-3-19 下午07:45:10 
 */
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger N = new BigInteger(String.valueOf(n));
        sc.close();
        BigInteger N1 = new BigInteger(String.valueOf(n - 1));
        BigInteger N2 = new BigInteger(String.valueOf(n - 2));
        BigInteger N3 = new BigInteger(String.valueOf(n - 3));
        if(n <= 2){
            System.out.println(N);
        }else if(n % 2 != 0){
            System.out.println(N.multiply(N1.multiply(N2)));
        }else{
            if(n % 3 != 0){
                System.out.println(N.multiply(N1.multiply(N3)));
            }else{
                System.out.println(N1.multiply(N2.multiply(N3)));
            }
        }

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518656&siteId=291194637