A Smith Number is an interesting mathematical number in which the sum of the digits of the number is equal to the sum of the digits of all its prime factors. It is a programming problem that combines prime number checking, prime factorisation, digit extraction, loops, conditional statements and user-defined methods.
In Java, this problem can be solved by first checking whether the given number is valid and composite, finding its prime factors and then comparing the digit sum of the original number with the digit sum of all its prime factors. Let us understand the concept, develop the algorithm, write the Java program and examine its working through examples and a detailed dry run.
What Is a Smith Number?
A Smith number is a composite number for which the sum of the digits of the number is equal to the sum of the digits of all its prime factors. A prime factor is counted as many times as it divides the number.
For example, consider the number 666.
Its prime factorisation is:
666 = 2 × 3 × 3 × 37
The sum of the digits of 666 is:
6 + 6 + 6 = 18
The sum of the digits of its prime factors is:
2 + 3 + 3 + (3 + 7) = 18
Since both sums are equal, 666 is a Smith Number.
Consider another example, 85.
Its prime factorisation is:
85 = 5 × 17
Digit sum of 85:
8 + 5 = 13
Digit sum of its prime factors:
5 + (1 + 7) = 13
Therefore, 85 is also a Smith Number.
Important: A Smith number must always be a composite number. Prime numbers are not considered Smith numbers, even if their digit sums appear to satisfy a similar condition.
Algorithm to Check a Smith Number
The program follows a systematic approach to determine whether a given composite number is a Smith number.
Step-by-step algorithm:
- Start.
- Accept an integer N from the user.
- Check whether N is less than or equal to 1.
- If N ≤ 1, display INVALID INPUT and terminate the program.
- Initialise a variable count to 0.
- Run a loop from 1 to N.
- Check whether N is exactly divisible by the loop variable.
- If divisible, increment count.
- If count is equal to 2, the number is prime. Display “Prime number: Not a Smith number” and terminate.
- Otherwise, initialise:
- x = N
- sof = 0
- i = 2
- st = an empty string
- Repeat the following steps while x is not equal to 1:
- Check whether x is divisible by i.
- If divisible:
- Divide x by i.
- Append i to the string st.
- Calculate the sum of digits of i using the sod() method.
- Add the result to sof.
- Otherwise, increment i.
- Calculate the sum of digits of N using sod(N).
- Compare the digit sum of N with sof.
- If both values are equal, display “Smith Number”.
- Otherwise, display “Not a Smith Number”.
- Stop.
Java Program to Check Smith Number
The following program uses a user-defined static method sod() to calculate the sum of digits. It also uses a factor-counting technique to identify prime numbers and a while loop to determine the prime factors.
import java.util.Scanner;
public class Smith_number
{
public static int sod(int n)
{
int sum = 0, dig = 0;
while(n != 0)
{
dig = n % 10;
sum = sum + dig;
n = n / 10;
}
return sum;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int num = sc.nextInt();
if(num <= 1)
{
System.out.println("INVALID INPUT");
System.exit(0);
}
else
{
int count = 0;
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
{
count++;
}
}
if(count == 2)
{
System.out.println("Prime number: Not a Smith number");
System.exit(0);
}
else
{
int x = num, sof = 0;
int i = 2;
String st = "";
while(x != 1)
{
if(x % i == 0)
{
x = x / i;
st = st + i + " x ";
sof = sof + sod(i);
}
else
i++;
}
System.out.println("Prime Factors: " +st.substring(0, st.length() - 3));
int s = sod(num);
System.out.println("Digit Sum of N: " + s);
System.out.println("Digit Sum of Factors: " + sof);
if(s == sof)
System.out.println("Smith Number");
else
System.out.println("Not a Smith Number");
}
}
}
}Explanation of the Methods
sod(int n)
This method calculates the sum of the digits of a given integer.
It uses two arithmetic operators:
- Modulus operator (%) extracts the last digit of a number.
- Integer division (/) removes the last digit of a number.
Consider the following example:
n = 666
| Iteration | n | dig = n % 10 | sum | n = n / 10 |
| 1 | 666 | 6 | 6 | 66 |
| 2 | 66 | 6 | 12 | 6 |
| 3 | 6 | 6 | 18 | 0 |
When n becomes 0, the loop terminates and the method returns 18.
main()
The main() method performs the following operations:
- Accepts an integer from the user using the Scanner class.
- Checks whether the input is valid.
- Determines whether the number is prime by counting its factors.
- If the number is composite, finds its prime factors.
- Calculates the digit sum of the original number and its prime factors.
- Compares both sums to determine whether the number is a Smith number.
Understanding the Important Loops
There are three important loops in this program. Understanding their purpose is essential for writing the solution correctly.
- Prime Number Checking Loop
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
{
count++;
}
}This loop counts the total number of factors of the given number.
For example, consider N = 13.
The factors are:
1 and 13.
Therefore, count = 2.
A number having exactly two factors is a prime number.
If count == 2, the program displays the prime number message and terminates.
Prime Factorisation Loop
while(x != 1)
{
if(x % i == 0)
{
x = x / i;
st = st + i + " x ";
sof = sof + sod(i);
}
else
i++;
}This is the most important loop in the program.
It repeatedly divides the number by its smallest possible prime factor.
For example:
666 = 2 × 3 × 3 × 37
The value of i remains unchanged when a factor divides x successfully. This is important because the same factor may divide the number more than once.
Consider the factor 3 in 666:
- 666 is divisible by 2. The remaining number becomes 333.
- 333 is divisible by 3. The remaining number becomes 111.
- 111 is again divisible by 3. The remaining number becomes 37.
Thus, the factor 3 is counted twice.
Remember: The loop must not increment i immediately after successful division because repeated prime factors need to be counted.
Digit Sum Calculation Loop
while(n != 0)
{
dig = n % 10;
sum = sum + dig;
n = n / 10;
}
This loop extracts each digit and adds it to the variable sum.
For example, if n = 85:
- First iteration: dig = 5, sum = 5.
- Second iteration: dig = 8, sum = 13.
The method returns 13.
Sample Executions
Execution 1: Input = 666
Enter the number
666
Prime Factors: 2 x 3 x 3 x 37
Digit Sum of N: 18
Digit Sum of Factors: 18
Smith Number
Explanation:
The prime factors of 666 are 2, 3, 3 and 37.
Digit sum of the original number = 18.
Digit sum of the prime factors = 2 + 3 + 3 + 3 + 7 = 18.
Since both sums are equal, 666 is a Smith number.
Execution 2: Input = 85
Enter the number
85
Prime Factors: 5 x 17
Digit Sum of N: 13
Digit Sum of Factors: 13
Smith Number
Explanation:
The prime factors of 85 are 5 and 17.
Digit sum of 85 = 8 + 5 = 13.
Digit sum of its prime factors = 5 + 1 + 7 = 13.
Therefore, 85 is a Smith number.
Execution 3: Input = 18
Enter the number
18
Prime Factors: 2 x 3 x 3
Digit Sum of N: 9
Digit Sum of Factors: 8
Not a Smith Number
Explanation:
The prime factors of 18 are 2, 3 and 3.
Digit sum of 18 = 1 + 8 = 9.
Digit sum of its prime factors = 2 + 3 + 3 = 8.
Since both sums are different, 18 is not a Smith number.
Execution 4: Input = 13
Enter the number
13
Prime number: Not a Smith number
Explanation:
13 has exactly two factors: 1 and 13.
Therefore, it is a prime number and cannot be classified as a Smith number.
Execution 5: Input = 1
Enter the number
1
INVALID INPUT
Explanation:
Since the input is less than or equal to 1, it is invalid according to the given condition.
Dry Run for N = 666
Let us understand the execution of the program by tracing each iteration of the prime factorisation loop.
Given:
N = 666
Initial values:
x = 666;
i = 2;
sof = 0;
st = “”;
The loop continues until x becomes 1.
Dry Run Table
| Iteration | i | x before division | x after division | sod(i) | sof |
| 1 | 2 | 666 | 333 | 2 | 2 |
| 2 | 3 | 333 | 111 | 3 | 5 |
| 3 | 3 | 111 | 37 | 3 | 8 |
| 4 | 4 to 36 | 37 | 37 | — | 8 |
| 5 | 37 | 37 | 1 | 10 | 18 |
Note: In iteration 4, the loop checks the values from 4 to 36. None divides 37, so i is incremented each time. The factorisation continues when i becomes 37.
Explanation of Each Successful Division
Iteration 1:
- i = 2
- x = 666
- 666 % 2 = 0
- x = 666 / 2 = 333
- sof = 0 + sod(2) = 2
The factor 2 is stored.
Iteration 2:
- i = 3
- x = 333
- 333 % 3 = 0
- x = 333 / 3 = 111
- sof = 2 + sod(3) = 5
The factor 3 is stored.
Iteration 3:
- i = 3
- x = 111
- 111 % 3 = 0
- x = 111 / 3 = 37
- sof = 5 + sod(3) = 8
The factor 3 is stored again.
Iteration 4:
- The values of i from 4 to 36 do not divide 37.
- Therefore, i keeps increasing.
Iteration 5:
- i = 37
- x = 37
- 37 % 37 = 0
- x = 37 / 37 = 1
- sof = 8 + sod(37) = 18
The loop terminates because x becomes 1.
Final Calculation
Digit sum of N:
6 + 6 + 6 = 18
Digit sum of prime factors:
2 + 3 + 3 + (3 + 7) = 18
Therefore:
s = sof = 18
Final Result: 666 is a Smith Number.
Important Concepts Used in the Program
| Java Concept | Purpose |
| Scanner class | Accepting input from the user |
| Static method | Calculating the sum of digits |
| % operator | Extracting digits and checking divisibility |
| / operator | Removing the last digit and dividing by factors |
| for loop | Counting the factors of a number |
| while loop | Finding prime factors and extracting digits |
| if-else | Checking conditions and comparing sums |
| String concatenation (+) | Storing the prime factors |
| substring() | Removing the extra separator |
| System.exit(0) | Terminating the program |
Why is sod(i) used instead of simply adding i?
Consider the prime factor 37.
If we add i directly:
sof = sof + 37
This would be incorrect because the Smith number condition requires the sum of the digits of the prime factors.
The correct calculation is:
sod(37) = 3 + 7 = 10
Therefore, the program uses:
sof = sof + sod(i);
This statement works for both single-digit and multiple-digit prime factors.
For example:
- sod(2) = 2
- sod(3) = 3
- sod(17) = 8
- sod(37) = 10
It also ensures that repeated factors are counted separately.
Important Examination Tips
Students should remember the following points while writing this program in their ISC practical examination:
- Always validate the input before performing any calculation.
- A Smith number must be composite.
- A prime number has exactly two factors.
- Initialise the factor variable i to 2.
- Use a while loop to extract repeated prime factors.
- Do not increment i when a successful division takes place.
- Use sod(i) to calculate the digit sum of each prime factor.
- Repeated prime factors must be counted separately.
- Calculate the digit sum of the original number using sod(num).
- Compare the two sums to determine whether the number is a Smith number.
- Use substring() carefully when removing the trailing separator from the factor string.
Conclusion
The Smith Number program is an excellent Java programming exercise that tests a student’s understanding of prime number checking, factorisation, digit extraction, loops, string manipulation and user-defined methods.
The most important part of the solution is the prime factorisation process. Students must understand why the same prime factor is checked repeatedly and why the digit sum of every factor must be calculated separately.
By understanding the algorithm, tracing the dry run and practising different input values, students can confidently solve similar number-based programming problems in Java.
Important Viva Questions and Answers
Q1. What is a Smith Number?
A Smith number is a composite number for which the sum of its digits is equal to the sum of the digits of all its prime factors, including repeated factors.
Q2. Is every composite number a Smith number?
No. A composite number is a Smith number only when the digit sum of the original number equals the digit sum of all its prime factors.
Q3. Can a prime number be a Smith number?
No. By definition, a Smith number must be composite.
Q4. Why is the variable count initialised to zero?
It stores the total number of factors of the given number. It is used to determine whether the number is prime.
Q5. Why is count == 2 used to identify a prime number?
A prime number has exactly two distinct factors: 1 and itself.
Q6. What is the purpose of the sod() method?
It calculates and returns the sum of the digits of a given integer.
Q7. Why is the modulus operator (%) used in the sod() method?
It extracts the last digit of a number.
Q8. Why is integer division used in the sod() method?
Integer division by 10 removes the last digit of the number.
Q9. Why does the prime factorisation loop start with i = 2?
Because 2 is the smallest prime number and the smallest possible prime factor of any composite number.
Q10. Why is a while loop used for prime factorisation?
A prime factor may occur more than once. The while loop allows the same factor to divide the number repeatedly.
Q11. Why is i not incremented immediately after successful division?
To ensure that repeated occurrences of the same prime factor are identified and counted.
Q12. Why is sod(i) used while calculating sof?
Because the Smith number condition requires the sum of the digits of each prime factor, not the numerical value of the factor itself.
Q13. What is the purpose of the variable sof?
It stores the sum of the digits of all prime factors, including repeated factors.
Q14. Why is substring() used in the program?
It removes the extra separator ” x ” present at the end of the string containing the prime factors.
Q15. Is 666 a Smith number?
Yes. Its digit sum is 18, and the digit sum of its prime factors (2, 3, 3 and 37) is also 18.
Q16. Is 18 a Smith number?
No. The digit sum of 18 is 9, whereas the digit sum of its prime factors (2, 3 and 3) is 8.
Q17. What happens when the input is less than or equal to 1?
The program displays INVALID INPUT and terminates.
Q18. Why is System.exit(0) used in the program?
It terminates the program immediately after detecting invalid input or a prime number.
Q19. Can a prime factor contain more than one digit?
Yes. For example, 17 and 37 are prime factors of 85 and 666 respectively.
Q20. Give another example of a Smith number.
85 is a Smith number because its digit sum is 8 + 5 = 13, and the digit sum of its prime factors 5 and 17 is also 5 + 1 + 7 = 13.