Java 코드를 사용하여 인수 분해 및 최소 공배수의 예

1125 단어
인수 분해

/* 
               ,    。        n(n>1)      。 
   ,n=60,    :2 2 3 5。        。 
 */ 
public class      { 
 public static void f(int n) { 
  for (int i = 2; i < n / 2; i++) { 
   while(n%i==0){ //    
    System.out.printf("%d ", i); 
    n = n / i; 
   } 
  } 
  if (n > 1) 
   System.out.printf("%d
", n); } public static void main(String[] args) { f(60); } }

실행 결과:

2 2 3 5 

최소 공배수

/* 
                  。  ,3 5      15。6 8       24。 
                      。        ,          。 
      (       ,     )             “  .txt”   。 
 */ 
public class       { 
 public static int f(int a, int b) 
 { 
  int i; 
  for(i=a;;i+=a){ //    
   if(i%b==0) return i; 
  } 
 } 
 public static void main(String[] args){ 
  System.out.println(f(6,8)); 
 } 
} 

실행 결과:

   24 
 

좋은 웹페이지 즐겨찾기