How to resolve the algorithm Smith numbers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smith numbers step by step in the Java programming language

Table of Contents

Problem Statement

Smith numbers are numbers such that the sum of the decimal digits of the integers that make up that number is the same as the sum of the decimal digits of its prime factors excluding 1. By definition, all primes are excluded as they (naturally) satisfy this condition! Smith numbers are also known as   joke   numbers.

Using the number 166 Find the prime factors of 166 which are: 2 x 83 Then, take those two prime factors and sum all their decimal digits: 2 + 8 + 3 which is 13 Then, take the decimal digits of 166 and add their decimal digits: 1 + 6 + 6 which is 13 Therefore, the number 166 is a Smith number.

Write a program to find all Smith numbers below 10000.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smith numbers step by step in the Java programming language

The provided Java program identifies Smith numbers in the range 1 to 10,000. Smith numbers are positive integers where the sum of their digits is equal to the sum of the digits in their prime factors.

Here's a breakdown of the code:

  1. primeFactors method:

    • This method takes an integer n as input and returns a list of its prime factors.
    • It initializes an empty list result.
    • It enters a loop that divides n repeatedly by 2, adding 2 to the result list each time until n is no longer divisible by 2.
    • It then enters a loop that starts from 3 and iterates by 2 (to check odd numbers only). For each i, it repeatedly divides n by i and adds i to the result list until n is no longer divisible by i.
    • If n is still greater than 1 after both loops, it means n itself is a prime factor, so it is added to the result list.
    • Finally, the method returns the result list containing the prime factors of n.
  2. sumDigits method:

    • This method takes an integer n as input and returns the sum of its digits.
    • It initializes a variable sum to 0.
    • It enters a loop that repeatedly calculates the last digit of n by taking n % 10, adds that digit to sum, and then removes the last digit from n by dividing it by 10.
    • The loop continues until n becomes 0.
    • Finally, the method returns the sum of the digits.
  3. main method:

    • This is the entry point of the program.
    • It iterates over integers from 1 to 9,999 (exclusive).
    • For each n, it calls the primeFactors method to obtain the list of prime factors of n.
    • If n has more than one prime factor (i.e., the size of the prime factors list is greater than 1), it proceeds to check if it's a Smith number.
    • It calculates the sum of digits of n using the sumDigits method.
    • It then iterates over the prime factors of n and subtracts the sum of digits of each prime factor from the sum of digits of n.
    • If the result of this subtraction is 0, it indicates that n is a Smith number, and it is printed to the console.

Source code in the java programming language

import java.util.*;

public class SmithNumbers {

    public static void main(String[] args) {
        for (int n = 1; n < 10_000; n++) {
            List<Integer> factors = primeFactors(n);
            if (factors.size() > 1) {
                int sum = sumDigits(n);
                for (int f : factors)
                    sum -= sumDigits(f);
                if (sum == 0)
                    System.out.println(n);
            }
        }
    }

    static List<Integer> primeFactors(int n) {
        List<Integer> result = new ArrayList<>();

        for (int i = 2; n % i == 0; n /= i)
            result.add(i);

        for (int i = 3; i * i <= n; i += 2) {
            while (n % i == 0) {
                result.add(i);
                n /= i;
            }
        }

        if (n != 1)
            result.add(n);

        return result;
    }

    static int sumDigits(int n) {
        int sum = 0;
        while (n > 0) {
            sum += (n % 10);
            n /= 10;
        }
        return sum;
    }
}


  

You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Fantom programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the Haskell programming language
You may also check:How to resolve the algorithm Null object step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Comments step by step in the Yacas programming language
You may also check:How to resolve the algorithm Arrays step by step in the Robotic programming language