Prime Integer

Suggest two different methods of knowing if a given integer is prime and analyse them.

Questions by Dheerendra_juli

Showing Answers 1 - 3 of 3 Answers

rrc_83

  • Jun 21st, 2010
 

Prime Integer can be calculated as follows -

if n < 2:
  return False

if n == 2 :
  return True  // Only even number thats Prime

if (n % 10) in [0, 2, 4, 5, 6, 8]:
  return False  // Numbers ending in 0, 2, 4, 5, 6 ,8 are not prime

// Divide the number by all odd numbers upto square root of number
for x in range(3, n**0.5 + 1, 2): 
  if n % x == 0:
     return True

return False

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions