-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeastCommonMultiple.java
More file actions
68 lines (44 loc) · 1.49 KB
/
LeastCommonMultiple.java
File metadata and controls
68 lines (44 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author SAMUEL ADOGA
*/
public class LeastCommonMultiple {
// static int start = 0;
public static void main(String[] arfs){
int[] testdenominators = {2,24,6,8,4};
int[] testdenominators2 = {2,19};
System.out.println("The LCM for the given array is : "+LCM(testdenominators,testdenominators.length,0));
//start = 0;
System.out.println("The LCM for the second given array is : "+LCM(testdenominators2,testdenominators2.length,0));
}
public static int LCM(int[] numbers, int size, int start){
if(size == 1)
return numbers[start];
int temp = lcmfind(numbers[start], numbers[start+1]);
numbers[start] = 0;
numbers[start+1] = temp;
return LCM(numbers, size-1,start+1);
}
public static int lcmfind(int a, int b){
int temp = a * b;
int max = max(a,b);
for(int i = max; i <= temp; i++){
if(i % a == 0 && i % b == 0){
return i;
}
}
return 0;
}
public static int max(int a, int b){
if(a > b){
return a;
}
return b;
}
}