Sort by

recency

|

131 Discussions

|

  • + 0 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))

    # 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
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int a=sc.nextInt();
            int s=0;
            int b=a;
            for(int x=2;x<=a;x++)
            {
                while(a%x==0)
                {
                    a=a/x;
                    int q=x;
                    while(q>0)
                    {
                        s=s+q%10;
                        q=q/10;
                    }
                }
            }        
            int u=0;
            while(b>0)
            {
                u=u+b%10;
                b=b/10;
            }
            
            if(u==s)
            {
                System.out.println("1");
            }
            else
            {
                System.out.println("0");
            }
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        }
    }
    
  • + 0 comments
    def primeFactors(n):
         
        # Print the number of two's that divide n
        a = []
        while n % 2 == 0:
            a.append(2)
            n = n // 2
             
        # n must be odd at this point
        # so a skip of 2 ( i = i + 2) can be used
        for i in range(3,int(math.sqrt(n))+1,2):
             
            # while i divides n , print i and divide n
            while n % i== 0:
                a.append(i)
                n = n // i
                 
        # Condition if n is a prime
        # number greater than 2
        
        if n > 2:
            a.append(n)
        return a
    def sum_of_digits(n):
        s=0
        while(n!=0):
            s+=n%10
            n=n//10
        return s
    
    def solve(n):
        # Write your code here
        a=primeFactors(n)
        s = 0
        for i in range(len(a)):
            if a[i] <10:
                s+=a[i]
            else:
                s+=sum_of_digits(a[i])
        s1 = sum_of_digits(n)
        if s1 == s:
            return 1
        else:
            return 0
        
    
  • + 0 comments

    int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10);

    if (endptr == str || *endptr != '\0') {
        exit(EXIT_FAILURE);
    }
    
    return value;
    

    } int sum_of_digits(int num) {

    // Function to calculate the sum of the digits of a number
    int sum = 0;
    while (num > 0) {
        sum += num % 10;
        num /= 10;
    }
    return sum;
    

    } int sum_of_prime_factors_digits(int num) {

    // Function to calculate the sum of the digits of prime factors
    int sum = 0;
    int factor = 2;
    while (num > 1) {
        // Calculate prime factors and sum their digits
        while (num % factor == 0) {
            sum += sum_of_digits(factor);
            num /= factor;
        }
        factor++;
        if (factor * factor > num) {
            break;
        }
    }
    // Add the digits of any remaining prime factor greater than 1
    if (num > 1) {
        sum += sum_of_digits(num);
    }
    return sum;
    

    } int solve(int n) {

    // Check if the given integer is less than 4 (cannot be a Smith number)
    if (n < 4) {
        return 0;
    }
    
    // Calculate the sum of the digits of the integer
    int sum_num_digits = sum_of_digits(n);
    
    // Calculate the sum of the digits of prime factors
    int sum_factors_digits = sum_of_prime_factors_digits(n);
    
    // Return 1 if the sums are equal, otherwise return 0
    if (sum_num_digits == sum_factors_digits) {
        return 1; // n is a Smith number
    } else {
        return 0; // n is not a Smith number
    }
    

    }

  • + 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()