-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.js
More file actions
124 lines (98 loc) · 3.33 KB
/
algorithms.js
File metadata and controls
124 lines (98 loc) · 3.33 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// JAVASCRIPT PROBLEMS AND SOLUTIONS
//1. A permutation is an ordered arrangement of objects. What is the millionth lexicographic permutation of the digits 0,1,2,3,4,5,6,7,8,9
function getNthPermutation(n, digits) {
let result = [];
let factorial = 1;
for (let i = 1; i <= digits.length; i++) {
factorial *= i;
}
n--; // Convert to 0-based index
for (let i = digits.length; i > 0; i--) {
factorial /= i;
const index = Math.floor(n / factorial);
n %= factorial;
result.push(digits.splice(index, 1)[0]);
}
return result.join('');
}
const millionthPermutation = getNthPermutation(1000000, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
console.log(millionthPermutation);
//2. Find the sum of the digits in the number 100!
function calculateFactorial(num) {
if (num === 0 || num === 1) {
return BigInt(1);
} else {
return BigInt(num) * calculateFactorial(num - 1);
}
}
function sumOfDigits(num) {
return num.toString().split('').reduce((sum, digit) => sum + parseInt(digit, 10), 0);
}
const factorial100 = calculateFactorial(100);
const sumDigitsFactorial100 = sumOfDigits(factorial100);
console.log(sumDigitsFactorial100);
//3. The sequence of triangle numbers is generated by adding the natural numbers. What is the value of the first triangle number to have over five hundred divisions
function getDivisorsCount(number) {
let count = 0;
for (let i = 1; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
count += 2; // Both i and number/i are divisors
}
}
// Adjust if the number is a perfect square
if (Math.sqrt(number) ** 2 === number) {
count--;
}
return count;
}
function findTriangleNumberWithDivisors(divisorCount) {
let triangleNumber = 0;
let naturalNumber = 1;
while (true) {
triangleNumber += naturalNumber;
naturalNumber++;
const divisorsCount = getDivisorsCount(triangleNumber);
if (divisorsCount > divisorCount) {
return triangleNumber;
}
}
}
const targetDivisorCount = 500;
const result2 = findTriangleNumberWithDivisors(targetDivisorCount);
console.log(result2);
//4. The number, 197, 8£ called a circular orine because all rotations of the digit: 197,971, and 719 are themselves prime.How many circular primes are there below one million
function isPrime(num) {
if (num < 2) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
function rotateString(str) {
const rotations = [];
for (let i = 0; i < str.length; i++) {
const rotated = str.substring(i) + str.substring(0, i);
rotations.push(rotated);
}
return rotations;
}
function areAllRotationsPrime(num) {
const rotations = rotateString(num.toString());
return rotations.every(rotation => isPrime(parseInt(rotation, 10)));
}
function countCircularPrimes(limit) {
let count = 0;
for (let i = 2; i < limit; i++) {
if (isPrime(i) && areAllRotationsPrime(i)) {
count++;
}
}
return count;
}
const limit = 1000000;
const result = countCircularPrimes(limit);
console.log(result);