aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-27 23:54:03 +0100
committerGitHub <noreply@github.com>2021-07-27 23:54:03 +0100
commitd48f022a298a83a187f15b0146874423fa51956c (patch)
treea1b1a6e560d1494f55cddf2b55b9dc90e9a0a5b8
parentdc97e7590a50e62edfb2ae426fc73ef5e6ac5bd9 (diff)
parent0324029092373f4cce35fbd28a656218221f970a (diff)
downloadperlweeklychallenge-club-d48f022a298a83a187f15b0146874423fa51956c.tar.gz
perlweeklychallenge-club-d48f022a298a83a187f15b0146874423fa51956c.tar.bz2
perlweeklychallenge-club-d48f022a298a83a187f15b0146874423fa51956c.zip
Merge pull request #4609 from stuart-little/stuart-little_123_node
1st commit on 123_node
-rwxr-xr-xchallenge-123/stuart-little/node/ch-1.js26
-rwxr-xr-xchallenge-123/stuart-little/node/ch-2.js25
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-123/stuart-little/node/ch-1.js b/challenge-123/stuart-little/node/ch-1.js
new file mode 100755
index 0000000000..5ada161822
--- /dev/null
+++ b/challenge-123/stuart-little/node/ch-1.js
@@ -0,0 +1,26 @@
+#!/usr/bin/env node
+
+// run <script> <number $n> to return the first $n ugly numbers
+
+let memo = new Set([1,2,3,5])
+
+function smth5p(n) {
+ if (memo.has(n)) {return true}
+ for (let prm of [2,3,5]) {
+ if (n%prm===0 && memo.has(n/prm)) {
+ memo.add(n)
+ return true
+ }
+ }
+ return false
+}
+
+let count=0
+let nr=0
+while (count < parseInt(process.argv[2])) {
+ nr++
+ if (smth5p(nr)) {
+ count++
+ console.log(nr)
+ }
+}
diff --git a/challenge-123/stuart-little/node/ch-2.js b/challenge-123/stuart-little/node/ch-2.js
new file mode 100755
index 0000000000..0611520f33
--- /dev/null
+++ b/challenge-123/stuart-little/node/ch-2.js
@@ -0,0 +1,25 @@
+#!/usr/bin/env node
+
+// run <script> <x1 y1 x2 y2 ..>
+
+function sqDist(coords) {
+ return (coords[2]-coords[0])**2 + (coords[3]-coords[1])**2
+}
+
+function sqDistHash(coords) {
+ let hsh={};
+ [0,1,2].forEach( function(i) {
+ [0,1,2,3].slice(i+1).forEach( function(j) {
+ let dst = sqDist([2*i,2*i+1,2*j,2*j+1].map(ix => coords[ix]))
+ hsh[dst] = hsh[dst] ? hsh[dst]+1 : 1
+ })
+ })
+ return hsh
+}
+
+function isSq(coords) {
+ const freqs = Object.values(sqDistHash(coords))
+ return freqs.includes(2) && freqs.includes(4)
+}
+
+console.log(isSq(process.argv.slice(2,10).map(x => parseFloat(x))) ? 1 : 0)