- 论坛徽章:
- 6
|
本帖最后由 dorodaloo 于 2017-03-28 21:20 编辑
- package main
- import "math"
- func main() {
- p005(10) // 2520
- p005(20) // 232792560
- }
- func p005(n int) {
- var rec = make([]int, n+1)
- for i := range rec { rec[i] = 0 }
- for i := 2; i <= n; i++ {
- j := i
- m := 2
- var num = make(map[int]int)
- max := int(math.Sqrt(float64(j)))
- for m <= max {
- if j%m != 0 { m++; continue }
- num[m]++; j /= m
- }
-
- num[j]++
-
- for k := range num {
- if rec[k] < num[k] { rec[k] = num[k] }
- }
- }
-
- var res = 1
- for i := 2; i <= n; i++ {
- if rec[i] == 0 { continue }
- res *= int(math.Pow(float64(i), float64(rec[i])))
- }
-
- println(res)
- }
- // 题目:
- // 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 positive number that is evenly divisible by
- // all of the numbers from 1 to 20?
复制代码 |
|