Checking for Smith Number in Python using Functions
In this Python tutorial, we will learn about Smith Numbers and how to generate them in Python.
What are Smith Numbers?
Smith numbers have the following features:-
- They are composite numbers
- Sum of its digits is equal to the summation of the sum of the digits of its prime factors.
Let’s look at a few examples of Smith Numbers to understand the same:-
- 85. The sum of its digits is 8+5 = 13. Its prime factors are 5 and 17. Sum of the digits of prime factors are 5 + (1+7)= 13. Hence 85 is a Smith Number.
- 27. The sum of its digits is 2+7=9. Its prime factors are 3,3 and 3 Sum of the digits of prime factors are 3 + 3 + 3= 9. Hence 27 is a Smith Number
Let us now look at the Code in Python…
Code and Output
Code in Python:-
#Function to calculate the number of digits in a number def digCount(num): c = 0 while num != 0: num = num//10 c += 1 return c #Function to calculate the sum of digits of a number def digSum(num): temp = num sum = 0 for i in range(digCount(num)): sum+=num%10 num//=10 return sum #Function to check whether a number is prime or not def isPrime(num): for i in range(2,num): if (num % i) == 0: return False else: continue return True #Function to check whether a number is a Smith Number or not def isSmith(num): if(isPrime(num)): print("This is a prime number and only composite numbers can be Smith numbers") else: prime_factors = [] temp = num c = 2 while(temp>1): if(temp%c == 0 and isPrime(c)): prime_factors.append(c) temp/=c else: c+=1 continue for i in range(0,len(prime_factors)): if(digCount(prime_factors[i])>1): while(digCount(prime_factors[i])>1): prime_factors[i] = digSum(prime_factors[i]) if(sum(prime_factors) == digSum(num)): return True else: return False #Checking a list of numbers whether they are Smith Numbers or not list = [25,27,83,85,90,94,120,121,200,202] for num in list: if(isSmith(num)): print(f'{num} is a Smith Number') else: print(f'{num} is not a Smith Number')
Output:-
25 is not a Smith Number 27 is a Smith Number This is a prime number and only composite numbers can be Smith numbers 83 is not a Smith Number 85 is a Smith Number 90 is not a Smith Number 94 is not a Smith Number 120 is not a Smith Number 121 is a Smith Number 200 is not a Smith Number 202 is a Smith Number
Please try and understand the code using pen and paper before moving on to the explanation of the Python code below!
Explanation of the code
Let us look at what the functions are supposed to do in this code:-
- digCount() function: Counts the number of digits of the argument passed to it and returns it.
- digSum() function: Returns the sum of digits of the argument passed to it.
- isPrime() function: Checks, whether the argument passed, is a prime number or not.
We iterate over a list of numbers and pass them on to the isSmith() function.
The function firsts checks whether the argument passed is a prime number or not. If the argument is a prime number then we print “This is a prime number and only composite numbers can be Smith numbers”.
If the argument is not a prime number, then we find out the prime factors of the argument passed and append it to the list prime_factors.
After having the prime factors of the argument passed, we iterate over the list prime_factors and calculate the sum of digits of every prime factor, replacing them with the sum of their respective digits.
Then we consider the sum of all the elements of the prime_factors list (which now contains the sum of digits of prime factors) and check whether it is equal to the sum of digits of the argument passed. If those two are equal, then the argument passed is a Smith Number and the function returns True. Otherwise, the function returns False because the argument is not a Smith Number.
I hope this Python tutorial was useful for you!
Leave a Reply