Home>

Factorization

/*
 Factorization is a very basic mathematical operation.
Wide range of applications.
The following program factorizes the integer n (n>1).
 For example, n=60, then output:2 2 3 5. Please fill in the missing parts.
 * /
public class factorization {
 public static void f (int n) {
  for (int i=2;i<n/2;i ++) {
   while (n%i == 0) {//fill in the blanks
    system.out.printf ("%d", i);
    n=n/i;
   }
  }
  if (n>1)
   system.out.printf ("%d \ n", n);
 }
 public static void main (string [] args) {
  f (60);
 }
}

operation result:

2 2 3 5

Least common multiple

/*
Finding the least common multiple of two numbers is a very common operation.
For example, the least common multiple of 3 and 5 is 15. The least common multiple of 6 and 8 is 24.
The following code finds the least common multiple of a given two positive integers.
Please fill in the missing code.
Make the program run as efficiently as possible.
Fill in the blank answer (only the answer in the blank,Excluding the question surface) can be stored in the answer.txt corresponding to the question number in the candidate folder.
 * /
public class least common multiple {
 public static int f (int a, int b)
 {
  int i;
  for (i=a ;;i +=a) {//fill in the blanks
   if (i%b == 0) return i;
  }
 }
 public static void main (string [] args) {
  system.out.println (f (6,8));
 }
}

operation result:

twenty four
  • Previous Basic Event Learning Tutorial in MySQL
  • Next The usage of tostring () and valueof () in JavaScript and the difference between the two