Smallest Multiple by GoLang
来源:互联网 发布:淘宝购买steam游戏 编辑:程序博客网 时间:2024/06/04 23:28
My answer gives a general algorithm to deal with this problem via golang, and it produces the smallest positive number that is evenly divisible by all of the numbers from 1 to N. However, the overflow problem should be considered well by yourself in case of big int. Any question or suggestion, feel free to contact me.
func SmallestMultiple(n int) int { primeLst := []int{} factorCntMap := make(map[int]int) for i := 2; i <= n; i++ { tag := true tmp := i for _, j := range primeLst { if tmp%j == 0 { tag = false cnt := 0 for tmp%j == 0 { tmp /= j cnt++ } if cnt > factorCntMap[j] { factorCntMap[j] = cnt } } else if tmp == 1 { break } } if tag == true { primeLst = append(primeLst, i) factorCntMap[i] = 1 } } res := 1 for k, v := range factorCntMap { for i := 0; i < v; i++ { res *= k } } return res}
阅读全文
0 0
- Smallest Multiple by GoLang
- Smallest multiple
- Problem 5:Smallest multiple
- PE 5 Smallest multiple
- Smallest Common Multiple
- Smallest Common Multiple
- Problem 5 Smallest multiple
- Smallest Common Multiple
- (Problem 5)Smallest multiple
- 5 Smallest multiple - Project Eulder
- Project Euler 005 Smallest multiple
- 【Project Euler】【Problem 5】Smallest multiple
- project euler problem 5:Smallest multiple
- Smallest multiple 求1-20的最小公倍数
- 欧拉计划(5) Smallest multiple
- 欧拉项目第五题 Smallest multiple
- Problem 5 Smallest multiple(LCM)
- Project Euler Problem 5: Smallest multiple
- 线程和协程的异同!
- C语言内存
- Python定时器
- HDU 1956 Sightseeing tour(混合图欧拉回路)
- 闪回1-恢复区设置
- Smallest Multiple by GoLang
- 属性
- Nodejs连接mysql的增、删、改、查操作
- 搭建es6+react开发环境---webpack
- 阿里巴巴下一代数据集成技术
- 倒计时(时、分、秒)面板
- 基于大数据的电影网站项目开发之Hadoop2.6.0伪分布式设置(二)
- 单目激光三维重建的标定方法
- Mybatis基于MySql分页插件PageHelper的使用