2008年8月9日

o0 Project Euler - Problem 5 0o

。o Problem 5 o。


2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

※日本語の問題文はこちら


。o 解答言語 o。
JavaScript

。o 解答内容 o。
var MAX_NUM = 20;
var ans = 0;
var i = 1;

while (1) {
  var flag = 0;
  /*
   * 20,19は既に評価対象(ans)に代入する時点で
   * 保証されているので18から評価する
   */
  var check_num = MAX_NUM -2;                           
  ans = (MAX_NUM * (MAX_NUM -1)) * i;             
                                                  
  while (1) {                                     
    if (ans % check_num != 0) {                   
      flag = 1;                                   
      break;                                      
    }                                             
    check_num--;                                  
    if (check_num == 0) break;                    
  }                                               
  if (flag == 0) {     
    alert(ans);        
    break;             
  }                    
  i++;                 
}