求最小公倍数

本文详细介绍了如何通过编程实现找到1到20之间的最小公倍数,并提供了优化的代码示例。
部署运行你感兴趣的模型镜像

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
}
 

 

 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值