forked from intellectronica/eliteagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.js
More file actions
37 lines (35 loc) · 886 Bytes
/
prime.js
File metadata and controls
37 lines (35 loc) · 886 Bytes
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
function isPrime(n) {
if (n < 2) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
let i = 3;
while (i * i <= n) {
if (n % i === 0) return false;
i += 2;
}
return true;
}
function intToRoman(n) {
if (!(1 <= n && n <= 3999)) {
throw new Error("intToRoman supports values from 1 to 3999");
}
const numerals = [
[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"],
[100, "C"], [90, "XC"], [50, "L"], [40, "XL"],
[10, "X"], [9, "IX"], [5, "V"], [4, "IV"], [1, "I"],
];
let result = [];
for (let [value, symbol] of numerals) {
if (n === 0) break;
const count = Math.floor(n / value);
n %= value;
if (count > 0) {
result.push(symbol.repeat(count));
}
}
return result.join("");
}
for (let i = 1; i <= 1000; i++) {
const rn = intToRoman(i);
console.log(`${rn}${isPrime(i) ? '*' : ''}`);
}