Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether the sum of absolute difference of adjacent digits is Prime or not in Python
Sometimes we need to check if the sum of absolute differences between adjacent digits in a number results in a prime number. This involves calculating differences between consecutive digits and testing if their sum is prime.
Problem Understanding
Given a number n, we need to ?
- Calculate the absolute difference between each pair of adjacent digits
- Sum all these differences
- Check if the sum is a prime number
For example, if n = 574, then |5−7| + |7−4| = 2 + 3 = 5, which is prime, so the result is True.
Algorithm Steps
To solve this problem, we follow these steps ?
- Convert the number to a string to easily access individual digits
- Initialize a total sum to 0
- Iterate through adjacent digit pairs and calculate absolute differences
- Sum all the differences
- Check if the sum is prime and return the result
Implementation
Prime Number Checker
First, we need a helper function to check if a number is prime ?
def isPrime(num):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
return False
# Test the prime checker
print(isPrime(5)) # Should be True
print(isPrime(4)) # Should be False
True False
Complete Solution
Now let's implement the main solution ?
def isPrime(num):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
return False
def solve(n):
num_str = str(n)
total = 0
# Calculate sum of absolute differences
for i in range(1, len(num_str)):
diff = abs(int(num_str[i - 1]) - int(num_str[i]))
total += diff
print(f"|{num_str[i-1]}-{num_str[i]}| = {diff}")
print(f"Total sum: {total}")
if isPrime(total):
return True
return False
# Test with the given example
n = 574
result = solve(n)
print(f"Result: {result}")
|5-7| = 2 |7-4| = 3 Total sum: 5 Result: True
Testing with Multiple Examples
Let's test the solution with different numbers ?
def isPrime(num):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
return False
def solve(n):
num_str = str(n)
total = 0
for i in range(1, len(num_str)):
total += abs(int(num_str[i - 1]) - int(num_str[i]))
return isPrime(total)
# Test multiple cases
test_numbers = [574, 123, 999, 135]
for num in test_numbers:
result = solve(num)
print(f"Number {num}: {result}")
Number 574: True Number 123: False Number 999: False Number 135: True
Key Points
- Convert the number to string for easy digit access
- Use
abs()function to calculate absolute differences - Optimize prime checking by only checking divisors up to ?n
- Handle edge cases where the sum might be 0 or 1 (not prime)
Time Complexity
The time complexity is O(d + ?s), where d is the number of digits and s is the sum of differences. The space complexity is O(1) excluding the string conversion.
Conclusion
This solution efficiently calculates the sum of absolute differences between adjacent digits and checks if the result is prime. The optimized prime checking function ensures good performance even for larger sums.
