• + 0 comments

    My python solution (I'm new to python!):

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'solve' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts INTEGER n as parameter.
    #
    
    def sum_digits(n):
        digits = 0
        while n != 0:
            digits += n % 10
            n = n // 10
        return digits
    
    def sum_prime_factors(n):
        factors = 0
        factor_list = []
        while n % 2 == 0:
            factors += 2
            factor_list.append(2)
            n = n // 2
        for i in range(3, int(math.sqrt(n)) + 1, 2):
            while n % i == 0:
                factors += sum_digits(i)
                factor_list.append(i)
                n = n // i
        if n > 2:
            factor_list.append(n)
            num = sum_digits(n)
            factors += num
        return factors
    
    def solve(n):
        factors = sum_prime_factors(n)
        digits = sum_digits(n)
        if factors == digits:
            return 1
        else:
            return 0
            
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        n = int(input().strip())
    
        result = solve(n)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()