Soluție HackerRank pentru Sherlock and Divisors, subdomeniul Fundamentals, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod…

  • Problemă: Sherlock and Divisors
  • Domeniu: Fundamentals
  • Limbaj: Python 3

Challenge: Sherlock and Divisors

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 30.0 / 30

Submission status: Accepted

Submission score: 1.0

Submission ID: 464726814

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/sherlock-and-divisors/problem

Cerință

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

Input Format
First line contains T, the number of testcases. This is followed by T lines each containing an integer N.

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

Constraints
1 ≤ T ≤ 100
1 ≤ N ≤ 10^9

Sample Input

2
9
8

Sample Output

0
3

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.

Cod sursă

#!/bin/python3

import math
import os
import random
import re
import sys

def divisors(n):
    count = 0
    for i in range(1,int(math.sqrt(n))+1):
        if n%i==0 and i%2==0:
            # i is even
            count+=1
        if n%(n//i)==0 and (n//i)%2==0:
            # n//i is even and n's factor
            count+=1
        if i==n//i and i%2==0 and n%i==0:
            # if i is sqrt reduce by 1
            count-=1
    return count
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())

    for t_itr in range(t):
        n = int(input().strip())

        result = divisors(n)

        fptr.write(str(result) + '\n')

    fptr.close()
HackerRank Fundamentals – Sherlock and Divisors