Sample source code for finding the least common multiple of multiple values in Java

Source code

Main.java


package src;
import java.util.ArrayList;

public class Main {
	public static void main(String[] args) {
		int valueLCM = 0;
		ArrayList<Integer> list = new ArrayList<>();
		list.add(4);
		list.add(6);
		list.add(9);

		for(int index=0; index<list.size()-1; index++) {
			if( index==0 ) {
				valueLCM = calcLCM( list.get(index), list.get(index+1) );
			} else {
				valueLCM = calcLCM( valueLCM,        list.get(index+1) );
			}
		}
		System.out.println(valueLCM);
	}

	private static int calcLCM(int val1, int val2) {
		int maxValue = Math.max(val1, val2);
		int minValue = Math.min(val1, val2);
		long val3    = maxValue * minValue;

		if(minValue==0) return maxValue;

		int temp;
		while( (temp=maxValue%minValue)!=0 ) {
			maxValue=minValue;
			minValue=temp;
		}

		return (int)(val3/minValue);
	}

}

Supplement

After finding the greatest common divisor by Euclidean algorithm, Find the least common multiple.

Recommended Posts

Sample source code for finding the least common multiple of multiple values in Java
Sample code to get the values of major SQL types in Java + MySQL 8.0
Sample code to get the values of major SQL types in Java + Oracle Database 12c
Technology for reading Java source code in Eclipse
Find the greatest common divisor and least common multiple of any number of integers in Ruby
Find the greatest common divisor and least common multiple with JAVA
Guess the character code in Java
Pre-written source code for the activity
Basic structure of Java source code
Use an example domain for the package name in the sample code
Sample code to call the Yahoo! Local Search API in Java
[Reference example] Copyright is described in the comment of the source code.
Sample code that uses the Mustache template engine JMustache in Java
Get the result of POST in Java
Sample code to get key JDBC type values in Java + H2 Database
Sample code collection for Azure Java development
Sample program that returns the hash value of a file in Java
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
The story of writing Java in Emacs
Uncertainty of common Jar for multiple versions
Source code for finding an orthonormal basis with the Gram-Schmidt orthogonalization method
Code that deletes all files of the specified prefix in AWS S3 (Java)
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
ChatWork4j for using the ChatWork API in Java
The story of learning Java in the first programming
[Java] Explanation of Strategy pattern (with sample code)
Feel the passage of time even in Java
Import files of the same hierarchy in Java
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
Sample code to assign a value in a property file to a field of the expected type
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
Sample code to convert List to List <String> in Java Stream
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Quickly understand the basics of Java 8 Lambda
Include the source code generated by Doma in the source JAR
Source used to get the redirect source URL in Java
A note for Initializing Fields in the Java tutorial
[For beginners] Minimum sample to display RecyclerView in Java
[Java] Get the file in the jar regardless of the environment
[Java] When writing the source ... A memorandum of understanding ①
Sample code for log output by Java + SLF4J + Logback
Change the storage quality of JPEG images in Java
Differences in code when using the length system in Java
How to execute WebCamCapture sample of NyARToolkit for Java
Summarize the additional elements of the Optional class in Java 9
Define data source in code in spring-boot instead of property
A summary of what Java programmers find when reading Kotlin source for the first time
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
[Improvement of Java development efficiency] DCEVM and HotSwapAgent reflect changes in source code without restarting
Was done in the base year of the Java calendar week
[For beginners] Explanation of classes, instances, and statics in Java
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
A quick explanation of the five types of static in Java