how to use for loop to calculate a prime number
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 |
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 '18 at 5:58
well when I addif (i%j) != 0:
it dosen't work
– Haoyang Song
Nov 11 '18 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 '18 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 '18 at 17:41
add a comment |
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 '18 at 6:33
smci
14.8k672104
14.8k672104
asked Nov 11 '18 at 5:56
Haoyang SongHaoyang 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 '18 at 5:58
well when I addif (i%j) != 0:
it dosen't work
– Haoyang Song
Nov 11 '18 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 '18 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 '18 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 '18 at 5:58
well when I addif (i%j) != 0:
it dosen't work
– Haoyang Song
Nov 11 '18 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 '18 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 '18 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 '18 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 '18 at 5:58
well when I add
if (i%j) != 0:
it dosen't work– Haoyang Song
Nov 11 '18 at 6:06
well when I add
if (i%j) != 0:
it dosen't work– Haoyang Song
Nov 11 '18 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 '18 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 '18 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 '18 at 17:41
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 '18 at 17:41
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 6:16
1
if the range start at 2 thei > 1
check becomes unnecessary though
– Anton vBR
Nov 11 '18 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 at 6:35
The extrai > 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 '18 at 7:22
add a comment |
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 6:16
1
if the range start at 2 thei > 1
check becomes unnecessary though
– Anton vBR
Nov 11 '18 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 at 6:35
The extrai > 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 '18 at 7:22
add a comment |
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 '18 at 6:16
1
if the range start at 2 thei > 1
check becomes unnecessary though
– Anton vBR
Nov 11 '18 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 at 6:35
The extrai > 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 '18 at 7:22
add a comment |
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 '18 at 6:04
Mayank PorwalMayank Porwal
4,8822724
4,8822724
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 '18 at 6:16
1
if the range start at 2 thei > 1
check becomes unnecessary though
– Anton vBR
Nov 11 '18 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 at 6:35
The extrai > 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 '18 at 7:22
add a comment |
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 '18 at 6:16
1
if the range start at 2 thei > 1
check becomes unnecessary though
– Anton vBR
Nov 11 '18 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 at 6:35
The extrai > 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 '18 at 7:22
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 '18 at 6:16
You are welcome. Thanks for accepting.
– Mayank Porwal
Nov 11 '18 at 6:16
1
1
if the range start at 2 the
i > 1
check becomes unnecessary though– Anton vBR
Nov 11 '18 at 6:27
if the range start at 2 the
i > 1
check becomes unnecessary though– Anton vBR
Nov 11 '18 at 6:27
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 at 6:35
Yes, in this case it does.
– Mayank Porwal
Nov 11 '18 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 '18 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 '18 at 7:22
add a comment |
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 |
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 |
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 '18 at 6:16
answered Nov 11 '18 at 6:01
teltel
7,34121431
7,34121431
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.
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 '18 at 5:58
well when I add
if (i%j) != 0:
it dosen't work– Haoyang Song
Nov 11 '18 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 '18 at 13:30
I did try the others, the others wasn't what I was asking for
– Haoyang Song
Nov 11 '18 at 17:41