search
HomeJavaJavaBaseHow to calculate the least common multiple in java

How to calculate the least common multiple in java

Nov 27, 2019 pm 02:39 PM
javaleast common multiplecalculate

How to calculate the least common multiple in java

Problem description:

Given a positive integer N, ask three numbers randomly selected from 1~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 scale and convention:

1

Recommended online learning video tutorials: java course

Ideas:

First declare a few concepts:

The least common multiple of two non-zero adjacent natural numbers is their product; the least common multiple of two adjacent odd numbers is their product; the least common multiple of two adjacent even numbers (except 0) is their product half.

Now when it comes to the least common multiple of three numbers, there are two situations according to the parity of N:

1. When n is an odd number: n, n-1, n-2 Product

2. When n is an even number: n-1, n-2, n-3 are a set of maximum solutions. If the answer is greater than the current value, it can only be the product greater than these three numbers. Then only one of the numbers can be changed into n, and the three numbers must also be relatively prime. n, n-2, n-3 even and odd are obviously not mutually prime; n, n-1, n-3 are even and odd; n, n-1, n-2 even and odd are obviously not mutually prime.

Then the answer can only be n-1, n-2, n-3 or n, n-1, n-3. However, although n and n-3 are an odd number and an even number, they are not continuous and may not be relatively prime, such as the three numbers 5, 6, and 3.

n is an odd number: n, n-1, n-2

n is an even number: n is a multiple of 3 n-1, n-2, n-3

n is not a multiple of 3 n, n-1, n-3

Examples are as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        long N=input.nextLong();
        long temp=0;
        if(N<=2){
            System.out.println(N);
        }
        else if(N%2==0){
            temp=(N-1)*(N-2)*(N-3);
            if(N%3!=0){
                temp=Math.max(temp,N*(N-1)*(N-3));
            }
            System.out.println(temp);

        }

        else {
            System.out.println(N*(N-1)*(N-2));
        }
    }


}

Tips: Consider the case of N

Recommended java related article tutorials: Getting started with java development

The above is the detailed content of How to calculate the least common multiple in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?Mar 14, 2025 pm 05:06 PM

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

What is the Java Virtual Machine (JVM) and how does it work internally?What is the Java Virtual Machine (JVM) and how does it work internally?Mar 14, 2025 pm 05:05 PM

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

How do I use Java's Nashorn engine for scripting with JavaScript?How do I use Java's Nashorn engine for scripting with JavaScript?Mar 14, 2025 pm 05:00 PM

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

How do I use Java's try-with-resources statement for automatic resource management?How do I use Java's try-with-resources statement for automatic resource management?Mar 14, 2025 pm 04:59 PM

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

How do I use Java's enums to represent fixed sets of values?How do I use Java's enums to represent fixed sets of values?Mar 14, 2025 pm 04:57 PM

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.