Count Numbers Divisible by 7 but Not 5
Count integers up to five hundred divisible by seven but not five. Students use and with a negated divisibility test and count matches rather than sum them.
1# Count numbers2count = 03for num in range(1, 501):4 if num % 7 == 0 and num % 5 != 0:5 count += 167print(f"Count: {count}")
Output
Count: 57
In this one
- Combine inclusion and exclusion
- Count matching candidates
- Distinguish count from sum
How it works
- 1
Visit one through five hundred.
- 2
Require divisibility by seven and nondivisibility by five.
- 3
Increment count per match and print the total.
Edge cases
Is 35 counted?
Is 35 counted?
No. It is also divisible by five.
Is 7 counted?
Is 7 counted?
Yes. It satisfies both conditions.
What's going on
The candidate loop includes one through five hundred. A match needs remainder zero for seven and a nonzero remainder for five.
Thirty-five is excluded even though it is a multiple of seven. The counter increases by one per accepted candidate. There are seventy-one multiples of seven in the range and fourteen shared multiples of thirty-five, leaving fifty-seven matches.
Is 35 counted? No. It is also divisible by five.
Is 7 counted? Yes. It satisfies both conditions.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is the final count?
Pick an answer to check it.
Question 2
Is 35 counted?
Pick an answer to check it.
Questions people ask
Is 35 counted?
No. It is also divisible by five.
Is 7 counted?
Yes. It satisfies both conditions.
What is the final count?
57.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum