Python Number of Factors
dammit, I meant complement, not compliment :(
anyway, implementing the number of factors function while analyzing and improving the runtime of the solution.
In this video, we started off with a brute force solution, by checking every single number from 1 to n to see if its a factor of n. This is considered brute force because there is work being done that isn't necessary.
For each number, the last factor is always itself, and the factor before that will always be less than or equal to half of the number. Therefore, we could skip half the iterations after we get to number/2.
However, there is an even better way to solve the problem as every factor comes with a pair. Instead of checking all the numbers, we only need to check the numbers up to square root of the number. Therefore, we could write a much more efficient solution for our problem.
If n = 1,000,000 (1 million):
our first solution would take n iterations, so 1 million iterations.
our second solution would take half of n iterations, so 500k iterations.
our last solution would take square root of n iterations, so 1,000 iterations.
So, you can see that as n gets bigger, the number of iterations differ base on each of the 3 solutions, with our last solution being the most efficient.
Python Playlist:
https://youtube.com/playlist?list=PLnKe36F30Y4bcRomKi02sP9NR27KnBqCK
Subscribe for more coding tutorials 😄!