简体   繁体   中英

Print the numbers from 1-100 skipping the numbers divisible by 3 & 5

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?

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 (eg 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)

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

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]

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)

The first program is incorrect because you on line 6-7 you increase the counter without checking to see if you need to print the number.

A cleaner way to write this could would be:

for counter in xrange(1, 101):
    if not ((counter % 5 == 0) or (counter % 3 == 0)):
        print (a)
n=1

while 1<=n<=100:

    if((n%3 and n%5)==0) :
        False

    else:
        print(n)      
    n=n+1

In the first code you are using an and which is almost opposite to what you need, an or statement. I would advise you switch the and for an or statement.

#CODE
i=1
a=1
while i<=100:
    if (a%3==0 or a%5==0) :
           a=a+1
    else:
        print(a)
        a=a+1
    i=i+1

In the second code, you used a nand which is quite similar to an or in this situation and so works for the first parts of the data set. However in more complex code, this would give you a false negative to some checks and therefore I feel it should be avoided if not strictly needed.

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?

Do it like this.

for i in range(1, 101):
    if i % 3 != 0 and i % 5 != 0:
        print(i)
for i in range(1, 101):

   if i%3==0 and i%5==0:

    continue

   print(i)

Try this:

r = range(1,101,1)
for i in r:
      if i%3!=0 and i%5!=0:
            print(i)
i=1

while(i<=100):
if(i%3!=0 and i%5!=0):
    print(i)
i=i+1

Try this one. It's simple clean and easy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM