Skip to content

Commit b205c66

Browse files
author
Ajit Singh
committed
add gcd and spiralmatrix
1 parent 982334e commit b205c66

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

math/gcd.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// GCD - greatest common divisor or HCF - highest common factor
2+
3+
function gcd (small, large) {
4+
if (small === 0) { return large } else { return gcd(large % small, small) }
5+
}
6+
7+
const gcdList = [[6, 9], [6, 12], [12, 18], [7, 14], [7, 13]]
8+
9+
for (const set of gcdList) {
10+
const small = set[0]
11+
const large = set[1]
12+
console.log(`GCD for ${small} and ${large} is ${gcd(small, large)}`)
13+
}

math/spiralMatrix.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function getSpiralMatrix (n) {
2+
const matrix = []
3+
for (let i = 0; i < n; ++i) { matrix.push([]) }
4+
let counter = 1
5+
let startRow = 0
6+
let endRow = n - 1
7+
let startCol = 0
8+
let endCol = n - 1
9+
10+
while (startCol <= endCol && startRow <= endRow) {
11+
for (let i = startCol; i <= endCol; ++i) {
12+
matrix[startRow][i] = counter
13+
counter++
14+
}
15+
startRow++
16+
17+
for (let i = startRow; i <= endRow; ++i) {
18+
matrix[i][endCol] = counter
19+
counter++
20+
}
21+
endCol--
22+
23+
for (let i = endCol; i >= startCol; --i) {
24+
matrix[endRow][i] = counter
25+
counter++
26+
}
27+
endRow--
28+
29+
for (let i = endRow; i >= startRow; --i) {
30+
matrix[i][startCol] = counter
31+
counter++
32+
}
33+
startCol++
34+
}
35+
return matrix
36+
}
37+
38+
const matrix = getSpiralMatrix(3)
39+
console.log(matrix)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy