Help with pseudocode?



So I had this question in statistics case and I did not solve it. I did not actually understand it because I never read something like this before! I think it’s simple but I just don’t get it!


Hey @TheSaturn09 ! Let’s see if we can figure it out. It’s pretty trivial to implement in Python (don’t know if you’re familiar with that):

cnt = 0
for i in range(0, 100):
    if not (i % 5) and (i % 7):
        cnt += 1
print(cnt)  # results in 17

or a bit shorter (same code):

print(sum([1 for i in range(0, 100) if not (i % 5) and (i % 7)]))

Dont’ know if that is really helpful, but at least it’s for me. The correct answer is 17, but why? Let’s break it down.

  • We create variable cnt which is 0.
  • Then we loop through numbers 0 to 100. The value of the current number is assigned to i.
  • If a certain condition (see below) is valid, increment the counter by one. I added a print afterwards to show the value.

The condition states that i % 5 should not be 0, while i % 7 must be (both have to be true in order to satisfy the entire condition). What do they mean? The % subtracts the number after it (7 or 5) as long as it’s able to without going to zero. So 7 % 3 == 7 - 3 - 3 == 1 and 4 % 2 == 4 - 2 - 2 == 0. It’s often called the remainder. When applied in a clever way, it can be used to determine if a number is even (then do x % 2). In this case, the condition requires the number to be divisible by 7, but not by 5.

I hope it makes sense. Good luck!


I think that it is just the other way around.
i % 5 must be zero, so that counts 20 times
i % 7 must NOT be zero so that counts 18 times (only 35 and 70 are zero) .

So the answer is in my opinion 18 times that both conditions a re true.

Hey @Yeshead , thanks so much for jumping in! My Python answer actually covers the scenario exactly as you’re describing it:

if not (i % 5) and (i % 7)

We’d like to get something that evaluates to True on both the left and right side of the and, right? Keep in mind that in Python, 0 evaluates to False.

i % 5 must be zero

So if we’d like to get True, we’ll have to provide not (i % 5).

i % 7 must NOT be zero

And if we’d like to get False here for those cases, we can just provide (i % 7) since it’ll evaluate to False if it turns out to be 0.

Why do we still get different answers then? That has to do with the interpretation of “1 to 100”. I interpreted this as “1 up to but not including 100” which is exactly what range(100) does in Python. You seem to be reading it as “1 up to and including 100”, which from my point of view seems logical as well :grinning_face_with_smiling_eyes:

If you’d like to go for that, you can easily adapt the code to get your version:

print(sum([1 for i in range(0, 101) if not (i % 5) and (i % 7)]))

which indeeds yields the result of 18 :grinning_face_with_smiling_eyes:

1 Like