Created
October 18, 2016 05:41
-
-
Save feliperazeek/c28f6f41d3460c6bbb8b261e82a96137 to your computer and use it in GitHub Desktop.
HackerRank - Cracking the Code Interview - Time Complexity: Primality (https://www.hackerrank.com/challenges/ctci-big-o)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
private static boolean isPrime(int n) { | |
if (n <= 1) return false; | |
if (n % 2 == 0) return false; | |
for (int i = 3; i * i <= n; i+=2) { | |
if(n % i == 0) return false; | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int p = in.nextInt(); | |
for(int a0 = 0; a0 < p; a0++){ | |
int n = in.nextInt(); | |
if (isPrime(n)) System.out.println("Prime"); | |
else System.out.println("Not prime"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment