blob: dd9b97965fb3076a681aaa5b18cc0abb722efe5f (
plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#!/usr/bin/env node
// ch-1.js
/*******************************************************************************
* https://theweeklychallenge.org/blog/perl-weekly-challenge-146/
*
* ## Task 1 > 10001st Prime Number
* ================================
*
* Write a script to generate the 10001st prime number.
******************************************************************************/
'use strict';
/*******************************************************************************
* PWC Solution ****************************************************************
******************************************************************************/
function getPrime(numPrime = 10001) {
const primes = [2, 3];
const limit = parseInt(numPrime, 10);
const isPrime = (n) =>
!primes.some(
(prime) => prime <= Math.floor(Math.sqrt(n)) && n % prime === 0,
);
let num = 5;
while (primes.length < limit) {
if (isPrime(num)) primes.push(num);
num += 2;
}
return primes.slice(-1)[0];
}
/*******************************************************************************
* Utilities *******************************************************************
******************************************************************************/
function getSuffix(num) {
const lastDigit = parseInt([...num.toString()].pop(), 10);
if (lastDigit === 0 || lastDigit >= 4) return 'th';
if (lastDigit === 1) return 'st';
if (lastDigit === 2) return 'nd';
if (lastDigit === 3) return 'rd';
return '';
}
const colors = {
yellow: '\x1b[33m',
green: '\x1b[32m',
reset: '\x1b[0m',
};
/*******************************************************************************
* Main ************************************************************************
******************************************************************************/
(function main() {
const nth = process.argv[2] || 10001;
const prime = getPrime(nth);
const suffix = getSuffix(nth);
const { yellow: y, green: g, reset: r } = colors;
console.log(`The ${y + nth + suffix + r} prime number is: ${g + prime + r}`);
})();
|