Python Topics
- Python Intro
- Python String Methods
- Python Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Perfect Number
- Perfect Square
- Power of 2
- Power of 3
- Pronic Number
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- Python Star Pattern
- Python Number Pattern
- Python Alphabet Pattern
Python Program to Check Smith Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring number properties is a common and fascinating endeavor. One interesting type of number is the Smith Number.
A Smith Number is a composite number for which the sum of its digits is equal to the sum of the digits in its prime factorization.
In this tutorial, we'll delve into a Python program that checks whether a given number is a Smith Number.
π Example
Let's take a look at the Python code that checks whether a given number is a Smith Number.
def sum_of_digits(num):
return sum(int(digit) for digit in str(num))
def sum_of_prime_factors(num):
i, total = 2, 0
while num > 1:
while num % i == 0:
total += sum_of_digits(i)
num //= i
i += 1
return total
def is_smith_number(num):
return sum_of_digits(num) == sum_of_prime_factors(num)
# Replace this value with your desired number
number = 85
# Check if the number is a Smith Number
if is_smith_number(number):
print(f"{number} is a Smith Number.")
else:
print(f"{number} is not a Smith Number.")
π» Testing the Program
To test the program with different numbers, replace the value of number in the code.
85 is a Smith Number.
Run the script to check whether the given number is a Smith Number.
π§ How the Program Works
- The program defines three functions: sum_of_digits to calculate the sum of digits in a number, sum_of_prime_factors to calculate the sum of digits in the prime factorization, and is_smith_number to check if a number is a Smith Number.
- The program replaces the value of number with the desired number and checks if it is a Smith Number.
- The program utilizes loops and arithmetic operations to calculate the sum of digits and the sum of digits in the prime factorization.
π Between the Given Range
Let's take a look at the python code that checks for Smith Numbers in the specified range.
# Function to calculate the sum of digits
def sum_of_digits(n):
sum_val = 0
while n > 0:
sum_val += n % 10
n //= 10
return sum_val
# Function to calculate the sum of prime factors
def sum_of_prime_factors(n):
sum_val = 0
for i in range(2, n + 1):
while n % i == 0:
sum_val += sum_of_digits(i)
n //= i
return sum_val
# Function to check if a number is prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Function to check if a number is a Smith Number
def is_smith_number(n):
return False if is_prime(n) else sum_of_digits(n) == sum_of_prime_factors(n)
# Main program
print("Smith Numbers in the Range 1 to 100:")
for i in range(1, 101):
if is_smith_number(i):
print(i, end=" ")
print()
π» Testing the Program
Smith Numbers in the Range 1 to 100: 4 22 27 58 85 94
Run the program to see the Smith Numbers in the specified range.
π§ How the Program Works
- The program defines a function isPrime to check if a number is prime.
- The isSmithNumber function checks if a number is a Smith Number by comparing the sum of its digits with the sum of the digits in its prime factorization.
- The main function checks and prints Smith Numbers in the range from 1 to 100.
π§ Understanding the Concept of Smith Numbers
Before delving into the code, it's crucial to understand the concept of Smith Numbers. These numbers exhibit a unique property where the sum of their digits is equal to the sum of the digits in their prime factorization.
For example, 4, 22, 27, and 85 are Smith Numbers.
π’ Optimizing the Program
While the provided program is effective, consider exploring optimizations for larger numbers. You can further refine the code for improved efficiency or adapt it to your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Smith Number), please comment here. I will help you immediately.