how to use for loop to calculate a prime number
up vote
0
down vote
favorite
for i in range(2, 101):
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
I tried making the if (i%j) != 0: but then it wouldn't work (4 is not a prime number)
python loops sieve
add a comment |
up vote
0
down vote
favorite
for i in range(2, 101):
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
I tried making the if (i%j) != 0: but then it wouldn't work (4 is not a prime number)
python loops sieve
1
I'm not sure what you're asking here. That program appears to correctly (albeit not efficiently) differentiate between prime and composite numbers.
– Matt
Nov 11 at 5:58
well when I addif (i%j) != 0:it dosen't work
– Haoyang Song
Nov 11 at 6:06
1
if (i%j) != 0:tests for divisibility of i by j, specifically that i is not divisible by j. If i were divisible by j (for some 2 <= j < i), it couldn't be prime. There are 520 existing questions on [python] divisibility; this is a duplicate and should be closed; please read through the others.
– smci
Nov 11 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 at 17:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
for i in range(2, 101):
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
I tried making the if (i%j) != 0: but then it wouldn't work (4 is not a prime number)
python loops sieve
for i in range(2, 101):
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
I tried making the if (i%j) != 0: but then it wouldn't work (4 is not a prime number)
python loops sieve
python loops sieve
edited Nov 11 at 6:33
smci
14.4k672104
14.4k672104
asked Nov 11 at 5:56
Haoyang Song
10210
10210
1
I'm not sure what you're asking here. That program appears to correctly (albeit not efficiently) differentiate between prime and composite numbers.
– Matt
Nov 11 at 5:58
well when I addif (i%j) != 0:it dosen't work
– Haoyang Song
Nov 11 at 6:06
1
if (i%j) != 0:tests for divisibility of i by j, specifically that i is not divisible by j. If i were divisible by j (for some 2 <= j < i), it couldn't be prime. There are 520 existing questions on [python] divisibility; this is a duplicate and should be closed; please read through the others.
– smci
Nov 11 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 at 17:41
add a comment |
1
I'm not sure what you're asking here. That program appears to correctly (albeit not efficiently) differentiate between prime and composite numbers.
– Matt
Nov 11 at 5:58
well when I addif (i%j) != 0:it dosen't work
– Haoyang Song
Nov 11 at 6:06
1
if (i%j) != 0:tests for divisibility of i by j, specifically that i is not divisible by j. If i were divisible by j (for some 2 <= j < i), it couldn't be prime. There are 520 existing questions on [python] divisibility; this is a duplicate and should be closed; please read through the others.
– smci
Nov 11 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 at 17:41
1
1
I'm not sure what you're asking here. That program appears to correctly (albeit not efficiently) differentiate between prime and composite numbers.
– Matt
Nov 11 at 5:58
I'm not sure what you're asking here. That program appears to correctly (albeit not efficiently) differentiate between prime and composite numbers.
– Matt
Nov 11 at 5:58
well when I add
if (i%j) != 0: it dosen't work– Haoyang Song
Nov 11 at 6:06
well when I add
if (i%j) != 0: it dosen't work– Haoyang Song
Nov 11 at 6:06
1
1
if (i%j) != 0: tests for divisibility of i by j, specifically that i is not divisible by j. If i were divisible by j (for some 2 <= j < i), it couldn't be prime. There are 520 existing questions on [python] divisibility; this is a duplicate and should be closed; please read through the others.– smci
Nov 11 at 13:30
if (i%j) != 0: tests for divisibility of i by j, specifically that i is not divisible by j. If i were divisible by j (for some 2 <= j < i), it couldn't be prime. There are 520 existing questions on [python] divisibility; this is a duplicate and should be closed; please read through the others.– smci
Nov 11 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 at 17:41
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 at 17:41
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
The for loop you've used is correct for finding prime numbers. I would just another condition to it: if i > 1:. Also, you would want to print the prime number
for i in range(2, 101):
if i > 1: # Prime numbers are greater than 1
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
else:
print(i,"is a prime number")
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
1
if the range start at 2 thei > 1check becomes unnecessary though
– Anton vBR
Nov 11 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
The extrai > 1test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.
– smci
Nov 11 at 7:22
add a comment |
up vote
-2
down vote
You can fix up your original algorithm like this:
for i in range(2, 101):
if all([(i % j) for j in range(2, i)]):
print(i,"is a prime number")
In general, you're probably better off using/learning from the established algorithms in cases like these. Here's a Python implementation of a well-known algorithm (the Sieve of Eratosthenes) for generating the first n primes (credit to tech.io for the code):
def sieve(n):
primes = 2*[False] + (n-1)*[True]
for i in range(2, int(n**0.5+1.5)):
for j in range(i*i, n+1, i):
primes[j] = False
return [prime for prime, checked in enumerate(primes) if checked]
Some test output:
print(sieve(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
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
accepted
The for loop you've used is correct for finding prime numbers. I would just another condition to it: if i > 1:. Also, you would want to print the prime number
for i in range(2, 101):
if i > 1: # Prime numbers are greater than 1
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
else:
print(i,"is a prime number")
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
1
if the range start at 2 thei > 1check becomes unnecessary though
– Anton vBR
Nov 11 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
The extrai > 1test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.
– smci
Nov 11 at 7:22
add a comment |
up vote
2
down vote
accepted
The for loop you've used is correct for finding prime numbers. I would just another condition to it: if i > 1:. Also, you would want to print the prime number
for i in range(2, 101):
if i > 1: # Prime numbers are greater than 1
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
else:
print(i,"is a prime number")
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
1
if the range start at 2 thei > 1check becomes unnecessary though
– Anton vBR
Nov 11 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
The extrai > 1test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.
– smci
Nov 11 at 7:22
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The for loop you've used is correct for finding prime numbers. I would just another condition to it: if i > 1:. Also, you would want to print the prime number
for i in range(2, 101):
if i > 1: # Prime numbers are greater than 1
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
else:
print(i,"is a prime number")
The for loop you've used is correct for finding prime numbers. I would just another condition to it: if i > 1:. Also, you would want to print the prime number
for i in range(2, 101):
if i > 1: # Prime numbers are greater than 1
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
else:
print(i,"is a prime number")
answered Nov 11 at 6:04
Mayank Porwal
3,1651620
3,1651620
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
1
if the range start at 2 thei > 1check becomes unnecessary though
– Anton vBR
Nov 11 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
The extrai > 1test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.
– smci
Nov 11 at 7:22
add a comment |
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
1
if the range start at 2 thei > 1check becomes unnecessary though
– Anton vBR
Nov 11 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
The extrai > 1test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.
– smci
Nov 11 at 7:22
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 at 6:16
1
1
if the range start at 2 the
i > 1 check becomes unnecessary though– Anton vBR
Nov 11 at 6:27
if the range start at 2 the
i > 1 check becomes unnecessary though– Anton vBR
Nov 11 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
Yes, in this case it does.
– Mayank Porwal
Nov 11 at 6:35
The extra
i > 1 test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.– smci
Nov 11 at 7:22
The extra
i > 1 test is unnecessary and just obfuscates. You always run a sieve starting at 2 or higher, never 1. This answer adds nothing. Also, the question is a duplicate.– smci
Nov 11 at 7:22
add a comment |
up vote
-2
down vote
You can fix up your original algorithm like this:
for i in range(2, 101):
if all([(i % j) for j in range(2, i)]):
print(i,"is a prime number")
In general, you're probably better off using/learning from the established algorithms in cases like these. Here's a Python implementation of a well-known algorithm (the Sieve of Eratosthenes) for generating the first n primes (credit to tech.io for the code):
def sieve(n):
primes = 2*[False] + (n-1)*[True]
for i in range(2, int(n**0.5+1.5)):
for j in range(i*i, n+1, i):
primes[j] = False
return [prime for prime, checked in enumerate(primes) if checked]
Some test output:
print(sieve(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
add a comment |
up vote
-2
down vote
You can fix up your original algorithm like this:
for i in range(2, 101):
if all([(i % j) for j in range(2, i)]):
print(i,"is a prime number")
In general, you're probably better off using/learning from the established algorithms in cases like these. Here's a Python implementation of a well-known algorithm (the Sieve of Eratosthenes) for generating the first n primes (credit to tech.io for the code):
def sieve(n):
primes = 2*[False] + (n-1)*[True]
for i in range(2, int(n**0.5+1.5)):
for j in range(i*i, n+1, i):
primes[j] = False
return [prime for prime, checked in enumerate(primes) if checked]
Some test output:
print(sieve(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
add a comment |
up vote
-2
down vote
up vote
-2
down vote
You can fix up your original algorithm like this:
for i in range(2, 101):
if all([(i % j) for j in range(2, i)]):
print(i,"is a prime number")
In general, you're probably better off using/learning from the established algorithms in cases like these. Here's a Python implementation of a well-known algorithm (the Sieve of Eratosthenes) for generating the first n primes (credit to tech.io for the code):
def sieve(n):
primes = 2*[False] + (n-1)*[True]
for i in range(2, int(n**0.5+1.5)):
for j in range(i*i, n+1, i):
primes[j] = False
return [prime for prime, checked in enumerate(primes) if checked]
Some test output:
print(sieve(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
You can fix up your original algorithm like this:
for i in range(2, 101):
if all([(i % j) for j in range(2, i)]):
print(i,"is a prime number")
In general, you're probably better off using/learning from the established algorithms in cases like these. Here's a Python implementation of a well-known algorithm (the Sieve of Eratosthenes) for generating the first n primes (credit to tech.io for the code):
def sieve(n):
primes = 2*[False] + (n-1)*[True]
for i in range(2, int(n**0.5+1.5)):
for j in range(i*i, n+1, i):
primes[j] = False
return [prime for prime, checked in enumerate(primes) if checked]
Some test output:
print(sieve(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
edited Nov 11 at 6:16
answered Nov 11 at 6:01
tel
3,5511427
3,5511427
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53246232%2fhow-to-use-for-loop-to-calculate-a-prime-number%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
1
I'm not sure what you're asking here. That program appears to correctly (albeit not efficiently) differentiate between prime and composite numbers.
– Matt
Nov 11 at 5:58
well when I add
if (i%j) != 0:it dosen't work– Haoyang Song
Nov 11 at 6:06
1
if (i%j) != 0:tests for divisibility of i by j, specifically that i is not divisible by j. If i were divisible by j (for some 2 <= j < i), it couldn't be prime. There are 520 existing questions on [python] divisibility; this is a duplicate and should be closed; please read through the others.– smci
Nov 11 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 at 17:41