Day 25: Running Time and Complexity

Day 25: Running Time and Complexity

Objective

Today we’re learning about running time! Check out the Tutorial tab for learning materials and an instructional video!

Task

A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given a number, n, determine and print whether it’s Prime or Not Prime.

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, T, the number of test cases.
Each of the T subsequent lines contains an integer, n, to be tested for primality.

Constraints

  • 1 <= T <= 30
  • 1 <= n <= 2 X 109

Output Format

For each test case, print whether n is Prime or Not prime on a new line.

Sample Input

1
2
3
4
3
12
5
7

Sample Output

1
2
3
Not prime
Prime
Prime

Explanation

Test Case 0: n = 12.

12 is divisible by numbers other than 1 and itself (i.e.: 2, 3, 6), so we print Not prime on a new line.

Test Case 1: n = 5.

5 is only divisible 1 and itself, so we print Prime on a new line.

Test Case 2: n = 7.

7 is only divisible 1 and itself, so we print Prime on a new line.




Solutions

Solution 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/** Consider to write code by using the Sieve of Eratosthenes, then you can make a optimization code **/

function processData(input) {
//Enter your code here
let result = input.split('\n').slice(1).map(isPrime);

function isPrime(input, i) {
let messages = ['Not prime', 'Prime'];

if (input == 1) {
return messages[0];
}
if (input == 2) {
return messages[1];
}
if (input % 2 == 0) {
return messages[0];
}

for (i = 3; i <= Math.sqrt(input); i += 2) {
if (input % i == 0) {
return messages[0];
}
}
return messages[1];
}

console.log(result.join("\n"));
}

Solution 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function processData(input) {
//Enter your code here
input.split('\n').slice(1).map((n, i) => {
console.log(isPrime(n) ? 'Prime' : 'Not prime');
});

function isPrime(n) {
if (n == 1) return false;

for (let i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}

return true;
}
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×