免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 40092 | 回复: 1
打印 上一主题 下一主题

[算法] Project Euler - 005 [复制链接]

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-03-26 23:20 |只看该作者 |倒序浏览
本帖最后由 dorodaloo 于 2017-03-26 23:26 编辑

Project Euler - 005

题目:

  • 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?

  • 2520是最小的能被1-10中每个数字整除的正整数。
  • 最小的能被1-20中每个数整除的正整数是多少?

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
2 [报告]
发表于 2017-03-28 21:18 |只看该作者
本帖最后由 dorodaloo 于 2017-03-28 21:20 编辑
  1. package main

  2. import "math"

  3. func main() {
  4.         p005(10)    // 2520
  5.         p005(20)    // 232792560
  6. }

  7. func p005(n int) {
  8.         var rec = make([]int, n+1)
  9.         for i := range rec { rec[i] = 0 }
  10.         for i := 2; i <= n; i++ {
  11.                 j := i
  12.                 m := 2
  13.                 var num = make(map[int]int)
  14.                 max := int(math.Sqrt(float64(j)))

  15.                 for m <= max {
  16.                         if j%m != 0 { m++; continue }
  17.                         num[m]++; j /= m
  18.                 }
  19.         
  20.                 num[j]++
  21.         
  22.                 for k := range num {
  23.                         if rec[k] < num[k] { rec[k] = num[k] }
  24.                 }
  25.         }
  26.    
  27.         var res = 1

  28.         for i := 2; i <= n; i++ {
  29.                 if rec[i] == 0 { continue }
  30.                 res *= int(math.Pow(float64(i), float64(rec[i])))
  31.         }
  32.    
  33.         println(res)
  34. }

  35. // 题目:
  36. // 2520 is the smallest number
  37. // that can be divided by each of the numbers from 1 to 10
  38. // without any remainder.
  39. // What is the smallest positive number that is evenly divisible by
  40. // all of the numbers from 1 to 20?
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP