omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Prime number finder

    Pythonista
    prime numbers pythonista3
    5
    16
    10384
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • PINEHACKS
      PINEHACKS last edited by ccc

      I have some python prime number code which finds out prime numbers. I was looking for a way to make it more efficient as this currently checks for all factors until it reachers the number, it possible could you make the explainations simple as I am 13. Here is the code:

      import time
      
      print('Made by Yasas Kulatunga, Dhari Prashanth and Dhivy Prashanth')
      print(' ')
      number = int(input("Enter a Number: "))
      print(' ')
      time.sleep(0.5)
      print('THINKING.')
      time.sleep(0.5)
      print('THINKING..')
      time.sleep(0.5)
      print('THINKING...')
      time.sleep(0.5)
      
      print(' ')
      c = 0
      while number:
      	for i in range(2,number):
      	 if number % i == 0:
      			print(str(i),"times",str(number//i),"is",str(number))
      			time.sleep(0.3)
      			c = c + 1
      	if c == 0:
      		print(' ')
      		print(str(number),"has no factors apart from 1 and itself, therefore it is a prime number")
      		print(' ')
      		time.sleep(0.3)
      	else:
      		print(' ')
      		print(str(number),"is not a prime number, the number has factors more than 1 and the number itself.")
      		print(' ')
      		time.sleep(0.3)
      	number = int(input('Enter a number: '))
      	print(' ')
      	time.sleep(0.3)
      	c = 0
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by ccc

        I never used a for / else loop before but here it works well. When we find the first factor, we print a not prime message and then fast fail out of the loop with break. If the loop finishes without a break then we use the else clause to tell the use that we have a prime number.

        Let's call this the 5F approach: finding first factor fast fail...

        import time
        
        print('Made by Yasas Kulatunga, Dhari Prashanth and Dhivy Prashanth')
        print(' ')
        number = int(input("Enter a Number: ").strip())
        print(' ')
        time.sleep(0.5)
        print('THINKING.')
        time.sleep(0.5)
        print('THINKING..')
        time.sleep(0.5)
        print('THINKING...')
        time.sleep(0.5)
        
        print(' ')
        while number:
            for i in range(2, number):
                if number % i == 0:
                    print('{} is not prime.'.format(number))
                    break
            else:
                print('{} is prime!'.format(number))
            number = int(input("Enter a Number: ").strip())
        
        1 Reply Last reply Reply Quote 1
        • ramvee
          ramvee last edited by ramvee

          More efficient way would be to check the number for divisibility, only till its' square root - 1.
          Because the largest factor of any number cannot exceed it's square root, there is no need for range to go beyond that.
          With minimal alteration to script given by @ccc , this would be more efficient.

          # Revised as per bug reported by @cvp
          # This code takes care of numbers 1 and 2 also.
          import time
          from math import sqrt
          
          print('Made by Yasas Kulatunga, Dhari Prashanth and Dhivy Prashanth')
          print(' ')
          number = int(input("Enter a Number: ").strip())
          maxfact = int(sqrt(number)+1)
          print(' ')
          time.sleep(0.5)
          print('THINKING.')
          time.sleep(0.5)
          print('THINKING..')
          time.sleep(0.5)
          print('THINKING...')
          time.sleep(0.5)
          
          print(' ')
          while number:
              for i in range(2, maxfact):
                  if number % i == 0:
                      print('{} is not prime.'.format(number))
                      break
              else:
                  print('{} is prime!'.format(number))
              number = int(input("Enter a Number: ").strip())
              maxfact = int(sqrt(number)+1)
          
          1 Reply Last reply Reply Quote 1
          • ccc
            ccc last edited by

            The above implementations do not deal with the fact that all values less than 2 are not prime.

            https://primes.utm.edu/notes/faq/one.html
            https://www.reference.com/math/0-prime-number-1964bd6272594f3f
            https://primes.utm.edu/notes/faq/negative_primes.html

            1 Reply Last reply Reply Quote 0
            • cvp
              cvp last edited by cvp

              Bug, try with 25

              maxfact = int(sqrt(number)-1) -> maxfact = int(sqrt(number))
              
              1 Reply Last reply Reply Quote 0
              • cvp
                cvp last edited by cvp

                Bug, try with 25

                for i in range(2, maxfact+1):
                
                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp last edited by cvp

                  Check if 2 is a divisor but skip all other even divisors
                  (Perhaps what @ccc minded with "less than 2")

                   for i in [2]+list(range(3, maxfact+1,2)):
                  
                  1 Reply Last reply Reply Quote 0
                  • PINEHACKS
                    PINEHACKS last edited by

                    The exact output is this, although it is from the original code I posted on top: Enter a Number: 25

                    THINKING.
                    THINKING..
                    THINKING...

                    5 times 5 is 25

                    25 is not a prime number, the number has factors more than 1 and the number itself.

                    Enter a number:

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp last edited by

                      Sorry, I had tested the last code of @ramvee until √ - 1

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp last edited by cvp

                        But he was almost right, you have to check only until sqrt(number),
                        And, what I just added, by skipping the even divisors!

                        For instance, if number is 29, your code checks the division by
                        2 3 4 5 6 7 8 9 10 11 12 13 ...29
                        But "mine" (& @ramvee) only
                        2 3 5

                        1 Reply Last reply Reply Quote 0
                        • ccc
                          ccc last edited by

                          @cvp, If you go the the three vertical dots at the upper right of your forum post, you can edit it to add new insights. This is probably a better approach than replying to your own posts.

                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp last edited by

                            @ccc, Thanks, I knew it but sometimes I forget it, and sometimes I'm not sure how other people see an update, while I'm sure they see a new response

                            ramvee 1 Reply Last reply Reply Quote 0
                            • ramvee
                              ramvee @cvp last edited by

                              @cvp
                              Thank you for pointing the bug in my code. :)

                              I put in your line of code

                              for i in [2]+list(range(3, maxfact+1,2)):

                              and it works fine,
                              Except it shows 2 is not a prime.. If we sort this we're good.. I think..

                              1 Reply Last reply Reply Quote 0
                              • Phuket2
                                Phuket2 last edited by

                                I think the consecutive 3 post's from @cvp might have been better done in edit. Maybe and maybe not. If the items inside a post themselves were also threaded , then it would be clear to do like that. But they are not. Just saying it's not a clear cut choice.

                                1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp last edited by

                                  @Phuket2 thanks for your support 😜 but I think @ccc is right for most cases...

                                  Phuket2 1 Reply Last reply Reply Quote 0
                                  • Phuket2
                                    Phuket2 @cvp last edited by

                                    @cvp , lol, yes he is normally right. But need to keep him on his toes. 😱😈

                                    1 Reply Last reply Reply Quote 0
                                    • First post
                                      Last post
                                    Powered by NodeBB Forums | Contributors