I wrote a program that prints all the prime numbers under 25. But surprisingly it prints 27. My question is how does that happen? Also, this problem does not happen for all the numbers, I tested it with all the numbers from 10 to 2. If I try to write a program that prints all the prime numbers under 10, 15, 16, 21, 22, 25 then this problem occurs. It prints one extra prime number that is not in range of the target number. How do I fix it?
Code:
a = 2
b = 2
while (a<25):
for b in range (2, 100000):
if (b == a):
b = b+1
if (a%b != 0):
a = a
b = b+1
else:
a = a+1
b = 1
print (a)
a = a + 1
OK Beautiful first keep in mind that the two line of code
print (a)
a = a + 1
will only be used by the program once the for loop has ended. the problem starts when you reach 24. So now a is equal to 24, you start the for loop, you get the third condition working since 24 % 2 == 0 what happended is that a is now equal to 25 b is equal to one but the loop does not stop despite you setting b to one .Now a is 25, same thing will happen, a will be 26 , the for loop did not end yet( YOU CANNOT END A FOR LOOP BY SIMPLY SETTING B TO ONE). It will only end on 27 because it is a real prime number and that is why you get 27. the solution that i found for this is to add a condition on the two line of code that come after the for loop like this
if a >= 25:
break
the break keyword will immediately tell the program to exist the while loop without running the remaining code
Here my version of your work along with comments
a = 2
# you do not need to declare the b variable here unnecessary
# all the () in the while loop, for loop and if conditions can be removed they have no effect in python
while a < 25:
# using the for loop 100000 times is too much to find prime numbers under 25
# simply because if the second number is larger then the first one it will not divide it
# a max range of 25 will cut down any unnecessary iterations and make the program much faster
for b in range(2, 25):
if b == a:
# adding one to b actually does not tell the for loop to move to the next iteration
# do that by simply using the continue keyword
continue
if a % b != 0:
# same thing here move to the next iteration by using the continue keyword
continue
else:
# a+=1 is the same thing as a = a + 1
a += 1
if a >= 25:
break
print(a)
a = a + 1
If something is not clear feel free to message me if you want, sorry for the long explanation and have a good day
I wrote a program that shows the sum of all prime numbers below 100. It works if I try the same program for 20 or 25 but not for 100. It shows the wrong result. The result should be 1060 but it shows 1304. Can someone please explain why?
a = 2
b = 2
s = 0
while (a<100):
for b in range (2, 100000):
if (b==a):
b = b +1
if (a%b != 0):
a = a
b = b+1
else:
a = a+1
b = 2
if (a<100):
s = s+a
a = a+1
print (s)
# The program is this: print the sum of all the prime numbers before 100 (or 20 or 25, whatever number you wish).
a = 2 #variable one - This is the number you test if it is prime number or not & if it is less than 10 or not, so you the program can decide if to sum it up
b = 2 #Variable two - This is the number you divide a with. A prime number is a number that can be only divided evenly by itself or by 1. So this is the second number.
s = 0 #Variable 3: S stands for sum. if the number is prime & less than 10 then we sum it up in this variable.
while (a<100): #First loop - To check if a number is prime or not, then to check if a number is less than 100 or not, the sum it up & repeat the process
for b in range (2, 100000): #Loop to figure out if the number is prime or not. It is based on the same theory that a prime number can not be divided evenly by any other number but any other number but itself or 1. So I divide a by b, where b is b + 1 everytime it is not evenly divided until b is 100000. Basically I divide a with all the numbers until 100000. If it is not evenly divided it is prime. If not, then a = a +1 and b = 2 again. This way even if a = a non prime number like 8, the next prime number will come out of the loop like 11.
if (b==a): #This is to make sure we get the prime number, cause prime numbers can be evenly divided by itself, so if a == b, add 1 to b.
b = b +1
if (a%b != 0):
a = a
b = b+1
else:
a = a+1
b = 2
if (a<100): #From the for loop only prime number comes out. If a number is not prime for example 8, it is designed in a way that the next prime number 11 will come out of it, and value of a will be 11. Now if the value of a is bigger than 100 then it shouldn't sum it up, and close the loop.
s = s+a
a = a+1
print (s)
That’s the complete breakdown of the program. Hope it helps.
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.