Jörgs Lösung(en) für Euler 87

Ich habe die Aufgabe auf drei verschiedene Weisen implementiert. Den
einfachen, naiven Ansatz mit verschachtelten Schleifen, der auch von allen
drei Implementierungen der schnellste und speichersparendste ist.

Die Variante *func* ist an funktionale Programmierung angelehnt, wobei
alle Zwischenergebnisse in Arrays gespeichert werden, was das Programm
sehr speicherhungrig macht.

In neueren Versionen von Javascript gibt es Iteratoren, mit denen man
theoretisch die funktionale Lösung effizienter bauen kann, da die
Zwischenergebnisse nicht gespeichert werden. Allerdings hat ein Iterator
keinen Prototypen, den man schön um die Methoden erweitern könnte, so dass
der Code angenehm lesbar wäre wie in der Variante *func*.
This commit is contained in:
Jörg Sommer 2017-03-15 15:48:55 +01:00
parent 54d12e4d46
commit 416b24f900
3 changed files with 238 additions and 0 deletions

64
2017-03-14/jo-so_euler87.js Executable file
View file

@ -0,0 +1,64 @@
#!/usr/bin/js
/*
Copyright © 2017 Jörg Sommer <joerg@alea.gnuu.de>
License: MIT https://opensource.org/licenses/MIT
*/
'use strict';
//////
//
// Some monkey patching
//
if (typeof putstr === 'undefined')
// in case of node.js
var putstr = process.stdout.write.bind(process.stdout);
if (typeof print === 'undefined')
// in case of node.js
var print = console.log.bind(console);
//////
//
// Solution of Euler 87 https://projecteuler.net/problem=87
//
let primeNumbers = [2,3,5,7,11,13,17,19];
for (let num = primeNumbers[primeNumbers.length - 1] + 2;
num <= Math.sqrt(5e7); num += 2) {
for (var i = Math.floor(Math.sqrt(num)); i >= 3; --i) {
if (num % i == 0)
break;
}
if (i < 3)
primeNumbers.push(num);
}
print('There are ' + primeNumbers.length + ' prime numbers below sqrt(50.000.000)');
let nums = new Set();
for (let idx2 = 0; idx2 < primeNumbers.length; ++idx2) {
let pow2 = Math.pow(primeNumbers[idx2], 2);
for (let idx3 = 0; idx3 < primeNumbers.length; ++idx3) {
let pow3 = Math.pow(primeNumbers[idx3], 3);
let sum = pow2 + pow3;
if (sum >= 5e7)
break;
for (let idx4 = 0; idx4 < primeNumbers.length; ++idx4) {
let pow4 = Math.pow(primeNumbers[idx4], 4);
let sum2 = sum + pow4;
if (sum2 >= 5e7)
break;
nums.add(sum2);
}
}
}
print('Answer: ' + nums.size);

View file

@ -0,0 +1,79 @@
#!/usr/bin/js
/*
Copyright © 2017 Jörg Sommer <joerg@alea.gnuu.de>
License: MIT https://opensource.org/licenses/MIT
*/
'use strict';
//////
//
// Some monkey patching
//
if (typeof putstr === 'undefined')
// in case of node.js
var putstr = process.stdout.write.bind(process.stdout);
if (typeof print === 'undefined')
// in case of node.js
var print = console.log.bind(console);
//////
//
// Solution of Euler 87 https://projecteuler.net/problem=87
//
if (Array.from) {
// node.js
Array.prototype.uniq = function() {
return Array.from(new Set(this));
};
} else {
// Spider monkey
Array.prototype.uniq = new Function('return [...new Set(this)];');
}
// print([1,2,3,2,1].uniq());
Array.prototype.cross = function (other, op) {
let ret = [];
for (let a of this) {
for (let b of other) {
ret.push(op(a, b));
}
}
return ret;
};
// print([1, 2].cross([3, 4], (a, b) => a + b));
let primeNumbers = [2,3,5,7,11,13,17,19];
let limit = 5e7;
for (let num = primeNumbers[primeNumbers.length - 1] + 2;
num <= Math.sqrt(limit); num += 2) {
for (var i = Math.floor(Math.sqrt(num)); i >= 3; --i) {
if (num % i == 0)
break;
}
if (i < 3)
primeNumbers.push(num);
}
print('There are ' + primeNumbers.length + ' prime numbers below sqrt(50.000.000)');
function pnPower(exp) {
return primeNumbers.filter(x => x <= Math.pow(limit, 1 / exp))
.map(x => Math.pow(x, exp));
}
// print(pnPower(4));
let answ = pnPower(2).cross(pnPower(3), (a, b) => a + b)
.filter(x => x < limit)
.cross(pnPower(4), (a, b) => a + b)
.filter(x => x < limit)
.uniq()
.length;
print('Answer: ' + answ);

View file

@ -0,0 +1,95 @@
#!/usr/bin/js
/*
Copyright © 2017 Jörg Sommer <joerg@alea.gnuu.de>
License: MIT https://opensource.org/licenses/MIT
*/
'use strict';
//////
//
// Some monkey patching
//
if (typeof putstr === 'undefined')
// in case of node.js
var putstr = process.stdout.write.bind(process.stdout);
if (typeof print === 'undefined')
// in case of node.js
var print = console.log.bind(console);
//////
//
// Solution of Euler 87 https://projecteuler.net/problem=87 with iterators
//
function* filter(seq, fun) {
for (let e of seq) {
if (fun(e))
yield e;
}
};
// print(Array.from(filter([1,2,3,4], x => x < 3)));
function* cross(seq1, seq2_gen, op) {
if (seq2_gen.next) {
// convert the second sequence into an array, because we have to loop
// over it multiple times, which isn't supported by iterators
let orig_seq = Array.from(seq2_gen);
seq2_gen = () => orig_seq;
}
for (let a of seq1)
for (let b of seq2_gen())
yield op(a,b);
};
// print(Array.from(cross([1, 2], function* () { yield 3; yield 4; }(), (a, b) => a + b)));
let primeNumbers;
!function() {
var cache = [];
primeNumbers = function*() {
yield 2;
let idx;
for (idx = 0; idx < cache.length; ++idx)
yield cache[idx];
for (let num = cache[idx - 1] || 3; ; num += 2) {
for (var i = Math.floor(Math.sqrt(num)); i >= 3; --i) {
if (num % i === 0)
break;
}
if (i < 3) {
cache[idx] = num;
++idx;
yield num;
}
}
};
}();
// print(Array.from(function* () { for (let x of primeNumbers()) { if (x > 50) return; yield x; } }()));
let limit = 5e7;
function* pnPower(exp) {
for (let e of primeNumbers()) {
let x = Math.pow(e, exp);
if (x >= limit)
return;
yield x;
}
}
// print(Array.from(pnPower(4)));
let seq = cross(pnPower(2), pnPower.bind(undefined, 3), (a, b) => a + b);
seq = filter(seq, x => x < limit);
seq = cross(seq, pnPower.bind(undefined, 4), (a, b) => a + b);
seq = filter(seq, x => x < limit);
print('Answer: ' + new Set(seq).size);