Print the numbers from 1-100 skipping the numbers divisible by 3 & 5
Solution 1
Consider this:
a = 10
(a%3 == 0) and (a%5 == 0) # False
(a%3 and a%5) == 0 # True
The first attempt gives False
incorrectly because it needs both conditions to be satisfied; you need or
instead. If you look carefully, some numbers (e.g. 15
) are excluded, coinciding with numbers which have both 3
and 5
as factors.
The second attempt is correct because if a
is not divisible by either 3 or 5, the expression evaluates to False
, and 0 == False
gives True
. More idiomatic would be to write:
not (a%3 and a%5)
Solution 2
a much cleaner answer at the level that he is looking at
a = 1
while a <= 100:
if a%3 == 0 or a%5 ==0:
a = a+1
else:
print(a)
a = a+1
Solution 3
The probelm is that 'and' should be changed to logic 'or' in Line5 of CODE#1. Current version 1 skips the numbers only when both conditions are met. You want to skip the numbers when either one or both of the conditions are met.
PS: I would like to suggest a faster and more efficient way of getting this result.
import numpy as np
numbers = np.arange(1,101,1)
print('Original numbers \n', numbers)
print('Required numbers \n', numbers[(numbers%3!=0) & (numbers%5!=0)])
The answer from this will be:
Original numbers
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100]
Required numbers
[ 1 2 4 7 8 11 13 14 16 17 19 22 23 26 28 29 31 32 34 37 38 41 43 44
46 47 49 52 53 56 58 59 61 62 64 67 68 71 73 74 76 77 79 82 83 86 88 89
91 92 94 97 98]
Solution 4
check this also it's working! 100%
def result(N):
for num in range(N):
if num % 3 == 0 and num % 5 == 0:
print(str(num) + " ", end="")
else:
pass
if __name__ == "__main__":
N = 100
result(N)
Related videos on Youtube
Comments
-
xskxzr over 2 years
I want to print numbers from 1-100 skipping the numbers divisible by 3 & 5 and when I use the code-1 I'm not getting the correct output, I am getting full counting 1-100
#CODE1 i=1 a=1 while i<=100: if (a%3==0 and a%5==0) : a=a+1 else: print(a) a=a+1 i=i+1
but when I use the CODE-2 I am getting the desired result
#CODE2 i=1 a=1 while i<=100: if ((a%3 and a%5)==0) : a=a+1 else: print(a) a=a+1 i=i+1
notice the fourth line of the code, why is wrong with the 1st code?
-
Bernhard Barker over 5 yearsIf the first program "increase[s] the counter without checking to see if you need to print the number", the second program also does (the only difference between the two is how they check), yet that works.
-
j_4321 over 4 yearsThe OP does not want a code that works but wants to know why the first code snippet does not work while the second does, which is already done in other answers. I don't see how your post is answering the question. Also it is always better to add a few explanations with the code so that your answer can be helpful to as many people as possible.
-
help-info.de over 3 yearsMay I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers.