Apr-20-2020, 06:59 PM
Hi Guys,
Let me begin by confesing I am new to coding.
I understand my problem might seem basic to some but I am also sure you're eager to help one learn.
Here we go. I came up with a problem, I wanted to write a program that will return all prime numbers from a range set by user. I end up not being able to write it and found below code online. It works fine but it also puzzles me why. Especially this line - if (number % i) == 0:
This will break if a number in range "i" is divisible by any number in range "number".
This makes sense to me except for the fact that eventually number will be divided by itself which would also exclude all prime numbers. I wanted to write a line that would run modulo between "I" and "number" excluding I from number, but it doesn't seem necessary.
Help, can you please unconfused me? Why does it work?
print ("all primer numbers in a selected range")
a = int(input("enter bottom value of the range: "))
b = int(input("enter top value of the range: "))
for number in range(a, b +1):
if number > 1:
for i in range(2, number):
if (number % i) == 0:
break
else:
print(number)
Let me begin by confesing I am new to coding.
I understand my problem might seem basic to some but I am also sure you're eager to help one learn.
Here we go. I came up with a problem, I wanted to write a program that will return all prime numbers from a range set by user. I end up not being able to write it and found below code online. It works fine but it also puzzles me why. Especially this line - if (number % i) == 0:
This will break if a number in range "i" is divisible by any number in range "number".
This makes sense to me except for the fact that eventually number will be divided by itself which would also exclude all prime numbers. I wanted to write a line that would run modulo between "I" and "number" excluding I from number, but it doesn't seem necessary.
Help, can you please unconfused me? Why does it work?
print ("all primer numbers in a selected range")
a = int(input("enter bottom value of the range: "))
b = int(input("enter top value of the range: "))
for number in range(a, b +1):
if number > 1:
for i in range(2, number):
if (number % i) == 0:
break
else:
print(number)