# Smith Number in Java

In this blog, we’ll understand the concept of Smith Number in Java, Methods to find smith Number in Java.

## What is Smith Number in Java?

Smith Numbers are special composite numbers that exhibit a unique property related to their digit sum and prime factorization. A number is considered a Smith Number if the sum of its digits is equal to the sum of the digits of its prime factorization.

To illustrate this concept, let’s consider the number 85. Its prime factorization is 5 * 17. The sum of its digits is 8 + 5 = 13, and the sum of the digits in its prime factorization is 5 + 1 + 7 = 13. Since both sums are equal, 85 is a Smith Number.

A. Here are a few more examples of Smith Numbers:

Number: 4

Prime Factorization: 2 * 2

Digit Sum: 4

Sum of Digits in Prime Factorization: 2 + 2 = 4

Explanation: 4 is a Smith Number because the sum of its digits matches the sum of the digits in its prime factorization.

Number: 22

Prime Factorization: 2 * 11

Digit Sum: 2 + 2 = 4

Sum of Digits in Prime Factorization: 2 + 1 + 1 = 4

Explanation: 22 is a Smith Number because the sum of its digits matches the sum of the digits in its prime factorization.

Number: 4937775

Prime Factorization: 3 * 5 * 5 * 65837

Digit Sum: 4 + 9 + 3 + 7 + 7 + 7 + 5 = 42

Sum of Digits in Prime Factorization: 3 + 5 + 5 + 6 + 5 + 8 + 3 + 7 = 42

Explanation: 4937775 is a Smith Number because the sum of its digits matches the sum of the digits in its prime factorization.

Smith Numbers provide an interesting and unique pattern in their digit sum and prime factorization, making them a captivating topic in number theory and programming.

B. Factors and Prime Factorization:

To understand Smith numbers fully, it’s essential to grasp the concept of factors and prime factorization. Factors are the numbers that can divide a given number without leaving a remainder. Prime factors, on the other hand, are the factors that are prime numbers.

For instance, let’s consider the number 60. Its factors are 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60. However, the prime factors of 60 are 2, 2, 3, and 5. Prime factorization involves expressing a number as the product of its prime factors.

On the other hand, in smith numbers, we sum the prime factors of a given number to determine its contribution to the Smith number property.

C. Composite Numbers and their Role in Smith Numbers:

This numbers, unlike prime numbers, are integers greater than 1 that are not divisible by only 1 and themselves. Composite numbers can be expressed as a product of two or more prime factors.

In the context of Smith numbers, composite numbers play a crucial role. Smith numbers, by definition, are composite numbers since they are not prime. The unique property of Smith numbers lies in their digit sum being equal to the sum of their prime factors.

By exploring composite numbers and their prime factorization, we can identify and verify Smith numbers using their distinct characteristics. Understanding the interplay between the factors and the digits helps us grasp the underlying mechanics of Smith numbers.

Take a look at our blog on Mischievous Numbers Program in Java!

## Java program that determines whether a given number is a Smith number or not

```import java.util.*;
public class SmithNumEx
{
//function finds the sum of digits of its prime factors
static int findSumPrimeFact(int num)
{
int i=2, sum=0;
while(num>1)
{
if(num%i==0)
{
sum=sum+findSumOfDigit(i);
num=num/i;
}
else
{
do
{
i++;
}
while(!isPrime(i));
}
}
//returns the sum of digits of prime factors
return sum;
}
//function finds the sum of digits of the given numbers
static int findSumOfDigit(int num)
{
//stores the sum
int s=0;
while(num>0)
{
//finds the last digit of the number and add it to the variable s
s=s+num%10;
//removes the last digit from the given number
num=num/10;
}
//returns the sum of digits of the number
return s;
}
//function checks if the factor is prime or not
static boolean isPrime(int k)
{
boolean b=true;
int d=2;
while(d<Math.sqrt(k))
{
if(k%d==0)
{
b=false;
}
d++;
}
return b;
}
//driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
//reads an integer from the user
int num=sc.nextInt();
//calling the user-defined function that finds the sum of digits of the given number
int a = findSumOfDigit(num);
//calling the user-defined function that finds the sum of prime factors
int b = findSumPrimeFact(num);
System.out.println("Sum of Digits of the given number is = "+a);
System.out.println("Sum of digits of its prime factors is = "+b);
//compare both the sums
if(a==b)
//prints if above condition returns true
System.out.print("The given number is a smith number.");
//prints if above condition returns false
else
System.out.print("The given number is not a smith number.");
}
}```

Explanation of the code:

It does so by finding the sum of the digits of the number and the sum of the digits of its prime factors. Here’s how the code works:

The findSumPrimeFact function takes an input number and afterward calculates the sum of the digits of its prime factors. It uses a loop to find the prime factors and then calls the findSumOfDigit function to calculate the sum of the digits for each prime factor.

The findSumOfDigit function takes an input number and calculates the sum of its digits. It uses a loop to extract the last digit from the number, add it to a sum variable, and then remove the last digit until no digits are left.

The isPrime function checks whether a given number is prime or not. It iterates from 2 to the square root of the number and checks if any number divides it evenly.

In the main function, the user inputs a number, and the program calculates the sum of its digits and the sum of its prime factors’ digits. It then compares both sums and determines whether the number is a Smith number or not.

Finally, the program outputs the sum of digits, the sum of digits of prime factors, and a message indicating whether the given number is a Smith number or not.

Consequently, the code utilizes functions to modularize the logic and follows an algorithmic approach to identify Smith numbers efficiently.

Check out our blog on Strontio Number in Java here!

#### Output:

``````Enter a number: 166
Sum of Digits of the given number is = 13
Sum of digits of its prime factors is = 13
The given number is a smith number.``````

## Smith numbers in Java within the range from 1 to 100

```import java.util.*;
public class SmithNumEx
{
//function finds all the prime factors for the given number
static List<Integer> findPrimeFactors(int n)
{
//creating an array list that stores the prime factors
List<Integer> res = new ArrayList<>();
for (int i = 2; n % i == 0; n = n/i)
for (int i = 3; i * i <= n; i =i+2)
{
while (n % i == 0)
{
n = n/i;
}
}
if (n != 1)
return res;
}
//function finds the sum of digits of the given number
static int sumOfDigits(int n)
{
int sum = 0;
while (n > 0)
{
//finds the last digit and add it to the sum
sum =sum+(n % 10);
//removes the last digit from the number
n = n/10;
}
//returns the sum of digits
return sum;
}
//driver code
public static void main(String args[])
{
//finds all smit numbers up to 10000
for (int n = 1; n < 100; n++)
{
//calling the user-defined function that finds prime factors
List<Integer> factors = findPrimeFactors(n);
//the size() method returns the number of elements in the list
//executes until the condition becomes false
if (factors.size() > 1)
{
int sum = sumOfDigits(n);
for (int f : factors)
sum =sum-sumOfDigits(f);
if (sum == 0)
System.out.println(n);
}
}
}
} ```

Explanation of  the code:

A Smith number is an integer whose sum of digits is equal to the sum of the digits of its prime factors. Here’s an explanation of the code:

1. findPrimeFactors(int n): This function takes an integer ‘n’ as input and then finds all its prime factors. It utilizes a list (ArrayList) to store the prime factors of ‘n’.

2. sumOfDigits(int n): This function calculates the sum of digits for the given integer ‘n’. Furthermore, it iterates through the digits of ‘n’, extracts each digit, and accumulates their sum.

3. main(String args[]): The main method is the entry point of the program. It loops through integers from 1 to 100 (inclusive) to check for Smith numbers.

4. For each number ‘n’, it calls findPrimeFactors(n) to get the prime factors and stores them in the ‘factors’ list.

5. If the size of the ‘factors’ list is greater than 1 (i.e., ‘n’ has more than one prime factor), it proceeds to calculate the sum of digits of ‘n’ and subtracts the sum of digits of each prime factor.

6. If the resulting sum is zero, it means ‘n’ is a Smith number, and the program prints it to the console.

As a result, the code efficiently identifies Smith numbers within the given range and outputs them as the result.

#### Output:

``````4
22
27
58
85
94``````

## Real-World Examples and Applications

### A. Practical Scenarios Where Smith Numbers are Relevant

Smith numbers have intriguing properties that find applications in various real-world scenarios. Some practical scenarios where Smith numbers are relevant include:

1. Cryptocurrency and Blockchain: In blockchain technology, Smith numbers play a role in generating cryptographic keys and then ensuring the security of transactions. They are used in the creation of public and private keys for digital wallets, contributing to the robustness of the cryptocurrency ecosystem.

2. Error Detection in Data Transmission: Smith numbers can be utilized in error detection algorithms for data transmission. However, by encoding data with Smith numbers, errors in the transmitted data can be detected and corrected, ensuring the integrity and accuracy of the information.

3. Prime Factorization and Cryptography: Smith numbers have unique properties related to their prime factors. These properties find applications in cryptographic algorithms that rely on prime factorization, such as RSA encryption, which is widely used to secure communication over the internet.

### B. Utilizing Smith Numbers in Cryptography and Number Theory

In the field of number theory and cryptography, Smith numbers hold significant importance. Indeed they offer insights into the distribution of prime factors and the relationships between divisors of a number. Some key applications of Smith numbers in cryptography and number theory include:

1. RSA Encryption and Decryption: RSA encryption, one of the most widely used cryptographic algorithms, relies on the difficulty of factoring large composite numbers into their prime factors. In this context, Smith numbers contribute significantly to the study of factorization algorithms, which underpin the security of RSA.

2. Number Factoring Algorithms: Smith numbers provide valuable data for testing and developing number factoring algorithms. Efficient algorithms for factoring large numbers are essential for cryptographic systems, Therefore, smith numbers serve as valuable test cases for these algorithms.

3. Cryptographic Key Generation: Generating secure cryptographic keys involves the use of large prime numbers. Smith numbers aid in the study of key generation techniques, on the other hand contributing to the development of robust and secure cryptographic systems.

### C. Sharing Interesting Case Studies and Observations

Researchers and mathematicians have explored fascinating case studies and observations related to Smith numbers. Moreover, some noteworthy findings include:

1. Smith Numbers in Nature: There have been observations of Smith numbers in natural phenomena, such as the distribution of petals in certain flowers or the arrangement of leaves on stems. Therefore, the occurrence of Smith numbers in nature sparks intriguing research and discussions.

2. Smith Numbers in Art and Design: Artists and designers have explored the aesthetic properties of Smith numbers and incorporated them into various art forms. From sculptures to paintings, Smith numbers inspire creativity and then add an element of mathematical beauty to artistic creations.

In conclusion, Smith numbers are unique integers that possess fascinating properties related to the sum of their digits and the prime factors of their divisors. They find relevance in number theory, cryptography, and various real-world applications.

We hope you the Java implementation provided in this article offers an efficient way to identify “Smith number in Java”. In brief it showcases how programming languages can be used to explore and understand the properties of these intriguing numbers. Also, visit the Newtum’s  website and sign up for our online Java, PHP, Python, and other relevant courses to keep improving your coding skills.