Skip to content

Commit fc0aba0

Browse files
committed
math/
1 parent cabf733 commit fc0aba0

File tree

5 files changed

+169
-183
lines changed

5 files changed

+169
-183
lines changed

maths/DijkstraSmallestPath.js

+67-74
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
11
// starting at s
2-
function solve(graph, s) {
3-
var solutions = {};
4-
solutions[s] = [];
5-
solutions[s].dist = 0;
6-
7-
while(true) {
8-
var p = null;
9-
var neighbor = null;
10-
var dist = Infinity;
11-
12-
13-
for(var n in solutions) {
14-
if(!solutions[n])
15-
continue
16-
var ndist = solutions[n].dist;
17-
var adj = graph[n];
18-
19-
for(var a in adj) {
2+
function solve (graph, s) {
3+
var solutions = {}
4+
solutions[s] = []
5+
solutions[s].dist = 0
206

21-
if(solutions[a])
22-
continue;
23-
24-
var d = adj[a] + ndist;
25-
if(d < dist) {
26-
27-
p = solutions[n];
28-
neighbor = a;
29-
dist = d;
7+
while (true) {
8+
var p = null
9+
var neighbor = null
10+
var dist = Infinity
11+
12+
for (var n in solutions) {
13+
if (!solutions[n]) { continue }
14+
var ndist = solutions[n].dist
15+
var adj = graph[n]
16+
17+
for (var a in adj) {
18+
if (solutions[a]) { continue }
19+
20+
var d = adj[a] + ndist
21+
if (d < dist) {
22+
p = solutions[n]
23+
neighbor = a
24+
dist = d
3025
}
3126
}
3227
}
33-
34-
//no more solutions
35-
if(dist === Infinity) {
36-
break;
28+
29+
// no more solutions
30+
if (dist === Infinity) {
31+
break
3732
}
38-
39-
//extend parent's solution path
40-
solutions[neighbor] = p.concat(neighbor);
41-
//extend parent's cost
42-
solutions[neighbor].dist = dist;
33+
34+
// extend parent's solution path
35+
solutions[neighbor] = p.concat(neighbor)
36+
// extend parent's cost
37+
solutions[neighbor].dist = dist
4338
}
44-
45-
return solutions;
39+
40+
return solutions
4641
}
47-
//create graph
48-
var graph = {};
42+
// create graph
43+
var graph = {}
4944

5045
var layout = {
51-
'R': ['2'],
52-
'2': ['3','4'],
53-
'3': ['4','6','13'],
54-
'4': ['5','8'],
55-
'5': ['7','11'],
56-
'6': ['13','15'],
57-
'7': ['10'],
58-
'8': ['11','13'],
59-
'9': ['14'],
60-
'10': [],
61-
'11': ['12'],
62-
'12': [],
63-
'13': ['14'],
64-
'14': [],
65-
'15': []
46+
R: ['2'],
47+
2: ['3', '4'],
48+
3: ['4', '6', '13'],
49+
4: ['5', '8'],
50+
5: ['7', '11'],
51+
6: ['13', '15'],
52+
7: ['10'],
53+
8: ['11', '13'],
54+
9: ['14'],
55+
10: [],
56+
11: ['12'],
57+
12: [],
58+
13: ['14'],
59+
14: [],
60+
15: []
6661
}
6762

68-
//convert uni-directional to bi-directional graph
63+
// convert uni-directional to bi-directional graph
6964
// var graph = {
7065
// a: {e:1, b:1, g:3},
7166
// b: {a:1, c:1},
@@ -77,27 +72,25 @@ var layout = {
7772
// h: {f:1}
7873
// };
7974

80-
for(var id in layout) {
81-
if(!graph[id])
82-
graph[id] = {};
83-
layout[id].forEach(function(aid) {
84-
graph[id][aid] = 1;
85-
if(!graph[aid])
86-
graph[aid] = {};
87-
graph[aid][id] = 1;
88-
});
75+
for (var id in layout) {
76+
if (!graph[id]) { graph[id] = {} }
77+
layout[id].forEach(function (aid) {
78+
graph[id][aid] = 1
79+
if (!graph[aid]) { graph[aid] = {} }
80+
graph[aid][id] = 1
81+
})
8982
}
9083

91-
//choose start node
92-
var start = '10';
93-
//get all solutions
94-
var solutions = solve(graph, start);
84+
// choose start node
85+
var start = '10'
86+
// get all solutions
87+
var solutions = solve(graph, start)
9588

96-
console.log("From '"+start+"' to");
97-
//display solutions
98-
for(var s in solutions) {
99-
if(!solutions[s]) continue;
100-
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")");
89+
console.log("From '" + start + "' to")
90+
// display solutions
91+
for (var s in solutions) {
92+
if (!solutions[s]) continue
93+
console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
10194
}
10295

10396
// From '10' to

maths/abs.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
https://en.wikipedia.org/wiki/Absolute_value
1212
*/
1313

14-
function abs_val (num) {
14+
function absVal (num) {
1515
// Find absolute value of `num`.
1616
'use strict'
1717
if (num < 0) {
@@ -22,5 +22,5 @@ function abs_val (num) {
2222
}
2323

2424
// Run `abs` function to find absolute value of two numbers.
25-
console.log('The absolute value of -34 is ' + abs_val(-34))
26-
console.log('The absolute value of 34 is ' + abs_val(34))
25+
console.log('The absolute value of -34 is ' + absVal(-34))
26+
console.log('The absolute value of 34 is ' + absVal(34))

maths/factorial.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
'use strict'
1515

16-
function calc_range (num) {
16+
function calcRange (num) {
1717
// Generate a range of numbers from 1 to `num`.
1818
var i = 1
1919
var range = []
@@ -24,9 +24,9 @@ function calc_range (num) {
2424
return range
2525
}
2626

27-
function calc_factorial (num) {
27+
function calcFactorial (num) {
2828
var factorial
29-
var range = calc_range(num)
29+
var range = calcRange(num)
3030

3131
// Check if the number is negative, positive, null, undefined, or zero
3232
if (num < 0) {
@@ -48,5 +48,5 @@ function calc_factorial (num) {
4848
}
4949

5050
// Run `factorial` Function to find average of a list of numbers.
51-
var num = prompt('Enter a number: ')
52-
alert(calc_factorial(num))
51+
var num = console.log('Enter a number: ')
52+
console.log(calcFactorial(num))

maths/find_lcm.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
license: GPL-3.0 or later
44
55
Modified from:
6-
https://github.com/TheAlgorithms/Python/blob/master/maths/find_lcm.py
6+
https://github.com/TheAlgorithms/Python/blob/master/maths/findLcm.py
77
88
More about LCM:
99
https://en.wikipedia.org/wiki/Least_common_multiple
@@ -12,27 +12,27 @@
1212
'use strict'
1313

1414
// Find the LCM of two numbers.
15-
function find_lcm (num_1, num_2) {
16-
var max_num
15+
function findLcm (num1, num2) {
16+
var maxNum
1717
var lcm
18-
// Check to see whether num_1 or num_2 is larger.
19-
if (num_1 > num_2) {
20-
max_num = num_1
18+
// Check to see whether num1 or num2 is larger.
19+
if (num1 > num2) {
20+
maxNum = num1
2121
} else {
22-
max_num = num_2
22+
maxNum = num2
2323
}
24-
lcm = max_num
24+
lcm = maxNum
2525

2626
while (true) {
27-
if ((lcm % num_1 === 0) && (lcm % num_2 === 0)) {
27+
if ((lcm % num1 === 0) && (lcm % num2 === 0)) {
2828
break
2929
}
30-
lcm += max_num
30+
lcm += maxNum
3131
}
3232
return lcm
3333
}
3434

35-
// Run `find_lcm` Function
36-
var num_1 = 12
37-
var num_2 = 76
38-
console.log(find_lcm(num_1, num_2))
35+
// Run `findLcm` Function
36+
var num1 = 12
37+
var num2 = 76
38+
console.log(findLcm(num1, num2))

0 commit comments

Comments
 (0)