Testing divisibility of a list and appending if prime









up vote
1
down vote

favorite












I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.



I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:



from random import randrange
from time import sleep

def prime():
user_num = eval(input("Input a number: "))
list_prime =
for i in range(2,user_num):
if (i % 2) == 1 and
(i % 3) == 1 and
(i % 4) == 1 and
(i % 5) == 1 and
(i % 6) == 1 and
(i % 7) == 1 and
(i % 8) == 1 and
(i % 9) == 1 or
i == 2:
list_prime.append(i)

if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)

prime()









share|improve this question

























    up vote
    1
    down vote

    favorite












    I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.



    I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:



    from random import randrange
    from time import sleep

    def prime():
    user_num = eval(input("Input a number: "))
    list_prime =
    for i in range(2,user_num):
    if (i % 2) == 1 and
    (i % 3) == 1 and
    (i % 4) == 1 and
    (i % 5) == 1 and
    (i % 6) == 1 and
    (i % 7) == 1 and
    (i % 8) == 1 and
    (i % 9) == 1 or
    i == 2:
    list_prime.append(i)

    if list_prime == '':
    print('No prime numbers.')
    if list_prime != '':
    print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

    sleep(1)
    print('nClosing console in 60 seconds...')
    sleep(60)

    prime()









    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.



      I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:



      from random import randrange
      from time import sleep

      def prime():
      user_num = eval(input("Input a number: "))
      list_prime =
      for i in range(2,user_num):
      if (i % 2) == 1 and
      (i % 3) == 1 and
      (i % 4) == 1 and
      (i % 5) == 1 and
      (i % 6) == 1 and
      (i % 7) == 1 and
      (i % 8) == 1 and
      (i % 9) == 1 or
      i == 2:
      list_prime.append(i)

      if list_prime == '':
      print('No prime numbers.')
      if list_prime != '':
      print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

      sleep(1)
      print('nClosing console in 60 seconds...')
      sleep(60)

      prime()









      share|improve this question













      I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.



      I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:



      from random import randrange
      from time import sleep

      def prime():
      user_num = eval(input("Input a number: "))
      list_prime =
      for i in range(2,user_num):
      if (i % 2) == 1 and
      (i % 3) == 1 and
      (i % 4) == 1 and
      (i % 5) == 1 and
      (i % 6) == 1 and
      (i % 7) == 1 and
      (i % 8) == 1 and
      (i % 9) == 1 or
      i == 2:
      list_prime.append(i)

      if list_prime == '':
      print('No prime numbers.')
      if list_prime != '':
      print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

      sleep(1)
      print('nClosing console in 60 seconds...')
      sleep(60)

      prime()






      python-3.x primes






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 6:11









      LamerLink

      33




      33






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          As he said, you werent correctly checking for prime numbers.



          from random import randrange
          from time import sleep

          list_prime =
          user_num = 0

          def prime():
          user_num = eval(input("Input a number: "))
          for i in range(2,user_num):
          j = 2
          isprime = 1
          while (j <= i/2):
          if (i % j == 0):
          isprime = 0
          break
          j+=1
          if (isprime == 1):
          list_prime.append(i)

          prime()
          if list_prime == '':
          print('No prime numbers.')
          if list_prime != '':
          print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

          sleep(1)
          print('nClosing console in 60 seconds...')
          sleep(60)





          share|improve this answer




















          • Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
            – LamerLink
            Nov 11 at 14:12

















          up vote
          0
          down vote













          Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.



          A wildly sub-optimal implementation could look like this:



          list_prime = 
          for i in range(2, user_num):
          if all(n % x for x in list_prime):
          list_prime.append(i)





          share|improve this answer




















          • Thanks. What makes this sub-optimal?
            – LamerLink
            Nov 11 at 13:42










          • Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
            – billmill
            Nov 25 at 7:54










          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%2f53246320%2ftesting-divisibility-of-a-list-and-appending-if-prime%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
          1
          down vote



          accepted










          As he said, you werent correctly checking for prime numbers.



          from random import randrange
          from time import sleep

          list_prime =
          user_num = 0

          def prime():
          user_num = eval(input("Input a number: "))
          for i in range(2,user_num):
          j = 2
          isprime = 1
          while (j <= i/2):
          if (i % j == 0):
          isprime = 0
          break
          j+=1
          if (isprime == 1):
          list_prime.append(i)

          prime()
          if list_prime == '':
          print('No prime numbers.')
          if list_prime != '':
          print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

          sleep(1)
          print('nClosing console in 60 seconds...')
          sleep(60)





          share|improve this answer




















          • Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
            – LamerLink
            Nov 11 at 14:12














          up vote
          1
          down vote



          accepted










          As he said, you werent correctly checking for prime numbers.



          from random import randrange
          from time import sleep

          list_prime =
          user_num = 0

          def prime():
          user_num = eval(input("Input a number: "))
          for i in range(2,user_num):
          j = 2
          isprime = 1
          while (j <= i/2):
          if (i % j == 0):
          isprime = 0
          break
          j+=1
          if (isprime == 1):
          list_prime.append(i)

          prime()
          if list_prime == '':
          print('No prime numbers.')
          if list_prime != '':
          print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

          sleep(1)
          print('nClosing console in 60 seconds...')
          sleep(60)





          share|improve this answer




















          • Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
            – LamerLink
            Nov 11 at 14:12












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          As he said, you werent correctly checking for prime numbers.



          from random import randrange
          from time import sleep

          list_prime =
          user_num = 0

          def prime():
          user_num = eval(input("Input a number: "))
          for i in range(2,user_num):
          j = 2
          isprime = 1
          while (j <= i/2):
          if (i % j == 0):
          isprime = 0
          break
          j+=1
          if (isprime == 1):
          list_prime.append(i)

          prime()
          if list_prime == '':
          print('No prime numbers.')
          if list_prime != '':
          print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

          sleep(1)
          print('nClosing console in 60 seconds...')
          sleep(60)





          share|improve this answer












          As he said, you werent correctly checking for prime numbers.



          from random import randrange
          from time import sleep

          list_prime =
          user_num = 0

          def prime():
          user_num = eval(input("Input a number: "))
          for i in range(2,user_num):
          j = 2
          isprime = 1
          while (j <= i/2):
          if (i % j == 0):
          isprime = 0
          break
          j+=1
          if (isprime == 1):
          list_prime.append(i)

          prime()
          if list_prime == '':
          print('No prime numbers.')
          if list_prime != '':
          print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))

          sleep(1)
          print('nClosing console in 60 seconds...')
          sleep(60)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 6:50









          Simranjeet Singh

          261




          261











          • Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
            – LamerLink
            Nov 11 at 14:12
















          • Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
            – LamerLink
            Nov 11 at 14:12















          Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
          – LamerLink
          Nov 11 at 14:12




          Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
          – LamerLink
          Nov 11 at 14:12












          up vote
          0
          down vote













          Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.



          A wildly sub-optimal implementation could look like this:



          list_prime = 
          for i in range(2, user_num):
          if all(n % x for x in list_prime):
          list_prime.append(i)





          share|improve this answer




















          • Thanks. What makes this sub-optimal?
            – LamerLink
            Nov 11 at 13:42










          • Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
            – billmill
            Nov 25 at 7:54














          up vote
          0
          down vote













          Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.



          A wildly sub-optimal implementation could look like this:



          list_prime = 
          for i in range(2, user_num):
          if all(n % x for x in list_prime):
          list_prime.append(i)





          share|improve this answer




















          • Thanks. What makes this sub-optimal?
            – LamerLink
            Nov 11 at 13:42










          • Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
            – billmill
            Nov 25 at 7:54












          up vote
          0
          down vote










          up vote
          0
          down vote









          Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.



          A wildly sub-optimal implementation could look like this:



          list_prime = 
          for i in range(2, user_num):
          if all(n % x for x in list_prime):
          list_prime.append(i)





          share|improve this answer












          Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.



          A wildly sub-optimal implementation could look like this:



          list_prime = 
          for i in range(2, user_num):
          if all(n % x for x in list_prime):
          list_prime.append(i)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 6:26









          Mureinik

          175k21126194




          175k21126194











          • Thanks. What makes this sub-optimal?
            – LamerLink
            Nov 11 at 13:42










          • Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
            – billmill
            Nov 25 at 7:54
















          • Thanks. What makes this sub-optimal?
            – LamerLink
            Nov 11 at 13:42










          • Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
            – billmill
            Nov 25 at 7:54















          Thanks. What makes this sub-optimal?
          – LamerLink
          Nov 11 at 13:42




          Thanks. What makes this sub-optimal?
          – LamerLink
          Nov 11 at 13:42












          Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
          – billmill
          Nov 25 at 7:54




          Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
          – billmill
          Nov 25 at 7:54

















          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%2f53246320%2ftesting-divisibility-of-a-list-and-appending-if-prime%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto