primes in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Loyisile
    New Member
    • Mar 2010
    • 3

    primes in python

    I need to implement an algorithm that determines whether a number is prime or not.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Now that I understand your need, what is your question? Given a number, let's say 21, how would you determine if that number is prime or not on scratch paper?

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Are you trying to determine primes for numbers less than 341,550,071,728 ,321? Or are you planning on using this test in a cryptographic setting?

      One is easy... the other will make your brain hurt.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Here is the brain hurting part: http://www.math.dartmouth.edu/~carlp/

        Comment

        • bazookajoe1
          New Member
          • Mar 2010
          • 2

          #5
          Code:
          import math
          
          def isPrime(x):
              """Returns True if x is prime or False if x is not prime."""
              if x <= 1:
                  return False
              else:
                  y = int(math.sqrt(x))
                  while x % y != 0:
                      y -= 1
                  if y == 1:
                      return True
                  else:
                      return False

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            sympy has an isprime function

            Comment

            Working...