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.
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 prime triplets11print("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
Visit starting values below 95.
- 2
Check num, num + 2, and num + 6.
- 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.
๐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