Created
July 19, 2015 10:36
-
-
Save anonymous/85af55412756fbc12415 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/yetubi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/** | |
* This script finds 4 whole number values that have a specific relationship. | |
* Specifically, the sum of the squares of 3 numbers when they equal the | |
* square root of the 4th number. In linear algebra terms, this calculates | |
* the absolute value of a vector [x, y, z] when all values and the result | |
* are whole numbers. This formula is also known to calculate the distance | |
* of a 3D point from zero: d = sqrt(x*x + y*y + z*z) | |
* | |
* I recognize it's totally silly, but I wrote this because I was writing a | |
* unit test for a function that performs this calculation and I wanted my | |
* test cases to be whole numbers. This is how I found them. | |
* | |
* skip controls the smallest value in the vector | |
* start and stop control the range of distance, d | |
*/ | |
'use strict'; | |
console.clear(); | |
var skip = 2; | |
var start = 9; | |
var stop = 100; | |
var getNext = function getNext(_x, _x2, _x3) { | |
var _again = true; | |
_function: while (_again) { | |
var arr = _x, | |
target = _x2, | |
result = _x3; | |
current = next = slice = undefined; | |
_again = false; | |
if (arr.length === 0) return result; | |
var current = _.last(arr); | |
result.push(current); | |
var next = target - current; | |
var slice = _.filter(arr, function (i) { | |
return i <= next; | |
}); | |
_x = slice; | |
_x2 = next; | |
_x3 = result; | |
_again = true; | |
continue _function; | |
} | |
}; | |
var getAnswer = function getAnswer(num) { | |
var target = Math.pow(num, 2); | |
var square = function square(val) { | |
return Math.pow(val, 2); | |
}; | |
var squares = _.range(skip, num).map(square); | |
var maybe = getNext(squares, target, []); | |
var check = _.reduce(maybe, function (total, i) { | |
return total + i; | |
}); | |
if (maybe.length === 3 && check === target) { | |
console.log(num + ': ' + _.map(maybe, Math.sqrt)); | |
} | |
}; | |
_.range(start, stop).map(getAnswer); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/** | |
* This script finds 4 whole number values that have a specific relationship. | |
* Specifically, the sum of the squares of 3 numbers when they equal the | |
* square root of the 4th number. In linear algebra terms, this calculates | |
* the absolute value of a vector [x, y, z] when all values and the result | |
* are whole numbers. This formula is also known to calculate the distance | |
* of a 3D point from zero: d = sqrt(x*x + y*y + z*z) | |
* | |
* I recognize it's totally silly, but I wrote this because I was writing a | |
* unit test for a function that performs this calculation and I wanted my | |
* test cases to be whole numbers. This is how I found them. | |
* | |
* skip controls the smallest value in the vector | |
* start and stop control the range of distance, d | |
*/ | |
console.clear() | |
const skip = 2 | |
const start = 9 | |
const stop = 100 | |
var getNext = (arr, target, result) => { | |
if(arr.length === 0) return result | |
const current = _.last(arr) | |
result.push(current) | |
const next = target - current | |
const slice = _.filter(arr, (i) => { return i <= next }) | |
return getNext(slice, next, result) | |
} | |
var getAnswer = (num) => { | |
const target = Math.pow(num, 2) | |
const square = (val) => { return Math.pow(val, 2) } | |
const squares = _.range(skip, num).map(square) | |
const maybe = getNext(squares, target, []) | |
const check = _.reduce(maybe, (total, i) => total + i) | |
if(maybe.length === 3 && check === target) { | |
console.log(num + ': '+ _.map(maybe, Math.sqrt)) | |
} | |
} | |
_.range(start, stop).map(getAnswer) | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This script finds 4 whole number values that have a specific relationship. | |
* Specifically, the sum of the squares of 3 numbers when they equal the | |
* square root of the 4th number. In linear algebra terms, this calculates | |
* the absolute value of a vector [x, y, z] when all values and the result | |
* are whole numbers. This formula is also known to calculate the distance | |
* of a 3D point from zero: d = sqrt(x*x + y*y + z*z) | |
* | |
* I recognize it's totally silly, but I wrote this because I was writing a | |
* unit test for a function that performs this calculation and I wanted my | |
* test cases to be whole numbers. This is how I found them. | |
* | |
* skip controls the smallest value in the vector | |
* start and stop control the range of distance, d | |
*/ | |
'use strict'; | |
console.clear(); | |
var skip = 2; | |
var start = 9; | |
var stop = 100; | |
var getNext = function getNext(_x, _x2, _x3) { | |
var _again = true; | |
_function: while (_again) { | |
var arr = _x, | |
target = _x2, | |
result = _x3; | |
current = next = slice = undefined; | |
_again = false; | |
if (arr.length === 0) return result; | |
var current = _.last(arr); | |
result.push(current); | |
var next = target - current; | |
var slice = _.filter(arr, function (i) { | |
return i <= next; | |
}); | |
_x = slice; | |
_x2 = next; | |
_x3 = result; | |
_again = true; | |
continue _function; | |
} | |
}; | |
var getAnswer = function getAnswer(num) { | |
var target = Math.pow(num, 2); | |
var square = function square(val) { | |
return Math.pow(val, 2); | |
}; | |
var squares = _.range(skip, num).map(square); | |
var maybe = getNext(squares, target, []); | |
var check = _.reduce(maybe, function (total, i) { | |
return total + i; | |
}); | |
if (maybe.length === 3 && check === target) { | |
console.log(num + ': ' + _.map(maybe, Math.sqrt)); | |
} | |
}; | |
_.range(start, stop).map(getAnswer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment