Today I gave myself challenge day on trying to solve a toy problem which is a algorithm problem. The start was so good that I thought I could've solved it but I encountered a problem on the big O part. I still will need to understand what the big O actually is in programming. However, then I decided to learn from the reference code.
The problem required me to come up with an algorithm function that when given an input of a base number and a exponent number, returns the result. So if 3, 40 is given as the parameters the output is
19334827.
The code is as follows:
function power(base, exponent) {
if (exponent === 0) {
return 1; // this breaks the recursive code.
}
const half = parseInt(exponent / 2); // this halves all the exponent during the recursive code to go until exponent becomes 0. parseInt when used here returns rounded decimal points. if 2.5 it returns 2 when is used with parseInt.
const temp = power(base, half); // this is a recursive function to break down the big exponent number into small functions. This part in the code seemed to pass the big O problem I encountered when using for loop to solve the algorithm
const result = (temp * temp) % 94906249
if (exponent % 2 === 1){
return (base * result) % 94906249
} else {
return result
}
}
I broke down the code into smaller parts and learned how the code works and I learned that I came some way until the time where I can understand such codes. It took me some time to understand the code but I still feel good on being able to finish this question and also learn somethings.
Coding is such a hard subject but I learned that when given some time to analyze a code, I still can use past experiences to come up with possible answers to the questions. I should know that my progress is slow but there is something happening. Thank God.
Having a eye, manager, and a worker is something I would need to start a bootcamp in Thailand.