We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def check_condition(n):
# Function to compute the sum of digits of a number
def sum_of_digits(num):
return sum(int(digit) for digit in str(num))
# Compute the sum of digits of (n-1)
sum_digits_n_minus_1 = sum_of_digits(n - 1)
# Initialize sum of prime factors
prime_factors_sum = 0
# Check for factors of 2
while n % 2 == 0:
prime_factors_sum += 2
n //= 2
# Check for odd factors from 3 upwards
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
prime_factors_sum += i
n //= i
# If n is still greater than 2, it's prime and should be added
if n > 2:
prime_factors_sum += n
# Calculate the sum of digits of the sum of prime factors
sum_digits_prime_factors_sum = sum_of_digits(prime_factors_sum)
# Compare the two sums modulo 9 + 1
if sum_digits_n_minus_1 == sum_digits_prime_factors_sum:
Variable Naming: Renamed variables for clarity ( [sum](https://r2park.net/) _of_digits_of_numbers to sum_digits_n_minus_1, etc.).
return 1
else:
return 0
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Identify Smith Numbers
You are viewing a single comment's thread. Return to all comments →
import math
def check_condition(n): # Function to compute the sum of digits of a number def sum_of_digits(num): return sum(int(digit) for digit in str(num))