+ 1

What is the error in this code? please answer urgently

print('All primes between 10 and 30 are:') s = 0 for i in range(10, 31): for j in range(2, i): if (i%j != 0): print(i) s += i break else: print("checked by all") else: print("and their sum is:",s)

8th Jan 2021, 4:36 PM
Science Buster
Science Buster - avatar
7 RĂ©ponses
+ 8
Sorry, during editing my post the tablet seems to crash and the post was gone. I mentioned that the code you presented is a kind of “hard coded”. This means that you are using a fixed set of divisors = 2, 3, 5, 7 in the modulo division. This may give correct results in lower numbers, but will lead to problems with higher numbers to check if they are prime or not. I just used your code and modified it by using a different range of numbers to start and end. When using: . for i in range(700,800): your code listed some numbers as prime, but they are not! The numbers are 767, 779, 781, 793, 799 (this list is not complete, it’s just to mention some problems.) 767 / 59 = 13 779 / 41 = 19 781 / 71 = 11 793 / 61 = 13 799 / 47 = 17 So all these numbers are NO primes. The logic to check for numbers if they are primes or not is always the same, it does not matter what range you want to check. It can be optimized for performance reasons, but the hard coded divisors is a no-go. Just imagine that users want to check any number or any range with the same code. Here is a very simple code that should work; start = int(input()) end = int(input()) print("-"*20) for i in range(start, end): for j in range(2, i//2): if i % j == 0: break else: print(i)
8th Jan 2021, 7:10 PM
Lothar
Lothar - avatar
+ 4
It is because of the condition on line 5 "if(i % j != 0)" This will always become True because for example: ---Loop 2--- # i = 10 and j = 3 if(10%3 != 0) # <--- True print(i) # <--- Prints 10 (not prime) break # <--- Loop breaks 10 is recognised as Prime just because one number "j" is not a factor of 10 or "i" - - - - - - - - - - - - - - - - - - Note that "i" is a number from 10 to 30 and "j" is a number from 2 to "i" so it means if a one number "j" is not a factor of "i", the number will be recognised as Prime eventhough it is not. If you have more questions or my explanation is not clear, feel free to ask. Thanks!! https://code.sololearn.com/csTO9xu31O72/?ref=app
8th Jan 2021, 4:45 PM
noteve
noteve - avatar
+ 4
Ratnapal Shende And by the way, you can use list comprehension to find prime numbers in a given range🙂 *(edited/correction) https://code.sololearn.com/cpnsPfIL9OL0/?ref=app
8th Jan 2021, 5:14 PM
noteve
noteve - avatar
+ 3
8th Jan 2021, 4:53 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 3
what is mean "hard-coded"? đŸ€” Lothar sir is it not good with logic ?? actually I tried to do with less lines 😉 using list comprehension I am troubling... lst=[ i for i in range(10,31) for j in range(2,i) if i%j!=0] print(lst) how to solve it using list comprehension??
8th Jan 2021, 5:04 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 3
《 Nicko12 》 maybe I am wrong but it's nothing like that we can find prime numbers of any range using the same logic... for i in range(50,100): if i%2!=0 and i%3!=0 and i%5!=0 and i%7!=0: print(i) #output : 53 59 61 67 71 73 79 83 89 97
8th Jan 2021, 5:21 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 2
Lothar got it sir Thank you so much for the correction! 😘
9th Jan 2021, 4:48 AM
Ratnapal Shende
Ratnapal Shende - avatar