Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

AdvancedPhase 6 - Category 1: Number LogicExample 6 of 10
numbers-with-prime-digit-sum.py
1# Helper function
2def is_prime(n):
3 if n < 2:
4 return False
5 for i in range(2, int(n ** 0.5) + 1):
6 if n % i == 0:
7 return False
8 return True
9
10# Find numbers
11print("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. 1

    For each candidate, sum converted decimal characters.

  2. 2

    Pass that sum to is_prime.

  3. 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.