Program to find probability that any proper divisor of n would be an even perfect square number in Python

Given a number n, we need to find the probability that any proper divisor of n would be an even perfect square number. A proper divisor is any positive divisor of n except n itself.

So, if the input is like n = 36, then the output will be 1/8 because there are eight proper divisors of 36, these are {1, 2, 3, 4, 6, 9, 12, 18} and among them only one number (4) is both a perfect square and even.

Algorithm

To solve this, we will follow these steps ?

  • If n mod 4 is not equal to 0, return 0 (no even perfect square divisors possible)
  • Otherwise, perform prime factorization of n
  • Calculate total proper divisors and count even perfect square divisors
  • Return the probability as a simplified fraction

Example

Let us see the following implementation to get better understanding ?

from math import gcd

def solve(n):
    if n % 4 != 0:
        return 0
    else:
        nc = n
        ptr = 2
        prime_powers = []
        
        # Prime factorization
        while ptr <= nc ** 0.5:
            power = 0
            while nc % ptr == 0:
                power += 1
                nc = nc // ptr
            if power > 0:
                prime_powers.append(power)
            ptr += 1
        
        if nc > 1:
            prime_powers.append(1)
        
        # Calculate total proper divisors and even perfect square divisors
        k = prime_powers[0]
        total_divisors = k + 1
        even_perfect_squares = k // 2
        
        for i in prime_powers[1:]:
            total_divisors *= (i + 1)
            even_perfect_squares *= (i // 2) + 1
        
        total_divisors -= 1  # Exclude n itself (proper divisors)
        
        # If n is a perfect square, exclude it from even perfect squares
        if int(n ** 0.5) ** 2 == n:
            even_perfect_squares -= 1
        
        # Simplify the fraction
        g = gcd(total_divisors, even_perfect_squares)
        total_divisors //= g
        even_perfect_squares //= g
        
        if even_perfect_squares == 0:
            return 0
        else:
            return f"{even_perfect_squares}/{total_divisors}"

# Test with example
n = 36
print(f"Input: {n}")
print(f"Probability: {solve(n)}")
Input: 36
Probability: 1/8

How It Works

For n = 36:

  • Prime factorization: 36 = 2² × 3²
  • All divisors: {1, 2, 3, 4, 6, 9, 12, 18, 36}
  • Proper divisors: {1, 2, 3, 4, 6, 9, 12, 18} (8 total)
  • Even perfect squares among proper divisors: {4} (1 total)
  • Probability = 1/8

Additional Test Cases

# Test with different values
test_cases = [36, 16, 12, 8]

for n in test_cases:
    result = solve(n)
    print(f"n = {n}: Probability = {result}")
n = 36: Probability = 1/8
n = 16: Probability = 1/4
n = 12: Probability = 1/5
n = 8: Probability = 1/3

Conclusion

This algorithm uses prime factorization to efficiently calculate the probability that a proper divisor is an even perfect square. The key insight is that only numbers divisible by 4 can have even perfect square divisors, and the calculation involves counting divisors based on prime power exponents.

Updated on: 2026-03-26T18:27:10+05:30

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements