The inverse function of "Prime"


Consider p such that PrimeQ[p] == True. How do I compute n such that Prime[n] == p?

In other words, what is the inverse function of "Prime"?


As a concrete example, consider p to be the first prime that factorizes rsa-768,

p = rsa768a = 33478071698956898786044169848212690817704794983713768568912431388982883793878002287614711652531743087737814467999489

PrimeQ[rsa768a] is True; I want to know its n.

Trying Jason's suggestion, InverseFunction[Prime][rsa768a], does not return the expected result (for a small prime it does). Trying LLlAMnYP suggestion, PrimePi[rsa768a] returns

PrimePi::largp: Argument ... in PrimePi[...] is too large for this implementation. >>

Jorge Leitao

Posted 2016-03-09T09:20:01.510

Reputation: 783

Question was closed 2016-03-09T13:56:40.327

1InverseFunction[Prime] – Jason B. – 2016-03-09T09:21:40.933

4@JasonB Cheeky :-P and cruel. PrimePi is much faster. – LLlAMnYP – 2016-03-09T09:24:06.190

I don't understand how it's cheeky or cruel, it's direct and does what is intended, right? – Jason B. – 2016-03-09T09:25:52.097

1It's so direct, it's almost mocking the OP, but I like, how it shows off Mathematica's capability to do the thinking for you. But on a more serious note, I can't imagine when it would be preferable to PrimePi. – LLlAMnYP – 2016-03-09T09:28:05.130

Because PrimePi will take non-prime numbers and give an answer – Jason B. – 2016-03-09T09:28:52.173

1primePi[x_?PrimeQ]:=PrimePi[x] – LLlAMnYP – 2016-03-09T09:30:02.500

Thank you both for your comments, they answered to the question I posed, only not for any prime p. I edited by question to include the case I was testing. – Jorge Leitao – 2016-03-09T09:30:07.380

@J.C.Leitão, neither of these would work for a number that large apparently – Jason B. – 2016-03-09T09:30:58.877

4You can attempt to use NextPrime[#, -1]& but good luck waiting for that computation to end in this universe. There's a reason, that these things are used for encryption, y'know :-) – LLlAMnYP – 2016-03-09T09:36:24.423


@J.C.Leitão This question What is so special about Prime? explicitly states this problem. Prime has an extensible domain, however you can't extend it up to rsa768a unless you have an enormous amount of RAM, but it is unlikely realizable.

– Artes – 2016-03-09T09:52:33.080

1This page links to quite a few algorithms you could code yourself for the prime counting function, but I don't see any of them listed as being applicable to a number over $10^{115}$ – Jason B. – 2016-03-09T09:59:59.797

@JasonB, thanks. Another option is to solve n Log[n] == p (or other approximation of $p_n$) for n. – Jorge Leitao – 2016-03-09T10:15:45.527


If you need a good approximation for PrimePi see e.g. Approximation to the prime counting function.

– Artes – 2016-03-09T10:22:59.883

1Indeed, you can't really expect to get an exact value of n in any sensible time for such large primes (well, maybe doing some original research in quantum computation might help), but maybe you can live with the fact RiemannR is good for an approximation. – kirma – 2016-03-09T11:38:16.540

No answers