3 Replies - 1850 Views - Last Post: 01 December 2011 - 09:16 PM

#1 e-papa   User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 17-March 11

Prime Number Checker

Posted 25 March 2011 - 12:27 AM

Description: Just run in python3.x environment, no modules needed.This function checks if a number is prime or not, if it's not and is more than 3 it's also outputs the lowest factor.
#this function checks if a number is a prime number,
#if not it outputs the lowest factor.

def isprime(n):
    """Determines whether a number is a Prime number,
    Takes a single arguement n, which is the number"""
    if n==1:
        return 'Not a Prime number, only has one distinct factor'
    elif n==2:
        return 'Prime number'
    elif n==3:
        return 'Prime number'
    else:
        for i in range(2,n):
            if n%i==0:
                return 'Not a prime number',i,'is a factor'
            else:
                return 'Prime number'
#example
#print(isprime(590))
#outputs
#>>>
#('Not a prime number', 2, 'is a factor')

#Adegoke Obasa [email protected]


Is This A Good Question/Topic? 0
  • +

Replies To: Prime Number Checker

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Prime Number Checker

Posted 26 March 2011 - 05:19 PM

why doesn't this return a Boolean?
Was This Post Helpful? 0
  • +
  • -

#3 e-papa   User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 17-March 11

Re: Prime Number Checker

Posted 28 March 2011 - 01:11 AM

well I figured all other codes I've seen return boolean, so i thought it should, let me try something else, but is it compulsory, please i want to learn more about best practices, to meet the standard.
Was This Post Helpful? 0
  • +
  • -

#4 MontyPython   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 01-December 11

Re: Prime Number Checker

Posted 01 December 2011 - 09:16 PM

I believe it all depends what do you want to achieve after the function, it will be more elegant to use the Boolean return with the next condition, rather than check what is the string return.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1