Project Euler:Problem 69 Totient maximum

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.

nRelatively Primeφ(n)n/φ(n)
2112
31,221.5
41,322
51,2,3,441.25
61,523
71,2,3,4,5,661.1666...
81,3,5,742
91,2,4,5,7,861.5
101,3,7,942.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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值