Project Euler:Problem 70 Totient permutation
来源:互联网 发布:淘宝店铺一键安装 编辑:程序博客网 时间:2024/06/06 00:23
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of positive numbers less than or equal to 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.
The number 1 is considered to be relatively prime to every positive number, so φ(1)=1.
Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation of 79180.
Find the value of n, 1 < n < 107, for which φ(n) is a permutation of n and the ratio n/φ(n) produces a minimum.
#include <iostream>#include <map>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;}map<int, int> getnum(int n){map<int, int>mp;while (n){mp[n % 10]++;n /= 10;}return mp;}bool isPermutation(int a,int b){map<int, int>an = getnum(a);map<int, int>bn = getnum(b);if (an == bn)return true;elsereturn false;}int main(){double mine = 10000000.0;int num;for (int i = 2; i <= 10000000; i++){int b = getEuler(i);double tmp = i*1.0 / b;if (isPermutation(i,b)){//cout << i << " " << b << endl;if (tmp < mine){mine = tmp;num = i;}}}cout << num << " " << mine << endl;system("pause");return 0;}
0 0
- Project Euler:Problem 70 Totient permutation
- Project Euler:Problem 69 Totient maximum
- Project Euler Problem 51-70
- Euler Project Problem 6
- project euler problem 11
- Project Euler Problem 81
- Project Euler Problem 60
- Project Euler Problem 59
- Project Euler Problem 58
- Project Euler Problem 57
- Project Euler Problem 16
- Project Euler Problem 30
- Project Euler Problem 31
- Project Euler Problem 32
- Project Euler Problem 14
- Project Euler Problem 15
- Project Euler Problem 17
- Project Euler - Problem 18
- 深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)
- 使用pb的connection对象建立TCP连接
- Java 一行代码初始化ArrayList
- protel 99se :format %x invalid or incompatible with argument
- 深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP
- Project Euler:Problem 70 Totient permutation
- ACM--Doing Homework again(贪心算法)
- 深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
- 如何在shell下使用JavaScript和Mongodb交互?
- 深入理解JavaScript系列(4):立即调用的函数表达式
- C++ 全局对象构造和析构
- shell(1)
- 数字滤波器的时域理解
- GRE写作必备句型