-4

The problem statement is:

Watson gives an integer NN to Sherlock and asks him: What is the number of divisors of NN that are divisible by 2?.

Input Format

First line contains TT, the number of testcases. This is followed by TT lines each containing an integer NN.

Output Format

For each testcase, print the required answer in one line.

Constraints

1≤T≤1001≤T≤100 
1≤N≤109

Sample Input :

2\n
9\n
8

Sample Output:

0\n
3

I have typed '\n' which means the next integer will be in a new line. Hacker Rank would not accept my code due to timeout. Please help me optimise this C code.

My C code is :

int main() {

      int length,number,i,count =0;
      scanf("%d",&length);

      while(length--){
           scanf("%d",&number);
           for(i=2;i<=number/2;i++){
                 if(number % i == 0 && i % 2 == 0){
                   count = count + 1;
                  }
           }
           if(number % 2 == 0){
             count = count + 1;
            }
           printf("%d\n",count);
           count =0;
        }
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Gauraang Khurana
  • 707
  • 1
  • 8
  • 18
  • 1
    For one thing, change your `i` loop to use `i += 2` to increment rather than `i++`. That way you skip all the odd numbers. Then you can also get rid of the `i % 2 == 0` test, since you will know it's always true. – Tom Karzes Apr 02 '16 at 04:45
  • There is no loop needed if `number` is odd. – kaylum Apr 02 '16 at 04:53
  • what is the name of the problem on Hackerrank? – Rajeev Singh Apr 02 '16 at 05:06
  • @RajeevSingh: [Here](https://hackerrank-challenge-pdfs.s3.amazonaws.com/2536-sherlock-and-divisors-English?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&Expires=1459581740&Signature=VL2KB4XqPX79k%2FNPKsMPPrcdP74%3D&response-content-disposition=inline%3B%20filename%3Dsherlock-and-divisors-English.pdf&response-content-type=application%2Fpdf). – M Oehm Apr 02 '16 at 06:23
  • 2
    Many online competitions give you a simple problem that is trivial to solve with brute force for the small data sets or for the small numbers in the examples. When you sumbit the code, they throw huge numbers at you so that your brute-force approach times out. The challenge is to exploit the properties of the data to speed up the solution. Your solution uses brute force. – M Oehm Apr 02 '16 at 06:32

1 Answers1

1

Below is a algo to solve this problem:

If the number is even:
Run the loop from i=2 till square root of the number ie: sqrt(number) and increase the count in two cases below if i divides the number(number%i==0) :

  • if i is even(i%2==0)

  • for q where q=number/i if i not equal to q(i!=q) and q is even(q%2==0) because if i divides the number it means q will also divide(number/q=i) it.

At last increase the count by 1 as number also divides itself.

If the number is odd:
count will be 0 as no i(even) would divide an odd number.

int main() 
{
  int length,number,i,count;
  scanf("%d",&length);

  while(length--)
  {
      scanf("%d",&number);
      count =0;i=2;
      if(number%2==0)
      {
          while(i*i<=number)
          {
              if(number%i==0)
              {
                  if(i%2==0)
                      count++;
                  int q=number / i;
                  if (i !=q && q%2==0)
                      count++;
              }
              i++;
          }
          count++;
      }
      printf("%d\n",count);
  }
  return 0;
}
Rajeev Singh
  • 3,292
  • 2
  • 19
  • 30
  • 1
    `sqrt` is a floating-point number and may have issues with rounding; maybe `i*i < n` is a better condition. You can further optimize this by testing for `number % i == 0` first in the inner while loop and then calculate `q` only if `i` is a divisor. In that case `number % q == 0` is guaranted to be true and you can skip this test. – M Oehm Apr 02 '16 at 07:00
  • thanks for the optimization, it now takes 0.01s less time :) – Rajeev Singh Apr 02 '16 at 07:13
  • Well, the gain depends on how much it took before. I could cut the execution time in half with that optimisation in my tests. (I've run tests on more than 100 numbers, though.) – M Oehm Apr 02 '16 at 08:40