2 Replies - 1693 Views - Last Post: 07 March 2012 - 06:34 PM

#1 Cuzzie   User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 342
  • Joined: 16-July 10

Prime Number Checker

Posted 16 July 2010 - 11:39 AM

Description: For example: fnPrime(7) output: true If the output is true, it means the number is prime number. If it's false, then it's not a prime number.This is not a full application. It's just a function to check if a number is prime number or not.
def fnPrime(n):
	for i in range(2,n,1):
		if(n % i == 0):
			return bool(0)
	return bool(1)


Is This A Good Question/Topic? 0
  • +

Replies To: Prime Number Checker

#2 csm23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-July 10

Re: Prime Number Checker

Posted 22 November 2011 - 12:41 PM

While this code will work it can be a lot faster. First see if it is 2, return true if its 2. Then check to see if its even, return false if it is. Then starting from 3 divide by every odd number less the square root of the number that is being tested. If none of those number divide evenly into the number being tested then the number is prime, otherwise it is not.
Was This Post Helpful? 0
  • +
  • -

#3 mridulpv   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-September 10

Re: Prime Number Checker

Posted 07 March 2012 - 06:34 PM

to reduce time complicity def fnPrime(n): for i in range(3,n,2): if(n % i == 0): return bool(0) return bool(1)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1