Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Day 25 Running Time and Complexity 30 days of code solution

YASH PAL, 31 July 202422 October 2025

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.

HackerRank Day 25 Running Time and Complexity 30 days of code solution

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);
});

30 days of code coding problems solutions HackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes