11
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) -> (output)
4^8 -> 4^8=16^4=256^2=65536
5^11 -> 5^11=48828125
2^15 -> 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!
@JonathanAllan I was looking at that too.
32^3
and8^15
are not 512 either. – Yytsi – 2017-02-22T13:24:27.6731@JonathanAllan thanks for spotting that :) – Okx – 2017-02-22T16:06:21.667
@Okx the last one can be printed as
x^1
? – Rod – 2017-02-22T16:08:34.617@Rod no, it can't. That would be silly. – Okx – 2017-02-22T16:08:52.120