if k is small you can just do a simple loop where you grow factorial running every loop iteration and same for the power, each loop increase a running power, and divide it and add. If the numbers can go out of range (and they will very quickly) you need to do something smarter. say K is 4. 1!,2!,3! are normal and then 4! is actually 3! ( 4^4/3!*4 = 4^3/3! right?) and so on. note that for 4, you can also divide top and bottom by 2, and so on... so some sort of ongoing fraction reduction strategy will let you run it out farther. If you need more than that, you will need to study it deeper.