Calculate Exponents bit by bit Your task is to slowly calculate exponentiation, with the following steps:

Given two inputs (in this example, 4 and 8), you must calculate the exponentiation by calculate the equation bit by bit. You would do 4^8, have a greater base value (4) and a smaller exponent (8). You can do this using more exponentiation and division. You can divide the exponent by a value X (provided X is a prime divisor of the exponent), and make the base value (B) into B^X. For example, you can do:

4^8 = (4 ^ 2)^(8 / 2) = 16^4


I have replaced X with 2 in the previous equation.

You can 'simplify' 16^4 further, again with X = 2:

16^4 = (16 ^ 2)^(4 / 2) = 256^2


And then finally calculate a number (again, X = 2):

256^2 = (256 ^ 2)^(2 / 2) = 65536^1 = 65536


Therefore,

4^8 = 16^4 = 256^2 = 65536


This is the output you should give. The output separator is a little flexible, for example, you can separate the equations by newlines or spaces instead of =. Or, you may put them into a list (but you mustn't use a digit or the ^ character as a separator).

As Martin Ender pointed out, the ^ is also flexible. For example, you may use [A, B] or A**B instead of A^B in the output.

X may only be prime, which means you cannot use X = 8 to get straight to the solution, and the values of X will only be prime factors of the second input (the exponent).

Examples:

(input) -&gt; (output)
4^8 -&gt; 4^8=16^4=256^2=65536
5^11 -&gt; 5^11=48828125
2^15 -&gt; 2^15=32^3=32768 (2^15=8^5=32768 is also a valid output)


Mind that the input format is also flexible (eg you may take A \n B or A B instead of A^B. Obviously, this wouldn't be a problem if you write a function taking two arguments.

In the second example, we go straight to calculation, since 11 is prime and we cannot take any more steps.

You may write a program or a function to solve this, and you may print or return the value, respectively.

As this is code-golf, that shortest code wins!
JavaScript (ES7), 55 bytes

f=(a,b,c=2)=&gt;b&gt;1?b%c?f(a,b,c+1):a+['^'+b,f(a**c,b/c)]:a


Uses , in place of = (2^15,8^5,32768).

Test cases



f=(a,b,c=2)=&gt;b&gt;1?b%c?f(a,b,c+1):a+['^'+b,f(Math.pow(a,c),b/c)]:a
g=(a,b)=&gt;console.log(`f(${a}, ${b}):`,f(a,b))

g(4,8)
g(5,11)
g(2,15)




Note: the snippet uses Math.pow instead of ** for cross-browser compatibility.