Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
var eulers_sum_of_powers = function (iMaxN) {
|
||||
|
||||
var aPow5 = [];
|
||||
var oPow5ToN = {};
|
||||
|
||||
for (var iP = 0; iP <= iMaxN; iP++) {
|
||||
var iPow5 = Math.pow(iP, 5);
|
||||
aPow5.push(iPow5);
|
||||
oPow5ToN[iPow5] = iP;
|
||||
}
|
||||
|
||||
for (var i0 = 1; i0 <= iMaxN; i0++) {
|
||||
for (var i1 = 1; i1 <= i0; i1++) {
|
||||
for (var i2 = 1; i2 <= i1; i2++) {
|
||||
for (var i3 = 1; i3 <= i2; i3++) {
|
||||
var iPow5Sum = aPow5[i0] + aPow5[i1] + aPow5[i2] + aPow5[i3];
|
||||
if (typeof oPow5ToN[iPow5Sum] != 'undefined') {
|
||||
return {
|
||||
i0: i0,
|
||||
i1: i1,l
|
||||
i2: i2,
|
||||
i3: i3,
|
||||
iSum: oPow5ToN[iPow5Sum]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var oResult = eulers_sum_of_powers(250);
|
||||
|
||||
console.log(oResult.i0 + '^5 + ' + oResult.i1 + '^5 + ' + oResult.i2 +
|
||||
'^5 + ' + oResult.i3 + '^5 = ' + oResult.iSum + '^5');
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
var N=1000, first=false
|
||||
var ns={}, npv=[]
|
||||
for (var n=0; n<=N; n++) {
|
||||
var np=Math.pow(n,5); ns[np]=n; npv.push(np)
|
||||
}
|
||||
loop:
|
||||
for (var a=1; a<=N; a+=1)
|
||||
for (var b=a+1; b<=N; b+=1)
|
||||
for (var c=b+1; c<=N; c+=1)
|
||||
for (var d=c+1; d<=N; d+=1) {
|
||||
var x = ns[ npv[a]+npv[b]+npv[c]+npv[d] ]
|
||||
if (!x) continue
|
||||
print( [a, b, c, d, x] )
|
||||
if (first) break loop
|
||||
}
|
||||
function print(c) {
|
||||
var e='<sup>5</sup>', ep=e+' + '
|
||||
document.write(c[0], ep, c[1], ep, c[2], ep, c[3], e, ' = ', c[4], e, '<br>')
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
var N=1000, first=false
|
||||
var npv=[], M=30 // x^5 == x modulo M (=2*3*5)
|
||||
for (var n=0; n<=N; n+=1) npv[n]=Math.pow(n, 5)
|
||||
var mx=1+npv[N]; while(n<=N+M) npv[n++]=mx
|
||||
|
||||
loop:
|
||||
for (var a=1; a<=N; a+=1)
|
||||
for (var b=a+1; b<=N; b+=1)
|
||||
for (var c=b+1; c<=N; c+=1)
|
||||
for (var t=npv[a]+npv[b]+npv[c], d=c+1, x=t%M+d; (n=t+npv[d])<mx; d+=1, x+=1) {
|
||||
while (npv[x]<=n) x+=M; x-=M // jump over M=30 values for x>d
|
||||
if (npv[x] != n) continue
|
||||
print( [a, b, c, d, x] )
|
||||
if (first) break loop;
|
||||
}
|
||||
function print(c) {
|
||||
var e='<sup>5</sup>', ep=e+' + '
|
||||
document.write(c[0], ep, c[1], ep, c[2], ep, c[3], e, ' = ', c[4], e, '<br>')
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
var N=1000, first=false
|
||||
var dxs={}, pow=Math.pow
|
||||
for (var d=1; d<=N; d+=1)
|
||||
for (var dp=pow(d,5), x=d+1; x<=N; x+=1)
|
||||
dxs[pow(x,5)-dp]=[d,x]
|
||||
loop:
|
||||
for (var a=1; a<N; a+=1)
|
||||
for (var ap=pow(a,5), b=a+1; b<N; b+=1)
|
||||
for (var abp=ap+pow(b,5), c=b+1; c<N; c+=1) {
|
||||
var dx = dxs[ abp+pow(c,5) ]
|
||||
if (!dx || c >= dx[0]) continue
|
||||
print( [a, b, c].concat( dx ) )
|
||||
if (first) break loop
|
||||
}
|
||||
function print(c) {
|
||||
var e='<sup>5</sup>', ep=e+' + '
|
||||
document.write(c[0], ep, c[1], ep, c[2], ep, c[3], e, ' = ', c[4], e, '<br>')
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
var N=1000, first=false
|
||||
var is={}, ipv=[], ijs={}, ijpv=[], pow=Math.pow
|
||||
for (var i=1; i<=N; i+=1) {
|
||||
var ip=pow(i,5); is[ip]=i; ipv.push(ip)
|
||||
for (var j=i+1; j<=N; j+=1) {
|
||||
var ijp=ip+pow(j,5); ijs[ijp]=[i,j]; ijpv.push(ijp)
|
||||
}
|
||||
}
|
||||
ijpv.sort( function (a,b) {return a - b } )
|
||||
loop:
|
||||
for (var i=0, ei=ipv.length; i<ei; i+=1)
|
||||
for (var xp=ipv[i], j=0, je=ijpv.length; j<je; j+=1) {
|
||||
var cdp = ijpv[j]
|
||||
if (cdp >= xp) break
|
||||
var cd = ijs[xp-cdp]
|
||||
if (!cd) continue
|
||||
var ab = ijs[cdp]
|
||||
if (ab[1] >= cd[0]) continue
|
||||
print( [].concat(ab, cd, is[xp]) )
|
||||
if (first) break loop
|
||||
}
|
||||
function print(c) {
|
||||
var e='<sup>5</sup>', ep=e+' + '
|
||||
document.write(c[0], ep, c[1], ep, c[2], ep, c[3], e, ' = ', c[4], e, '<br>')
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
const eulersSumOfPowers = intMax => {
|
||||
const
|
||||
pow = Math.pow,
|
||||
xs = range(0, intMax)
|
||||
.map(x => pow(x, 5)),
|
||||
dct = xs.reduce((a, x, i) =>
|
||||
(a[x] = i,
|
||||
a
|
||||
), {});
|
||||
|
||||
for (let a = 1; a <= intMax; a++) {
|
||||
for (let b = 2; b <= a; b++) {
|
||||
for (let c = 3; c <= b; c++) {
|
||||
for (let d = 4; d <= c; d++) {
|
||||
const sumOfPower = dct[xs[a] + xs[b] + xs[c] + xs[d]];
|
||||
if (sumOfPower !== undefined) {
|
||||
return [a, b, c, d, sumOfPower];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
const range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// TEST
|
||||
const soln = eulersSumOfPowers(250);
|
||||
return soln ? soln.slice(0, 4)
|
||||
.map(x => `${x}^5`)
|
||||
.join(' + ') + ` = ${soln[4]}^5` : 'No solution found.'
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,246 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
const main = () => {
|
||||
|
||||
const
|
||||
iFrom = 1,
|
||||
iTo = 249,
|
||||
xs = enumFromTo(1, 249),
|
||||
p5 = x => Math.pow(x, 5);
|
||||
|
||||
const
|
||||
// powerMap :: Dict Int Int
|
||||
powerMap = mapFromList(
|
||||
zip(map(p5, xs), xs)
|
||||
),
|
||||
// sumMap :: Dict Int (Int, Int)
|
||||
sumMap = mapFromList(
|
||||
bind(
|
||||
xs,
|
||||
x => bind(
|
||||
tail(xs),
|
||||
y => Tuple(
|
||||
p5(x) + p5(y),
|
||||
Tuple(x, y)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// mbExample :: Maybe (Int, Int)
|
||||
const mbExample = find(
|
||||
tpl => member(fst(tpl) - snd(tpl), sumMap),
|
||||
bind(
|
||||
map(x => parseInt(x, 10),
|
||||
keys(powerMap)
|
||||
),
|
||||
p => bind(
|
||||
takeWhile(
|
||||
x => x < p,
|
||||
map(x => parseInt(x, 10),
|
||||
keys(sumMap)
|
||||
)
|
||||
),
|
||||
s => [Tuple(p, s)]
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// showExample :: (Int, Int) -> String
|
||||
const showExample = tpl => {
|
||||
const [p, s] = Array.from(tpl);
|
||||
const [a, b] = Array.from(sumMap[p - s]);
|
||||
const [c, d] = Array.from(sumMap[s]);
|
||||
return 'Counter-example found:\n' + intercalate(
|
||||
'^5 + ',
|
||||
map(str, [a, b, c, d])
|
||||
) + '^5 = ' + str(powerMap[p]) + '^5';
|
||||
};
|
||||
|
||||
return maybe(
|
||||
'No counter-example found',
|
||||
showExample,
|
||||
mbExample
|
||||
);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Just :: a -> Maybe a
|
||||
const Just = x => ({
|
||||
type: 'Maybe',
|
||||
Nothing: false,
|
||||
Just: x
|
||||
});
|
||||
|
||||
// Nothing :: Maybe a
|
||||
const Nothing = () => ({
|
||||
type: 'Maybe',
|
||||
Nothing: true,
|
||||
});
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = (a, b) => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// bind (>>=) :: [a] -> (a -> [b]) -> [b]
|
||||
const bind = (xs, mf) => [].concat.apply([], xs.map(mf));
|
||||
|
||||
// concat :: [[a]] -> [a]
|
||||
// concat :: [String] -> String
|
||||
const concat = xs =>
|
||||
0 < xs.length ? (() => {
|
||||
const unit = 'string' !== typeof xs[0] ? (
|
||||
[]
|
||||
) : '';
|
||||
return unit.concat.apply(unit, xs);
|
||||
})() : [];
|
||||
|
||||
|
||||
// enumFromTo :: (Int, Int) -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// find :: (a -> Bool) -> [a] -> Maybe a
|
||||
const find = (p, xs) => {
|
||||
for (let i = 0, lng = xs.length; i < lng; i++) {
|
||||
if (p(xs[i])) return Just(xs[i]);
|
||||
}
|
||||
return Nothing();
|
||||
};
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = tpl => tpl[0];
|
||||
|
||||
// intercalate :: [a] -> [[a]] -> [a]
|
||||
// intercalate :: String -> [String] -> String
|
||||
const intercalate = (sep, xs) =>
|
||||
0 < xs.length && 'string' === typeof sep &&
|
||||
'string' === typeof xs[0] ? (
|
||||
xs.join(sep)
|
||||
) : concat(intersperse(sep, xs));
|
||||
|
||||
// intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3]
|
||||
|
||||
// intersperse :: a -> [a] -> [a]
|
||||
// intersperse :: Char -> String -> String
|
||||
const intersperse = (sep, xs) => {
|
||||
const bln = 'string' === typeof xs;
|
||||
return xs.length > 1 ? (
|
||||
(bln ? concat : x => x)(
|
||||
(bln ? (
|
||||
xs.split('')
|
||||
) : xs)
|
||||
.slice(1)
|
||||
.reduce((a, x) => a.concat([sep, x]), [xs[0]])
|
||||
)) : xs;
|
||||
};
|
||||
|
||||
// keys :: Dict -> [String]
|
||||
const keys = Object.keys;
|
||||
|
||||
// Returns Infinity over objects without finite length.
|
||||
// This enables zip and zipWith to choose the shorter
|
||||
// argument when one is non-finite, like cycle, repeat etc
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs =>
|
||||
(Array.isArray(xs) || 'string' === typeof xs) ? (
|
||||
xs.length
|
||||
) : Infinity;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) =>
|
||||
(Array.isArray(xs) ? (
|
||||
xs
|
||||
) : xs.split('')).map(f);
|
||||
|
||||
// mapFromList :: [(k, v)] -> Dict
|
||||
const mapFromList = kvs =>
|
||||
kvs.reduce(
|
||||
(a, kv) => {
|
||||
const k = kv[0];
|
||||
return Object.assign(a, {
|
||||
[
|
||||
(('string' === typeof k) && k) || JSON.stringify(k)
|
||||
]: kv[1]
|
||||
});
|
||||
}, {}
|
||||
);
|
||||
|
||||
// Default value (v) if m.Nothing, or f(m.Just)
|
||||
|
||||
// maybe :: b -> (a -> b) -> Maybe a -> b
|
||||
const maybe = (v, f, m) =>
|
||||
m.Nothing ? v : f(m.Just);
|
||||
|
||||
// member :: Key -> Dict -> Bool
|
||||
const member = (k, dct) => k in dct;
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// str :: a -> String
|
||||
const str = x => x.toString();
|
||||
|
||||
// tail :: [a] -> [a]
|
||||
const tail = xs => 0 < xs.length ? xs.slice(1) : [];
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = (n, xs) =>
|
||||
'GeneratorFunction' !== xs.constructor.constructor.name ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
// takeWhile :: (a -> Bool) -> [a] -> [a]
|
||||
// takeWhile :: (Char -> Bool) -> String -> String
|
||||
const takeWhile = (p, xs) =>
|
||||
xs.constructor.constructor.name !==
|
||||
'GeneratorFunction' ? (() => {
|
||||
const lng = xs.length;
|
||||
return 0 < lng ? xs.slice(
|
||||
0,
|
||||
until(
|
||||
i => lng === i || !p(xs[i]),
|
||||
i => 1 + i,
|
||||
0
|
||||
)
|
||||
) : [];
|
||||
})() : takeWhileGen(p, xs);
|
||||
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
const until = (p, f, x) => {
|
||||
let v = x;
|
||||
while (!p(v)) v = f(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
// Use of `take` and `length` here allows for zipping with non-finite
|
||||
// lists - i.e. generators like cycle, repeat, iterate.
|
||||
|
||||
// zip :: [a] -> [b] -> [(a, b)]
|
||||
const zip = (xs, ys) => {
|
||||
const lng = Math.min(length(xs), length(ys));
|
||||
return Infinity !== lng ? (() => {
|
||||
const bs = take(lng, ys);
|
||||
return take(lng, xs).map((x, i) => Tuple(x, bs[i]));
|
||||
})() : zipGen(xs, ys);
|
||||
};
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue