Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

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

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python 2 programming.
  • Problem solution in Python 3 programming.
    • Problem solution in java programming.
    • Problem solution in c++ programming.
    • Problem solution in c programming.
    • Problem solution in Javascript programming.

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes