Skip to content

Instantly share code, notes, and snippets.

@mharju
Last active December 22, 2015 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharju/6438996 to your computer and use it in GitHub Desktop.
Save mharju/6438996 to your computer and use it in GitHub Desktop.
Splits the array to parts of equal lengths, len = 3,4,5,...,35 and searches that space for numbers N satisfying N=x(x+1)(x+2)(x+3)(x+4).
var find_greatest = function(input) {
var check = function(n) {
if(n==0) { return false; }
// n is of form x^5 + O(x^4), so we can pick it by taking the
// fifth root and taking the extra crust off
var x = Math.floor( Math.pow( n, 0.2 ) ) - 1
return n === x*(x+1)*(x+2)*(x+3)*(x+4);
}
var slice_n = function(n,x) {
return _(_.range(0,Math.ceil(x.length / n))
.map(function(i) {
return _.partial(function(b, c, a) {
return a.slice(b, c);
}, n*i, n*i+n);
})).map(function(f) {
return f(x)
});
}
var check_all = function(x) {
// find only numbers that are less than 35 digits long
var slicers = _.range(3, 36)
.map(function(n) {
return _.partial(slice_n, n);
}),
makeInt = function(x) {
return _(x).map(function(n) { return parseInt(n.replace(/^0*?(\d*)$/, '$1')); });
};
return _.filter(
_.compose(
_.uniq,
_.sortBy,
makeInt,
_.flatten)(_(slicers).map(function(slicer) { return slicer(x); })), check);
};
return _.max(
check_all(input)
);
}
find_greatest('144247417057866628561931623430668801207202520672015120302405154409504015444012072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151212312312312312334743784789454578954798451928730302405544095040154441207202520672015120302405544095040154144247299140106872273666980203186723594537960441207202520672015130240554409504015444120720252067201512030240554409504015444120720252067201512030240554409504015444120720252067201512030240554409504015143985091891289122144412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544412072025206720151203024055440950401544400000000000000000000000120720252067201512030240554409504015444014424741705786662856193162343066880')
// 1.4424741705786662e+34
// = 6789245 * 6789246 * 6789247 * 6789248 * 6789249
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment