Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Day 25: Running Time and Complexity in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Link for the Problem – Day 25: Running Time and Complexity – Hacker Rank Solution
Day 25: Running Time and Complexity – Hacker Rank Solution
Problem:
Objective
Today we will learn about running time, also known as time complexity. Check out the Tutorial tab for learning materials and an instructional video.
Task
A prime is a natural number greater than that has no positive divisors other than and itself. Given a number, , determine and print whether it is or .
Note: If possible, try to come up with a primality algorithm, or see what sort of optimizations you come up with for an algorithm. Be sure to check out the Editorial after submitting your code.
Input Format
The first line contains an integer, , the number of test cases.
Each of the subsequent lines contains an integer, , to be tested for primality.
Constraints
Output Format
For each test case, print whether is or on a new line.
Sample Input
3 12 5 7
Sample Output
Not prime Prime Prime
Explanation
Test Case 0: .
is divisible by numbers other than and itself (i.e.: , , , ), so we print on a new line.
Test Case 1: .
is only divisible and itself, so we print on a new line.
Test Case 2: .
is only divisible and itself, so we print on a new line.
Day 25: Running Time and Complexity – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class Day25RunningTimeAndComplexity { static boolean isPrime(int n) { if (n < 2) { return false; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String result = isPrime(sc.nextInt()) ? "Prime" : "Not prime"; System.out.println(result); } sc.close(); } }