TIP1 - Totient in permutation (easy)

no tags 

In number theory, Euler's totient (or PHI function), is an arithmetic function that counts the number of positive integers less than or equal to a positive integer N that are relatively prime to this number N.

That is, if N is a positive integer, then PHI(N) is the number of integers K for which GCD(N, K) = 1 and 1 ≤ K ≤ N. We denote GCD the Greatest Common Divisor. For example, we have PHI(9)=6.

Interestingly, PHI(87109)=79180, and it can be seen that 87109 is a permutation of 79180.


The input begins with the number T of test cases in a single line.
In each of the next T lines there are an integer M.


For each given M, you have to print on a single line the value of N, for which 1 < N < M, PHI(N) is a permutation of N and the ratio N/PHI(N) produces a minimum. If there's several answers output the greatest, or if need, "No solution." without quotes.
Leading zeros are not allowed for integers greater than 0.




Explanations : For the first case, in the range ]1..22[, the lonely number n for witch phi(n) is in permutations(n) is 21, (we have phi(21)=12). So the answer is obviously 21.
For the second case, in the range ]1..222[, there's two numbers n for witch phi(n) is in permutations(n), we have phi(21)=12 and phi(63)=36. But as 63/36 is equal to 21/12, we're taking the greater : 63.
For the third case, in the range ]1..2222[, there's four numbers n for witch phi(n) is in permutations(n), phi(21)=12, phi(63)=36, phi(291)=192 and phi(502)=250. Within those solutions 291/192 is the minimum, we output 291.


1 < T < 10^5
1 < M < 10^7

Code size limit is 10kB ; less than 500B of python3 code can get AC under 2s.
After that you may try TIP2.
@Speed addicts : my C code ran in 0.02s, and my fastest python3.2 code ran in 1.21s, (0.90s in py2.7)

Edit 2017-02-11, after compiler updates. My old C code ends in 0.00s, my old Python code ends in 0.05s !!!

hide comments
Francky: 2017-02-12 15:36:17

Congratulations to Tjandra for being the lonely (for now) Python solver. Here, there's a little challenge.

DHEERAJ KUMAR: 2016-03-20 22:47:29

@Francky do i have WA for many inputs? submission id 16561400

.:frUstrAteD:.: 2015-10-03 21:50:00

@Francky Can you please check my code is getting WA
=(Francky)=> You have WA for only some small values. Good luck.

Last edit: 2015-10-12 21:02:36
Maroof: 2015-07-12 13:30:47

@FRANCKY do I have a lot of WAs in my last submission?

For small input, you have lot of WA, for bigger ones, it seems OK. I just gave a quick look. Have fun.

Last edit: 2015-07-12 13:52:02
[Lakshman]: 2015-07-02 05:36:33

AC now. A small bug and unlimited WA.

=(Francky)=> Well done, TIP2 is waiting for you now. TIP3 is still broken [:sad]. There's other bugs with higher priority...

-(Lakshman)-> @Francky can you please have a look at my TIP2 submission. Thanks.

Last edit: 2015-07-03 11:18:23
[Lakshman]: 2015-01-30 07:53:48

@Francky Can you please check my code getting WA, I have checked several test cases with my brute force code and getting correct answer.
--Francky--> Check again your brute force. You have some good answers.

Last edit: 2015-01-30 08:04:15
Harry Mathis: 2014-10-30 10:17:54

I get always a runtime error (NZEC) with my java code, but i have no idee what i'm doing wrong? Is it possible, that the input numbers are Strings? I expect integers.
--ans(Francky)--> Input is well formatted. I don't read Java, but I can give you a copy of the error :
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at Main.(Main.java:12)
Could not find the main class: Main. Program will exit.

@Francky: Your answer is very helpful. Thanx a lot.
--ans(Francky)--> You're welcome.

Last edit: 2014-10-30 22:29:23
Flago: 2014-05-19 14:39:22

"No solution" != "No solution."
I hate those stupid mistakes !

:p: 2013-06-03 15:46:05

some more test cases please, getting wrong answer..!! :(

abdelkarim: 2013-04-09 23:51:10


Last edit: 2013-04-09 23:55:45

Added by:Francky
Time limit:1.399s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Extension of Project Euler n°###