Created
October 29, 2016 16:49
-
-
Save zmitton/e4126290f3eb7064919452e2a9683760 to your computer and use it in GitHub Desktop.
This file contains 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
// 4 1 1 1 | |
// - = - + - + - | |
// n a b c | |
// 5: | |
// 4/5 = 1/2 + 1/5 + 1/10 | |
// 6: | |
// 2/3 = 1/3 + 1/6 + 1/6 | |
// 7: | |
// 4/7 = 1/2 + 1/28 + 1/28 | |
// 8: | |
// 1/2 = 1/4 + 1/8 + 1/8 | |
// 9: | |
// 4/9 = 1/3 + 1/18 + 1/18 | |
// 10: | |
// 2/5 = 1/5 + 1/10 + 1/10 | |
// 11: | |
// 4/11 = 1/3 + 1/66 + 1/66? | |
// try half, third, fourth... | |
// with each try, equate them w common denominator | |
// until either: you hit target: | |
// use 1/2 the value if its a. and 1/4, 1/4; or if its b, 1/2, 1/2; c, just use the value | |
// OR: value is slightly less then target. | |
// use that. and repeat above | |
// 12: | |
// 4/12 = 1/6 + 1/12 + 1/12 | |
// 13: | |
// 4/13 = 1/4 | |
// 16/52= 13/52 + 1/26 + 1/52 | |
// 2/52 | |
// 14: | |
// 4/14 = 1/4 + 1/56 + 1/56 | |
// 8/28 = 7/28 | |
// 15: | |
// 4/15 = 1/4 + 1/120 + 1/120 | |
// 16/60= 15/60 | |
// 16: | |
// 4/16 = 1/8 + 1/16 + 1/16 | |
// 17: | |
// 4/17 = 1/5 | |
// 20/85 = 17/85 + | |
// 3/85 = 1/29 | |
// 87/2465 = 85/2465 + | |
// 2/2465 = | |
// 17: | |
// 4/17 = 1/6 | |
// 24/102 = 17/102 + | |
// 7/102 = 1/29 | |
// 87/2465 = 102/2465 + | |
// 2/2465 = ????? | |
// cant find a solution. googled it, found Erdős–Straus conjecture. Looks like something | |
// that holds true up to 10^17 but nobody has proven why. I don't expect to beat them in | |
// 30 minutes, so I'll code up my algorithm that works up to 16 | |
for(var n = 5 ; n <= 8 ; n++){ | |
var a = 1; | |
var b = 1; | |
var c = 1; | |
var numerator = 1; | |
var denominator = n; | |
var a = geta(numerator, denominator); | |
console.log("SOLUTIONS:", n,a,b,c); | |
} | |
function geta(numerator, denominator){ | |
var remainder = numerator/denominator; | |
var a = 1 | |
while( 1/a < remainder){ | |
// if(a != denominator){ | |
// var commonDenom = lcm_two_numbers(a, denominator) | |
// a = numerator * (commonDenom/denominator) | |
// a = numerator * (a/denominator) | |
// } | |
// iFactors = primeFactorization(i); | |
// denomFactors = primeFactorization(denominator) | |
// if(1/i < ) | |
a++; | |
} | |
if(1/a == remainder){ | |
return a/2 | |
} | |
return a | |
} | |
function primeFactorization(num){ | |
var root = Math.sqrt(num), | |
result = arguments[1] || [], //get unnamed paremeter from recursive calls | |
x = 2; | |
if(num % x){//if not divisible by 2 | |
x = 3;//assign first odd | |
while((num % x) && ((x = x + 2) < root)){}//iterate odds | |
} | |
//if no factor found then num is prime | |
x = (x <= root) ? x : num; | |
result.push(x);//push latest prime factor | |
//if num isn't prime factor make recursive call | |
return (x === num) ? result : primeFactorization(num/x, result) ; | |
} | |
function lcm_two_numbers(x, y) { | |
if ((typeof x !== 'number') || (typeof y !== 'number')) | |
return false; | |
return (!x || !y) ? 0 : Math.abs((x * y) / gcd_two_numbers(x, y)); | |
} | |
function gcd_two_numbers(x, y) { | |
x = Math.abs(x); | |
y = Math.abs(y); | |
while(y) { | |
var t = y; | |
y = x % y; | |
x = t; | |
} | |
return x; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment