Numbers with Prime Digit Sum
Select integers whose decimal digit sum is prime using Python. Students separate the candidate's value from its derived sum and reuse a prime-checking helper.
1# Helper function2def is_prime(n):3 if n < 2:4 return False5 for i in range(2, int(n ** 0.5) + 1):6 if n % i == 0:7 return False8 return True910# Find numbers11print("Numbers 1-100 with prime digit sum:")12for num in range(1, 101):13 digit_sum = sum(int(d) for d in str(num))14 if is_prime(digit_sum):15 print(num, end=" ")16print()
Output
...
The sample abbreviates a longer result. The program prints the intervening values; it does not print these dots.
In this one
- Derive a digit sum
- Classify the derived value
- Separate properties of related numbers
How it works
- 1
For each candidate, sum converted decimal characters.
- 2
Pass that sum to is_prime.
- 3
Print the candidate when the sum is prime.
Edge cases
Does 12 qualify?
Does 12 qualify?
Yes. Its digit sum three is prime.
Does 100 qualify?
Does 100 qualify?
No. Its digit sum one is not prime.
What's going on
The generator expression converts each decimal character to an integer and sums those digits. is_prime then classifies that sum, not num itself.
For 12, the digit sum is three, so 12 qualifies despite being composite. A sum of one fails because one is not prime. The loop stays within one through one hundred, making all scanned decimal characters nonnegative digit text.
Does 12 qualify? Yes. Its digit sum three is prime.
Does 100 qualify? No. Its digit sum one is not prime.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which value is checked for primality?
Pick an answer to check it.
Question 2
Does 12 qualify?
Pick an answer to check it.
Questions people ask
Does 12 qualify?
Yes. Its digit sum three is prime.
Does 100 qualify?
No. Its digit sum one is not prime.
Which value is checked for primality?
digit_sum.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum