Introduction
Watson gives an integer N
to Sherlock and asks him: What is the number of divisors of N
that are divisible by 2
?
Input and Output Format
|
|
{: file="Input and Output” }
Explanation
9 has three divisors 1, 3 and 9 none of which is divisible by 2.
8 has four divisors 1,2,4 and 8, out of which three are divisible by 2.
Process
I thought that this is one of the easier ones to do, I first did a while loop to loop through all the numbers from 1 to N
and check if it is divisible by 2. If it is, I increment a counter. I then print the counter. This only worked for the first test case. I then did a for loop to loop through all the numbers from 1 to N
and check if it is divisible by 2. But this was a code that was not optimized for HackerRank’s tests and failed some test cases.
I then loop through numbers from 1 to square root of n
as the divisors of n
are always less than or equal to the square root of n
.
- I first checked that
i
is divisible byn
. - I then check if the number
i
is divisible by 2 and increment the counter if it is. - Next, I checked if
n/i
is divisible by 2 and increment the counter if it is. - THe last statement is to remove the number
n
if it is divisible by 2 as it is a duplicate.
This code was after some trial and error and multiple references.
Code
|
|
{: file="divisors.c” }
Afterthoughts
Math theory was very important and applicable to this challlenge, the fact that the divisors of n
are always less than or equal to the square root of n
was very important. This is also one of the ways to check if the number is a prime doing a prime factorization from 1 to the square root of the number.
This is an interesting challenge that made me apply my math knowledge learnt.