码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler:Problem 69 Totient maximum

时间:2015-07-17 10:05:11      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:project euler   c++   欧拉函数   

Euler‘s Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6.

n Relatively Prime φ(n) n/φ(n)
2 1 1 2
3 1,2 2 1.5
4 1,3 2 2
5 1,2,3,4 4 1.25
6 1,5 2 3
7 1,2,3,4,5,6 6 1.1666...
8 1,3,5,7 4 2
9 1,2,4,5,7,8 6 1.5
10 1,3,7,9 4 2.5

It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10.

Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum.


直接求欧拉函数,然后除一下,比较一下就出结果了。

#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()
{
	double maxe = 0.0, num;
	for (int i = 2; i <= 1000000; i++)
	{
		int pp = getEuler(i);
		double tmp = i*1.0 / pp;
		if (tmp > maxe)
		{
			maxe = tmp;
			num = i;
		}
	}
	cout << num << " " << maxe << endl;
	system("pause");
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Project Euler:Problem 69 Totient maximum

标签:project euler   c++   欧拉函数   

原文地址:http://blog.csdn.net/youb11/article/details/46918025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!