求最小公倍数

What is the smallest number divisible by each of the numbers 1 to 20?

[my resolutio]

 find the prime number first.beside the prime number ,find every number's smallest factor,then delete the equals factor.finally,multiple all the factors and prime number.

 

sample code as blow:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考代码如下:

; Returns 1 if the number is a whole number (integer).
alias is_int { if ($int($1) == $1) { return 1 } | else return 0 }
; 5
[code=python]
alias math_5_f {
  ; Gets a number, and returns it's deviders.
  ; 10 will return 2 5, 20 will return 2 2 5, etc.
  if (!$1) || ($1 !isnum) || ($1 <= 0) { halt }
  var %i = 2, %num = $1, %t, %ticks = $ticks
  ; If number is 1, return 1.
  if ($1 == 1) { %t = 1 | goto end }
  ; Loop until number doesn't devide by 2 anymore.
  while ($is_int($calc(%num / %i))) { var %t = %t %i, %num = $calc(%num / %i) }
  inc %i
  ; Loop from 3 and in steps of 2
  while (%i <= %num) {
    ; Loop in each loop until number doesn't
    ; devide anymore by %i.
    while ($is_int($calc(%num / %i))) { var %t = %t %i, %num = $calc(%num / %i) }
    inc %i 2
  }
  :end
  if ($isid) { return %t }
  else { echo -a %t Time: $calc($ticks - %ticks) }
}
alias math_5_nums {
  ; Main function, gets a number and returns
  ; the smallest number that can be devided
  ; by each of the numbers from 1 until it.
  if (!$1) { halt }
  if ($hget(math_5)) { hfree -s math_5 }
  ; Creates a hash table
  hmake -s math_5 10
  var %i = 1, %temp, %ticks = $ticks
  ; looping through numbers until $1 (given number).
  while (%i <= $1) {
    var %temp = $math_5_f(%i), %n
    while ($numtok(%temp,32)) {
      %n = $gettok(%temp,1,32)
      if ($findtok(%temp,%n,0,32) > $hget(math_5,%n)) || (!$hget(math_5,%n)) { hadd math_5 %n $findtok(%temp,%n,0,32) }
      while ($istok(%temp,%n,32)) { %temp = $remtok(%temp,%n,32) }
    }
    inc %i
  }
  var %i = 1, %temp = 1
  while (%i <= $hget(math_5,0).item) { %temp = $calc(%temp * $hget(math_5,%i).item ^ $hget(math_5,$hget(math_5,%i).item)) | inc %i }
  echo -a Answer: %temp Time: $calc($ticks - %ticks) ms
  hfree -s math_5
}
 

 

 

你可能感兴趣的:(projecteuler)