Python Prime Number for-else range
up vote
0
down vote
favorite
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Why does this code print "2" as a prime number? (it is but it should not print it)
2%2==0 so it should skip it...
python python-3.x algorithm primes
add a comment |
up vote
0
down vote
favorite
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Why does this code print "2" as a prime number? (it is but it should not print it)
2%2==0 so it should skip it...
python python-3.x algorithm primes
What happens wheni
is 3?
– Woody1193
9 hours ago
1
for i in range(2,num)
if num is 2 that's empy list so you wont get 2%2==0
– Filip Młynarski
9 hours ago
1
Well.... two is a prime number. It can only be divided by itself and one. However, I do see the error in your code causing the unexpected response:range(start,end)
needs theend
to be greater than thestart
. Python's duck-typing means a check won't be performed and your if statment doesn't get checked. I wish python didn't do that, but unfortunately it does.
– Mark_Anderson
9 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Why does this code print "2" as a prime number? (it is but it should not print it)
2%2==0 so it should skip it...
python python-3.x algorithm primes
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Why does this code print "2" as a prime number? (it is but it should not print it)
2%2==0 so it should skip it...
python python-3.x algorithm primes
python python-3.x algorithm primes
edited 9 hours ago
asked 9 hours ago
Sotiris Liagas
294
294
What happens wheni
is 3?
– Woody1193
9 hours ago
1
for i in range(2,num)
if num is 2 that's empy list so you wont get 2%2==0
– Filip Młynarski
9 hours ago
1
Well.... two is a prime number. It can only be divided by itself and one. However, I do see the error in your code causing the unexpected response:range(start,end)
needs theend
to be greater than thestart
. Python's duck-typing means a check won't be performed and your if statment doesn't get checked. I wish python didn't do that, but unfortunately it does.
– Mark_Anderson
9 hours ago
add a comment |
What happens wheni
is 3?
– Woody1193
9 hours ago
1
for i in range(2,num)
if num is 2 that's empy list so you wont get 2%2==0
– Filip Młynarski
9 hours ago
1
Well.... two is a prime number. It can only be divided by itself and one. However, I do see the error in your code causing the unexpected response:range(start,end)
needs theend
to be greater than thestart
. Python's duck-typing means a check won't be performed and your if statment doesn't get checked. I wish python didn't do that, but unfortunately it does.
– Mark_Anderson
9 hours ago
What happens when
i
is 3?– Woody1193
9 hours ago
What happens when
i
is 3?– Woody1193
9 hours ago
1
1
for i in range(2,num)
if num is 2 that's empy list so you wont get 2%2==0– Filip Młynarski
9 hours ago
for i in range(2,num)
if num is 2 that's empy list so you wont get 2%2==0– Filip Młynarski
9 hours ago
1
1
Well.... two is a prime number. It can only be divided by itself and one. However, I do see the error in your code causing the unexpected response:
range(start,end)
needs the end
to be greater than the start
. Python's duck-typing means a check won't be performed and your if statment doesn't get checked. I wish python didn't do that, but unfortunately it does.– Mark_Anderson
9 hours ago
Well.... two is a prime number. It can only be divided by itself and one. However, I do see the error in your code causing the unexpected response:
range(start,end)
needs the end
to be greater than the start
. Python's duck-typing means a check won't be performed and your if statment doesn't get checked. I wish python didn't do that, but unfortunately it does.– Mark_Anderson
9 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
When num
is 2, range(2, num)
is empty, so the if (num % i) == 0:
check is not performed, and the else
block executes.
add a comment |
up vote
1
down vote
Others have noted the error in the range(start,end)
code. Correcting that, your code for primes could be rewritten as:
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,max(num,3)):
if (num % i) == 0:
break
else:
print(num)
This isn't the fastest way to get primes mind you, each potential prime must be tested against EVERY smaller number as a possible divisor. It's much faster to instead count upwards and work out the multiples of smaller numbers. That way we only need to do the maths once for each possible divisor.
For completeness, here is a program that can produce primes efficiently (uses the sieve of Eratosthenes method).
#### INPUTS
lower = int(input("from:"))
upper = int(input("to:"))
### Code
n = upper
prime_booleans = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# Is current number a prime, eliminate the numbers that are multiples of it
if (prime_booleans[p] == True):
for i in range(p * 2, n+1, p):
prime_booleans[i] = False
p += 1
# Print all prime numbers
for p in range(lower, n):
if prime_booleans[p]:
print p,
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
When num
is 2, range(2, num)
is empty, so the if (num % i) == 0:
check is not performed, and the else
block executes.
add a comment |
up vote
2
down vote
When num
is 2, range(2, num)
is empty, so the if (num % i) == 0:
check is not performed, and the else
block executes.
add a comment |
up vote
2
down vote
up vote
2
down vote
When num
is 2, range(2, num)
is empty, so the if (num % i) == 0:
check is not performed, and the else
block executes.
When num
is 2, range(2, num)
is empty, so the if (num % i) == 0:
check is not performed, and the else
block executes.
answered 9 hours ago
Patrick Haugh
25.8k82544
25.8k82544
add a comment |
add a comment |
up vote
1
down vote
Others have noted the error in the range(start,end)
code. Correcting that, your code for primes could be rewritten as:
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,max(num,3)):
if (num % i) == 0:
break
else:
print(num)
This isn't the fastest way to get primes mind you, each potential prime must be tested against EVERY smaller number as a possible divisor. It's much faster to instead count upwards and work out the multiples of smaller numbers. That way we only need to do the maths once for each possible divisor.
For completeness, here is a program that can produce primes efficiently (uses the sieve of Eratosthenes method).
#### INPUTS
lower = int(input("from:"))
upper = int(input("to:"))
### Code
n = upper
prime_booleans = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# Is current number a prime, eliminate the numbers that are multiples of it
if (prime_booleans[p] == True):
for i in range(p * 2, n+1, p):
prime_booleans[i] = False
p += 1
# Print all prime numbers
for p in range(lower, n):
if prime_booleans[p]:
print p,
add a comment |
up vote
1
down vote
Others have noted the error in the range(start,end)
code. Correcting that, your code for primes could be rewritten as:
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,max(num,3)):
if (num % i) == 0:
break
else:
print(num)
This isn't the fastest way to get primes mind you, each potential prime must be tested against EVERY smaller number as a possible divisor. It's much faster to instead count upwards and work out the multiples of smaller numbers. That way we only need to do the maths once for each possible divisor.
For completeness, here is a program that can produce primes efficiently (uses the sieve of Eratosthenes method).
#### INPUTS
lower = int(input("from:"))
upper = int(input("to:"))
### Code
n = upper
prime_booleans = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# Is current number a prime, eliminate the numbers that are multiples of it
if (prime_booleans[p] == True):
for i in range(p * 2, n+1, p):
prime_booleans[i] = False
p += 1
# Print all prime numbers
for p in range(lower, n):
if prime_booleans[p]:
print p,
add a comment |
up vote
1
down vote
up vote
1
down vote
Others have noted the error in the range(start,end)
code. Correcting that, your code for primes could be rewritten as:
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,max(num,3)):
if (num % i) == 0:
break
else:
print(num)
This isn't the fastest way to get primes mind you, each potential prime must be tested against EVERY smaller number as a possible divisor. It's much faster to instead count upwards and work out the multiples of smaller numbers. That way we only need to do the maths once for each possible divisor.
For completeness, here is a program that can produce primes efficiently (uses the sieve of Eratosthenes method).
#### INPUTS
lower = int(input("from:"))
upper = int(input("to:"))
### Code
n = upper
prime_booleans = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# Is current number a prime, eliminate the numbers that are multiples of it
if (prime_booleans[p] == True):
for i in range(p * 2, n+1, p):
prime_booleans[i] = False
p += 1
# Print all prime numbers
for p in range(lower, n):
if prime_booleans[p]:
print p,
Others have noted the error in the range(start,end)
code. Correcting that, your code for primes could be rewritten as:
lower = int(input("from:"))
upper = int(input("to:"))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,max(num,3)):
if (num % i) == 0:
break
else:
print(num)
This isn't the fastest way to get primes mind you, each potential prime must be tested against EVERY smaller number as a possible divisor. It's much faster to instead count upwards and work out the multiples of smaller numbers. That way we only need to do the maths once for each possible divisor.
For completeness, here is a program that can produce primes efficiently (uses the sieve of Eratosthenes method).
#### INPUTS
lower = int(input("from:"))
upper = int(input("to:"))
### Code
n = upper
prime_booleans = [True for i in range(n+1)]
p = 2
while (p * p <= n):
# Is current number a prime, eliminate the numbers that are multiples of it
if (prime_booleans[p] == True):
for i in range(p * 2, n+1, p):
prime_booleans[i] = False
p += 1
# Print all prime numbers
for p in range(lower, n):
if prime_booleans[p]:
print p,
answered 9 hours ago
Mark_Anderson
421215
421215
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400108%2fpython-prime-number-for-else-range%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What happens when
i
is 3?– Woody1193
9 hours ago
1
for i in range(2,num)
if num is 2 that's empy list so you wont get 2%2==0– Filip Młynarski
9 hours ago
1
Well.... two is a prime number. It can only be divided by itself and one. However, I do see the error in your code causing the unexpected response:
range(start,end)
needs theend
to be greater than thestart
. Python's duck-typing means a check won't be performed and your if statment doesn't get checked. I wish python didn't do that, but unfortunately it does.– Mark_Anderson
9 hours ago