how to use for loop to calculate a prime number









up vote
0
down vote

favorite
2












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)










share|improve this question



















  • 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














up vote
0
down vote

favorite
2












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)










share|improve this question



















  • 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












up vote
0
down vote

favorite
2









up vote
0
down vote

favorite
2






2





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)










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












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")





share|improve this answer




















  • You are welcome. Thanks for accepting.
    – Mayank Porwal
    Nov 11 at 6:16






  • 1




    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










  • 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


















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]





share|improve this answer






















    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',
    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
    );



    );













    draft saved

    draft discarded


















    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








    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")





    share|improve this answer




















    • You are welcome. Thanks for accepting.
      – Mayank Porwal
      Nov 11 at 6:16






    • 1




      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










    • 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















    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")





    share|improve this answer




















    • You are welcome. Thanks for accepting.
      – Mayank Porwal
      Nov 11 at 6:16






    • 1




      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










    • 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













    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")





    share|improve this answer












    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")






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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










    • 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

















    • You are welcome. Thanks for accepting.
      – Mayank Porwal
      Nov 11 at 6:16






    • 1




      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










    • 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
















    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













    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]





    share|improve this answer


























      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]





      share|improve this answer
























        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]





        share|improve this answer














        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]






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 6:16

























        answered Nov 11 at 6:01









        tel

        3,5511427




        3,5511427



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric