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










share|improve this question
























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

















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










share|improve this question
























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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 hours ago

























asked 9 hours ago









Sotiris Liagas

294




294












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




















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


















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














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.






share|improve this answer




























    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,





    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%2f53400108%2fpython-prime-number-for-else-range%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













      When num is 2, range(2, num) is empty, so the if (num % i) == 0: check is not performed, and the else block executes.






      share|improve this answer

























        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.






        share|improve this answer























          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.






          share|improve this answer












          When num is 2, range(2, num) is empty, so the if (num % i) == 0: check is not performed, and the else block executes.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 9 hours ago









          Patrick Haugh

          25.8k82544




          25.8k82544
























              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,





              share|improve this answer

























                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,





                share|improve this answer























                  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,





                  share|improve this answer












                  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,






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  Mark_Anderson

                  421215




                  421215






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...