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

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.

AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 2 of 12
count-numbers-divisible-by-7-but-not-5.py
1# Count numbers
2count = 0
3for num in range(1, 501):
4 if num % 7 == 0 and num % 5 != 0:
5 count += 1
6
7print(f"Count: {count}")

Output

Count: 57

In this one

  • Combine inclusion and exclusion
  • Count matching candidates
  • Distinguish count from sum

How it works

  1. 1

    Visit one through five hundred.

  2. 2

    Require divisibility by seven and nondivisibility by five.

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