aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/roger-bell-west/javascript/ch-2.js
diff options
context:
space:
mode:
authorRoger Bell_West <Firedrake@users.noreply.github.com>2022-01-10 10:58:14 +0000
committerRoger Bell_West <Firedrake@users.noreply.github.com>2022-01-10 10:58:14 +0000
commit89b75091efcc8ab7ec62564a9dbe319cb0552eba (patch)
treea07d964290c1927412d827375c28126975744653 /challenge-147/roger-bell-west/javascript/ch-2.js
parente9411bdc7658179af3f23d3ada7970323547a7d7 (diff)
downloadperlweeklychallenge-club-89b75091efcc8ab7ec62564a9dbe319cb0552eba.tar.gz
perlweeklychallenge-club-89b75091efcc8ab7ec62564a9dbe319cb0552eba.tar.bz2
perlweeklychallenge-club-89b75091efcc8ab7ec62564a9dbe319cb0552eba.zip
Solutions for challenge #147
Diffstat (limited to 'challenge-147/roger-bell-west/javascript/ch-2.js')
-rwxr-xr-xchallenge-147/roger-bell-west/javascript/ch-2.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-147/roger-bell-west/javascript/ch-2.js b/challenge-147/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..8d55a5edde
--- /dev/null
+++ b/challenge-147/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,41 @@
+#! /usr/bin/node
+
+function pentagon(n) {
+ return Math.floor(n*(3*n-1)/2);
+}
+
+function pentpair() {
+ let fpent=[0];
+ let rpent=new Map();
+ let mx=0;
+ let a=1;
+ while (true) {
+ while (mx < a) {
+ mx++;
+ fpent.push(pentagon(mx));
+ rpent.set(fpent[mx],mx);
+ }
+ for (b = 1; b < a; b++) {
+ let d=fpent[a]-fpent[b];
+ if (d < fpent[b]) {
+ break;
+ }
+ if (rpent.has(d)) {
+ let s=fpent[a]+fpent[b];
+ while (s > fpent[mx]) {
+ mx++;
+ fpent.push(pentagon(mx));
+ rpent.set(fpent[mx],mx);
+ }
+ if (rpent.has(s)) {
+ console.log("P(%d) + P(%d) = %d + %d = %d = P(%d)",a,b,fpent[a],fpent[b],s,rpent.get(s));
+ console.log("P(%d) - P(%d) = %d - %d = %d = P(%d)",a,b,fpent[a],fpent[b],d,rpent.get(d));
+ throw '';
+ }
+ }
+ }
+ a++;
+ }
+}
+
+pentpair();