We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Time Complexity: Primality

Time Complexity: Primality

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

A prime is a natural number greater than that has no positive divisors other than and itself. Given integers, determine the primality of each integer and return Prime or Not prime on a new line.

Note: If possible, try to come up with an primality algorithm, or see what sort of optimizations you can come up with for an algorithm. Be sure to check out the Editorial after submitting your code.

Function Description

Complete the primality function in the editor below.

primality has the following parameter(s):

  • int n: an integer to test for primality

Returns

  • string: Prime if is prime, or Not prime

Input Format

The first line contains an integer, , the number of integers to check for primality.
Each of the subsequent lines contains an integer, , the number to test.

Constraints

Sample Input

STDIN   Function
-----   --------
3       p = 3 (number of values to follow)
12      n = 12 (first number to check)
5       n = 5
7       n = 7

Sample Output

Not prime
Prime
Prime

Explanation

We check the following integers for primality:

  1. is divisible by numbers other than and itself (i.e.: , , , ).
  2. is only divisible and itself.
  3. is only divisible and itself.

Author

blondiebytes

Difficulty

Medium

Max Score

30

Submitted By

52125

Need Help?


View discussions
View editorial
View top submissions
RESOURCES

  • Running Time and Complexity

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy