Project Euler:Problem 72 Counting fractions
2015-07-17 14:01
597 查看
Consider the fraction, n/d, where n and d are positive integers. If n<d and HCF(n,d)=1, it is called a reduced proper fraction.
If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, we get:
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
It can be seen that there are 21 elements in this set.
How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000?
统计2-1000000之间每个数的欧拉函数
If we list the set of reduced proper fractions for d ≤ 8 in ascending order of size, we get:
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
It can be seen that there are 21 elements in this set.
How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000?
统计2-1000000之间每个数的欧拉函数
#include <iostream> using namespace std; int getEuler(int n) { int m = n; int p = 2; int k = 0; while (p*p <= n) { k = 0; while (n%p == 0) { n /= p; k++; } if (k >= 1) m = m / p*(p - 1); p++; } if (n > 1) m = m / n*(n - 1); return m; } int main() { unsigned long long count = 0; for (int i = 2; i <= 1000000; i++) { count += getEuler(i); } cout << count << endl; system("pause"); return 0; }
相关文章推荐
- 【工作日志】【asp】【04】asp连接 sqlserver 2008的正确代码
- HDU 5092 Seam Carving (dp)
- Css样式之overflow
- 12-低延迟、全接口(HMDI、DVI、YPb Pr、RGB)H.264全高清编码器解码器
- 最近有个需求,就是把某个网址跳转到另外一个网址
- 提高iOS开发效率的方法和工具
- (4.3.1.2)【项目一】主体框架Fragment中启动Service与notification的使用
- 一个程序员的顿悟
- hdu 1008 Elevator
- 代码着色插件 for Windows Live Writer
- 管理后台比较好的U模板
- android intent隐式调用之一个应用程序启动另一个应用程序
- 【VS2010]如何删除【附加依赖项】中“继承的值”?
- java获取取前段页面的参数
- afnetworking2.0源码解析AFURLRequestSerialization
- discuz 上传头像失败解决方法
- yii2CSRF验证
- JAXB注解的使用详解
- 前台传后台参数,其中前台js 后台java处理乱码
- WebView的应用 持续积累