Problem Description: Your task is to create a new implementation of modpow so that it computes (x^y)%n for large y.
The problem with the current implementation is that the output of Math.pow is so large on our inputs that it won't fit in a 64-bit float.
You're also going to need to be efficient, because we'll be testing some pretty big numbers.
Level:5kyu
Link To Kata: Efficient Power Modulo n
This was a 1 level up Kata. From today onwards I will start working on level 5 Kata. Here we are asked to find an efficient algorithm to find a^b%n for very big numbers. We start solving this by initializing our result variable to 1. Then we move through a loop which continues until our b variable is not equal to zero. We check if b modulus 2 is a non zero number then we multiply our result variable with a and get its modulus with n. We square the value of a and get its modulus with n. In the end we change the value of b to its half.
In the end we return the value of our result variable as the answer to this Kata.
Please go through the code for a better understanding.
Here is the link to code: Kata Day 55
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment