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

Prime Triplets

Print prime triples with offsets zero, two, and six in Python. Students test three related values and keep the largest member within the stated search bound.

AdvancedPhase 6 - Category 3: Loop & Conditional MixExample 10 of 10
prime-triplets.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 prime triplets
11print("Prime triplets (p, p+2, p+6) up to 100:")
12for num in range(2, 95):
13 if is_prime(num) and is_prime(num + 2) and is_prime(num + 6):
14 print(f"({num}, {num + 2}, {num + 6})", end=" ")
15print()

Output

Prime triplets (p, p+2, p+6) up to 100:
(5, 7, 11) (11, 13, 17) (17, 19, 23) (41, 43, 47)

In this one

  • Define a specific triple shape
  • Require all three primes
  • Respect the maximum offset

How it works

  1. 1

    Visit starting values below 95.

  2. 2

    Check num, num + 2, and num + 6.

  3. 3

    Print the triple only when every check passes.

Edge cases

Does (5, 7, 11) qualify?

Does (5, 7, 11) qualify?

Yes. All three are prime.

Does the loop include start 95?

Does the loop include start 95?

No. range stops before 95.

What's going on

The pattern is specifically (p, p + 2, p + 6). The loop ends before 95, so the final possible starting value is 94.

Every member must pass is_prime. For five, the triple is (5, 7, 11). This is one chosen prime-triplet shape, not a listing of every possible three-prime grouping. and requires all tests, and the formatted tuple prints once per accepted start.

Does (5, 7, 11) qualify? Yes. All three are prime.

Does the loop include start 95? No. range stops before 95.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which offsets are checked?

Pick an answer to check it.

Question 2

Does (5, 7, 11) qualify?

Pick an answer to check it.

Questions people ask

Does (5, 7, 11) qualify?

Yes. All three are prime.

Does the loop include start 95?

No. range stops before 95.

Which offsets are checked?

0, 2, 6.