Java language Programming (Basic article)
Programming comprehensive question 6.29 beg LCM
Write a program that prompts the user to enter two integers and ask for their LCM (LCM).
The two-digit LCM refers to the smallest number in multiples of these two books.
For example
The LCM of 8 and 12 are 24,
The LCM of 15 and 25 are 75,
The LCM of 120 and 150 is 600.
There are many ways to find LCM, and in this exercise you use the following methods:
To find the LCM of two positive integers, first create a list of prime factors for each number, the first column contains all prime factors, and the second is the number of corresponding prime factors in the number.
For example, the prime factor of 120 is 2,2,2,3,5, so the 120 prime factor table looks like this:
120 prime Factor occurrence
2 3 3 1 5 1 150
The prime factor occurs the number of
times 2
1
3 1 The LCM of the 5 2
Two numbers is made up of the two most frequently occurring factors, so the LCM of 120 and 150 is 2*2*2*3*5*5=600. Of these, 2 appeared 3 times in 120, 3 in 120 and 1 in 5.
hint: can use two-dimensional array to represent prime factor table, write getprimefactors (int numer) method, make it return a two-dimensional array for prime number factor table.
Here's how I'm programmed to change the course of thinking:
**leastcommonmultiple1.java**
Import Javax.swing.JOptionPane;
public class LeastCommonMultiple1 {static int max=10; public static void Main (string[] args) {//get-integers from a dialog String NUM1=JOPTIONPANE.SHOWINP
Utdialog (NULL, "input", "Input Dialog", joptionpane.question_message);
creat a array then initialize it;
Int[][] Primefactorlist1=new Int[2][max];
for (int j=0;j<primefactorlist1[0].length;j++) {primefactorlist1[0][j]=1;
primefactorlist1[1][j]=0;
} leastcommonmultiple (Integer.parseint (NUM1), primeFactorList1);
Output the list to check;
System.out.println (Integer.parseint (NUM1) + "' s table of factors are");
for (int i=0;i<primefactorlist1.length;i++) {for (int j=0;j<primefactorlist1[0].length;j++) {
System.out.print (primefactorlist1[i][j]+ "");
} System.out.println (""); } String NUM2=JOPTIONPANE.SHOWINPUtdialog (NULL, "input", "Input Dialog", joptionpane.question_message);
creat a array then initialize it;
Int[][] Primefactorlist2=new Int[2][max];
for (int j=0;j<primefactorlist1[0].length;j++) {primefactorlist2[0][j]=1;
primefactorlist2[1][j]=0;
} leastcommonmultiple (Integer.parseint (num2), primeFactorList2);
System.out.println ("\ n" +integer.parseint (num2) + "' s table of factors are");
for (int i=0;i<primefactorlist2.length;i++) {for (int j=0;j<primefactorlist2[0].length;j++) {
System.out.print (primefactorlist2[i][j]+ "");
} System.out.println (""); }//calculate The least common multiple by the two list;
This is a bug which need to fix!
int leastcommonmultiple=1;
for (int j=0;j<primefactorlist1[0].length;j++) {if (Primefactorlist1[0][j]==primefactorlist2[0][j]) { if (primefactorlist1[1][j]<PRIMEFACTORLIST2[1][J]) Leastcommonmultiple*=math.pow (double) primefactorlist1[0][j], (double) PRI
MEFACTORLIST2[1][J]);
else Leastcommonmultiple*=math.pow (double) primefactorlist1[0][j], (double) primefactorlist1[1][j]); } else{Leastcommonmultiple*=math.pow (double) primefactorlist1[0][j], (double) prime
FACTORLIST1[1][J]);
Leastcommonmultiple*=math.pow (double) primefactorlist2[0][j], (double) primefactorlist2[1][j]);
}//output the LCM on both of console and dialog; String Result=string.format ("\ n" +leastcommonmultiple+ "is the least common multiple of" +integer.parseint (NUM1) + "and" +
Integer.parseint (num2));
SYSTEM.OUT.PRINTLN (result);
Joptionpane.showmessagedialog (null, result); }//it is able just to handle the integer whose # of factors is less than (s) and that ' why I set a const ARGU ment "MAX" whiCH is-default;
public static void Leastcommonmultiple (int number,int[][] primefactorlist) {int integer=number;
if (integer>=2) {for (int factor=2;factor<=integer;factor++) {if (integer%factor==0) {
Load into the factor list;
Updateprimefactorlist (factor,primefactorlist);
Integer=integer/factor;
System.out.print (factor+ "");
Leastcommonmultiple (integer,primefactorlist);
Break
}//the following "Else" isn't essential,it may not miss somewhere else
Continue }//Else//Joptionpane.showmessagedialog (NULL, "the number you are not valid!", "Error", Jopti
Onpane.information_message);
}//update the prime factors list;
public static void Updateprimefactorlist (int num,int[][] primefactorlist) { for (int j=0;j<primefactorlist[0].length;j++) {if (primefactorlist[0][j]==num) {primefact
orlist[1][j]++;
Break
else if (primefactorlist[0][j]==1) {primefactorlist[0][j]=num;
Primefactorlist[1][j]=1;
Break
else continue;
}
}
}
Result of test:
! [lcm_lcm1_ dialog box output _120 and 150 LCM] (https://img-blog.csdn.net/20151004234556565)
! [Write a picture description here] (https://img-blog.csdn.net/20151005162507916)
From the output here you can see that I am using the form of two-dimensional array Primefactorlist:int[2][max] to store the integer prime Factor table, Primefactorlist[i][0] store prime factors, the corresponding primefactorlist[i][ 1] Stores the number of occurrences of the corresponding prime factor and starts with a 1 initialization primefactorlist[i][0], with 0 initialization primefactorlist[i][1].
**bugs**-* * Obviously, as shown in the output above, there are traces of our programming in the prime factor table, which is unnecessary and unnecessary for users. Therefore, you can consider only the use of the program in the "Prime factor Table" in the mathematical meaning of the content, that is, to remove the prime factor of 1 and the occurrence of the number of 0 redundant data. From this point of view, there is a later optimization of the following procedures. Please see program Leastcommonmultiple2.java for details. **
* * In addition, there are some (logical) errors (bugs) in the Leastcommonmultiple1.java program:* *-* * * as shown in the following illustration, given the fact that the LCM (LCM) is computed-line by row by peer comparison through the two-number SU-su factor table, which means Primefactorlist1[i] and primefactorlist2[i] compared, if primefactorlist1[i][0]=primefactorlist2[i][0], there is no problem. This requires that both integers have an equal number of elements, and that each of them must be the same in order to get the correct answer (such as the input with the output of 120 and 150 above): This is obviously wrong. Not only are the numbers not equal, they cannot be processed, even if they have some of the same element but are not able to be processed because they do not correspond to equal two numbers (as shown in the following two graphs). ** ! [(distinct) examples of unequal number of prime factors] (https://img-blog.csdn.net/20151005031219555)
! [Example of prime factor partial equivalence] (https://img-blog.csdn.net/20151005031305202)
- ...... ———-
Leastcommonmultiple2.java
Package prime;
Import Javax.swing.JOptionPane;
Import sortalgorithms.*;
public class LeastCommonMultiple2 {static int max=10;
It is able just to handle the integer whose factor is less than 10;
public static void Setprimefactorlist (int number,int[][] PFL) {int integer=number; if (integer>=2) {for (int factor=2;integer!=1&&factor<number/2.0;factor++) {if int
eger%factor==0) {//load into the factor list;
Updateprimefactorlist (FACTOR,PFL);
Integer=integer/factor;
Factor=1;
}}//update the prime factors list; public static void Updateprimefactorlist (int num,int[][] primefactorlist) {for (int j=0;j<primefactorlist[0].len
gth;j++) {if (primefactorlist[0][j]==num) {primefactorlist[1][j]++;
Break else if (PRImefactorlist[0][j]==1) {primefactorlist[0][j]=num;
Primefactorlist[1][j]=1;
Break
else continue;
}//calculate The least common multiple by the two list; public static int CALCULATELCM (int[][] primefactorlist1,int[][] primefactorlist2,int[][] list) {int Leastcommonmul
tiple=1;
int l=0;
Scan the former prime factor list;
for (int i=0;i<primefactorlist1[0].length;i++) {list[0][l]=primefactorlist1[0][i];
LIST[1][L]=PRIMEFACTORLIST1[1][L];
int j=0;
for (; j<primefactorlist2[0].length;j++) {if (Primefactorlist1[0][i]==primefactorlist2[0][j]) {
if (Primefactorlist1[1][i]<primefactorlist2[1][j]) list[1][l]=primefactorlist2[1][j];
Break
}} l++; }//scan the LATter prime factor list;
for (int j=0;j<primefactorlist2[0].length;j++) {int i=0;
for (; i<primefactorlist1[0].length;i++) {if (Primefactorlist2[0][j]==primefactorlist1[0][i])
Break
} if (i==primefactorlist1[0].length) {list[0][l]=primefactorlist2[0][j];
LIST[1][L]=PRIMEFACTORLIST2[1][J];
l++;
for (int i=0;i<l;i++) {Leastcommonmultiple*=math.pow (List[0][i], list[1][i]);
return leastcommonmultiple;
public static void Outputprimefactorlist (int[][] l) {System.out.print ("factor:");
for (int j=0;j<l[0].length;j++) {if (l[0][j]!=1) System.out.print (l[0][j]+ "");
} System.out.print ("\npower:");
for (int i=0;i<l[0].length;i++) {if (l[1][i]!=0) System.out.print (l[1][i]+ "");
} System.out.println ("");} Public static void Main (string[] args) {int number1=1,number2=1;
int leastcommonmultiple=1;
Int[][] Primefactorlist1=new Int[2][max];
Int[][] Primefactorlist2=new Int[2][max];
Int[][] List=new int[2][max*2];
Initializing for (int i=0;i<primefactorlist1[0].length;i++) {primefactorlist1[0][i]=1;
primefactorlist1[1][i]=0;
Primefactorlist2[0][i]=1;
primefactorlist2[1][i]=0;
for (int i=0;i<list[0].length;i++) {list[0][i]=1;
list[1][i]=0;
} String Num1=joptionpane.showinputdialog (null, "input", "Input Dialog", joptionpane.question_message);
Number1=integer.parseint (NUM1);
Setprimefactorlist (NUMBER1,PRIMEFACTORLIST1);
String Num2=joptionpane.showinputdialog (NULL, "input", "Input Dialog", joptionpane.question_message);
Number2=integer.parseint (NUM2); Setprimefactorlist (Number2,primefactorLIST2);
System.out.println (number1+ "' s table of factors are");
Outputprimefactorlist (PRIMEFACTORLIST1);
System.out.println ("\ n" +number2+ "' s table of factors are");
Outputprimefactorlist (PRIMEFACTORLIST2);
Output the LCM;
LEASTCOMMONMULTIPLE=CALCULATELCM (primefactorlist1,primefactorlist2,list);
String Result=string.format ("\ n" +leastcommonmultiple+ "is the least common multiple of" +number1+ "and" +number2);
SYSTEM.OUT.PRINTLN (result); Invoke a sort function to make the LCM's table of factor more precise/**there was a bug that factor and thei R powers are not relevant,because the sort function cannot completely exchange two factor and their power in the form of LIS
T[2][max],it should be list[max][2] */Sortalgorithminincreasingorder.selectionsort (list[0));
Outputprimefactorlist (list);
Joptionpane.showmessagedialog (null, result);
}
}
**sortalgorithminincreasingorder.java**
Package sortalgorithms;
public class Sortalgorithminincreasingorder {public
static void Selectionsort (int[] array) {
int temp=array[0] ;
for (int i=0;i<array.length-1;i++) {
int k=i;
Temp=array[i];
for (int j=i;j<array.length;j++) {
if (Temp>array[j]) {
temp=array[j];
K=j
}
}
Array[k]=array[i];
Array[i]=temp
}}
}
Result of test:
Optimization & Bugs:
LCM_LCM2 improves the LCM1 by adding CALCULATELCM () More encapsulation, reducing the burden of testing the main function, and increasing the portability of the Code
lcm_lcm2 for LCM1-output LCM's prime factor table and provide the correct LCM's prime factor table for use in +lcm2 bugs
3. The problem with lcm_lcm2_bug1_setprimefactorlist--the number of primes that cannot be handled correctly--is due to LCM_LCM2 's failure to optimize LCM1.
The following figure shows the output of the Leastcommonmultiple1.java. It is indeed correct.
! [Write a picture description here] (https://img-blog.csdn.net/20151005041527206)
Here's the output of Leastcommonmultiple2.java-he's obviously not right.
! [Lcm_lcm2_bug1_setprimefactorlist has a problem] (https://img-blog.csdn.net/20151005034007361)
The place where the error occurred is here
! [Write a picture description here] (https://img-blog.csdn.net/20151005040216901)
4. At the same time, nor can I enter 1 (1 is neither prime nor composite)--although the data entered is mandatory, it is possible for the user to enter any data, the robustness of the algorithm (software testing) requires us to consider every possible situation as far as possible-from this perspective, You can also expand to enter the number of negative numbers of cases ...! [Write a picture description here] (https://img-blog.csdn.net/20151005034432528)
> Prime numbers (primes) are defined above the set of natural numbers greater than 1. >! [Write a picture description here] (https://img-blog.csdn.net/20151005034407230)
5. LCM_LCM2 's bug3_ to the LCM of the prime factor table of the choice of sorting exchange is not complete. Here we exchange the elements in the array list[0] (the mass factor), and the elements in the corresponding list[1] (the number of occurrences/levels of the mass factor) are not exchanged, as shown in the following figure. Obviously, the order of 7 in the LCM 420 of 105 and 60 should be 2. And the 2 order should be 1, just the opposite. Accidental is necessarily the algorithm bug. [Write a picture description here] (https://img-blog.csdn.net/20151005035303926) ———-
**leastcommonmultiple4.java**
Package prime;
Import Javax.swing.JOptionPane;
Import Sortalgorithms.sortalgorithminincreasingorder;
public class LeastCommonMultiple4 {static int max=10;
It is able just to handle the integer whose factor is less than 10;
public static void Setprimefactorlist (int number,int[][] PFL) {int integer=number;
if (integer>=2) {for (int factor=2;integer!=1&&factor<=integer;factor++) {
if (integer%factor==0) {//load into the factor list;
Updateprimefactorlist (FACTOR,PFL);
Integer=integer/factor;
Factor=1;
}} else if (integer==1) {pfl[0][1]++;
}//update the prime factors list; public static void Updateprimefactorlist (int num,int[][] primefactorlist) {for (int j=0;j<primefactorlist.length ; j+ +) {if (primefactorlist[j][0]==num) {primefactorlist[j][1]++;
Break
else if (primefactorlist[j][0]==1) {primefactorlist[j][0]=num;
Primefactorlist[j][1]=1;
Break
else continue;
}//calculate The least common multiple by the two list; public static int CALCULATELCM (int[][] primefactorlist1,int[][] primefactorlist2,int[][] list) {int Leastcommonmul
tiple=1;
int l=0;
Scan the former prime factor list;
for (int i=0;i<primefactorlist1.length;i++) {list[l][0]=primefactorlist1[i][0];
LIST[L][1]=PRIMEFACTORLIST1[L][1]; int j=