In this HackerRank Day 25: Running Time and Complexity problem solution we have given a number, n, determine and print whether it is Prime or Not prime.

HackerRank Day 25: Running Time and Complexity problem solution

Problem solution in Python programming.

import math
n = int(input())
for i in range(n):
    isPrime = True
    num = int(input())
    if num <= 1:
        isPrime = False
    else:
        for n in range(2, math.ceil(math.sqrt(num))):
            if num%n == 0:
                isPrime = False
                break
    if(isPrime):
        print( 'Prime' )
    else:
        print( 'Not prime' )



Problem solution in Java programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. */
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
      
        for(int idx = 0; idx < n; ++idx)
        {
            System.out.println(isPrime(scan.nextInt()) ? "Prime" : "Not prime");
        }
    }
    
    public static boolean isPrime(int value)
    {
        if(value<= 1) return false;
        if(value == 2) return true;
        
        //Check values up to the square root.
        int sqrt = (int)Math.ceil(Math.sqrt(value));
        for(int idx = 2; idx <= sqrt; ++idx)
        {
            if(value % idx == 0)
            {
                return false;
            }
        }
        
        return true;
    }
}




Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

bool p(int a){
    for(int i=2; i*i<=a; ++i)
        if(a%i == 0) return false;
    return true && a != 1;
}


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int t;
    cin >> t;
    while(t--){
        int a;
        cin >> a;
        if(p(a)) cout << "Prime" << endl;
        else cout << "Not prime" << endl;
    }
    return 0;
}



Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int T;
    scanf("%d\n", &T);
    for (int i = 0; i < T; i++) {
        int num;
        scanf("%d", &num);
        int k;
        int square_root = (int)sqrt(num);
        if (num > 1) {
            for (k = 2; k < square_root; k++) {
                if ((num % k && num != k) == 0) {
                    printf("Not prime\n");
                    break; 
              }
            }
            if (k == square_root || num == k) {
                printf("Prime\n");
            }
        } else {
            printf("Not prime\n");
        }
    }
    return 0;
}



Problem solution in Java programming.

function processData(input) {
    input = input.split('\n').slice(1);
    main_loop: for(var n of input){
        if(n==1){
            console.log('Not prime');
            continue;
        }
        if(n==2){
            console.log('Prime');
            continue;
        }
        for(var i=2; i<= Math.ceil(Math.sqrt(n));i++){
            if(n%i==0){
                console.log('Not prime');
                continue main_loop;
            }
        }
        console.log('Prime');
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});