Mathematica, 70 69 bytes
1##&@@(Prime[BitXor[PrimePi@#+1,1]-1]^#2&)@@@FactorInteger@#/._@_->1&
An unnamed function which takes and returns an integer. It throws an error on input 1
but still computes the correct result.
Explanation
As usual, due to all the syntactic sugar, the reading order is a bit funny. A &
on the right defines an unnamed function and its arguments are referred to by #
, #2
, #3
, etc.
...FactorInteger@#...
We start by factoring the input. This gives a list of pairs {prime, exponent}
e.g. input 12
gives {{2, 2}, {3, 1}}
. Somewhat inconveniently, 1
gives {{1, 1}}
.
(...&)@@@...
This applies the function on the left to the list of integers at level 1, that is the function is called for each pair, passing the prime and exponent in as separate arguments, and then returns a list of the results. (This is similar to mapping the function over the list, but receiving two separate arguments is more convenient than receiving a pair.)
...PrimePi@#...
We compute the number of primes up to and including the (prime) input using the built-in PrimePi
. This gives us the index of the prime.
...BitXor[...+1,1]-1...
The result is incremented, XOR'ed with 1
and decremented again. This swaps 1 <-> 2, 3 <-> 4, 5 <-> 6, ...
, i.e. all 1-based indices. Note that input 1
will yield 0
for PrimePi
which is then mapped to -1
in this process. We'll deal with that later.
...Prime[...]^#2...
We now obtain the nth prime (where n is the result from the previous computation), which is the correctly swapped prime, and raise it to the power of the original prime in the factorisation of the input. At this point Prime[-1]
will throw an error but will return itself unevaluated. The power in this case is 1
so that the entire process so far yields {Prime[-1]}
for input 1
and a list of correct prime powers for all other inputs.
1##&@@...
Next, we just multiply up all prime powers. 1##&
is a standard golfing trick for the Times
function. See this tip (section "Sequences of arguments") for how it works.
Finally, we need to take care of input 1
for which all of the above resulted in Prime[-1]
. We can easily fix that with a simple replacement rule. Remember that f@x
is short for f[x]
. We just want to match any expression of that form (since all other results will be integers, i.e. atomic expressions), and replace it with a 1
:
.../._@_->1
Here, /.
is short for ReplaceAll
, _@_
is a pattern for anything of the form f[x]
(i.e. any compound expression with a single child) and ->1
says "replace with 1
".