Smith Number

Back to Programming


A number is said to be a smith number if the sum of the digits of the number is equal to the sum of the digits of its prime factors (excluding 1) found using the prime factorization method.


For example, if we consider a number 666, the sum of the digits is 6 + 6 + 6 = 18

The factorization of 666 is shown below:

Therefore, the sum of the digits of the prime factors are: (2+3+3+ (3+7)) = 18 which is equal to the sum of its digits. Therefore, 666 is a smith number.

 If we consider a number 21, then the sum of its digits is 2+1 = 3. The prime factors of 21 are 3 and 7. The sum of its prime factors is 3+7=10which is not the same as the sum of digits of the number. Therefore, 21 is not a smith number.


INPUT: A number
OUTPUT: Whether the number is a smith number or not
Step 1: [Taking the input]
	Read n [The number to be checked]
Step 2: [Checking for smith number]
	Set sd<-0
Set sf<-0
	Set tmp<-n
	[Loop to calculate the sum of the digits]
	While tmp>0 repeat
		Set sd<-sd+(tmp mod 10)
		Set tmp<-tmp/10
	[End of ‘while’ loop]
	[Calculating the sum of the factors]
	Set tmp<-n
	While tmp>1 repeat
		[Finding the prime factor of the number]
		For i=2 to tmp repeat
			If tmp mod i=0 then
			[End of ‘if’]
		[End of ‘for’ loop]
		[Checking if 'i' is a prime number]
		Set c<-0
		Set t<-i
		For j=1 to i repeat
			If i mod j=0 then
				Set c<-c+1
			[End of ‘if’]
		[End of ‘for’ loop]
		If c=2 then
			[Adding the digits of the factor]
			While i>0 repeat
				[Extracting each digit of factor]
				Set sf<-sf+(i mod 10)
				Set i<-i/10
			[End of ‘while’ loop]
		[End of ‘if’]
		Set tmp<-tmp/t
	[End of ‘while’ loop]
	[Checking for smith number]
	If sd=sf then
		Print "The given number is a smith number"
		Print "The given number is not a smith number"
	[End of ‘if-else’]
Step 3: Stop.