In this HackerRank Day 25 Running Time and Complexity 30 days of code problem set, we need to develop a program that can take integer input and then print that whether a number is a prime number or not a prime number.
Problem solution in Python 2 programming.
# Enter your code here. Read input from STDIN. Print output to STDOUT import math N = int(raw_input().strip()) def isPrime(n): if n < 2: return False elif n < 4: return True else: prime = True for i in xrange(2, int(math.sqrt(n))): if (n % i == 0): prime = False break return prime for i in xrange(N): num = int(raw_input().strip()) if isPrime(num): print "Prime" else: print "Not prime"
Problem solution in Python 3 programming.
from math import sqrt T = int(input()) def isPrime(n): for i in range(2, int(sqrt(n)+1)): if n % i is 0: return False return True for _ in range(T): n = int(input()) if n >= 2 and isPrime(n): print("Prime") else: print("Not prime")
Problem solution in java programming.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); for (int i = 0; i < N; i++) { if (isPrime(sc.nextInt())) System.out.println("Prime"); else System.out.println("Not prime"); } } private static boolean isPrime(int num) { if (num == 1) return false; for (int i = 2; i < Math.sqrt(num); i++) if (num % i == 0) return false; return true; } }
Problem solution in c++ programming.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T; cin >> T; for(int t = 0; t < T; t++){ int n; cin >> n; if(n == 1){ printf("Not primen"); continue; } if(n <= 3){ printf("Primen"); continue; } if(n % 2 == 0 || n % 3 == 0){ printf("Not primen"); continue; } bool prime = true; int sqRoot = sqrt(n); for(int i = 3; i <= sqRoot; i+=2){ if(n % i == 0){ prime = false; break; } } if(prime){ printf("Primen"); } else { printf("Not primen"); } } return 0; }
Problem solution in c programming.
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int isPrime(int n) { int i; int res = 1; if (n < 2) { return 0; } for (i = 2; i < sqrt(n); i++) { if (n % i == 0) { res = 0; break; } } return res; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int i; int t; int n; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d", &n); if (isPrime(n)) { printf("Primen"); } else { printf("Not primen"); } } return 0; }
Problem solution in Javascript programming.
function processData(input) { var arr = input.split('n'); for (var i = 1; i < arr.length; i++){ var n = arr[i]; if(isPrime(n)){ console.log("Prime"); } else { console.log("Not prime"); } } } function isPrime(n){ if (n <= 1) { return false; } if (n <= 3) { return true; } // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0){ return false; } for (var index=5; index*index<=n; index=index+6){ if (n%index == 0 || n%(index+2) == 0) { return false; } } return true; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
the one from java is showing error for test case 9