Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created October 18, 2016 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliperazeek/c28f6f41d3460c6bbb8b261e82a96137 to your computer and use it in GitHub Desktop.
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)
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