Multiples of 3 and 5
2015-07-09 14:56
260 查看
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
using System; using System.Linq; public static class Kata { public static int Solution(int value) { // Magic Happens return value == 0 ? 0 : Enumerable.Range(1, value - 1).Where(x => (x % 3 == 0 || x % 5 == 0)).Aggregate(0, (sum, y) => sum + y); } }
其他人的解法:
如果只是简单的求和的话,可以直接使用Sum函数, Aggregate一般是在对每一项做了处理之后才使用的
using System.Linq; public static class Kata { public static int Solution(int value) { if (value == 0) return 0; return Enumerable .Range(1, value - 1) .Where(x => x % 3 == 0 || x % 5 == 0) .Sum(); } }
相关文章推荐
- java.lang.IllegalStateException: No activity
- php函数快速简单截取中英文
- 浅谈php的缓存机制之redis
- win10下提示无线网卡RTL8192DE无法使用怎么办?解决方法
- javascript的闭包
- #笔记#圣思园 JavaWeb 第29讲——Servlet
- Ext JS isField为空或不是对象问题的解决
- OC调用Swift
- 如何查找一个宏定义的位置
- DNS解析过程详解
- 启发式算法、寻路算法A*算法
- Linux下SVN服务器同时支持Apache的http和svnserve独立服务器两种模式且使用相同的访问权限账号
- Javascript面向对象编程(二):构造函数的继承
- 什么是网络资源管理?
- Docker dockerfile创建Eclipse镜像初试
- 游标
- AndroidStudio生成aar包
- Javascript 面向对象编程(一):封装
- CSS3 经典教程系列:CSS3 线性渐变(linear-gradient)
- mac 下配置ant和打包