Skip to content

Instantly share code, notes, and snippets.

@knjname
Created October 24, 2018 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knjname/b193aa6d9ae0565c1c25c10719ef1ae5 to your computer and use it in GitHub Desktop.
Save knjname/b193aa6d9ae0565c1c25c10719ef1ae5 to your computer and use it in GitHub Desktop.
const formula = [31,28,31,30,31,30,31,31,30,31,30,31].map((e, m) => {
m++;
const otherMonths = [1,2,3,4,5,6,7,8,9,10,11,12].filter(e => e !== m);
return `${e} * (${otherMonths.map(om => `(x-${om})`).join('*')}) / (${otherMonths.map(om => `(${m}-${om})`).join('*')})`
}).join("\n + ")
// 31 * ((x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((1-2)*(1-3)*(1-4)*(1-5)*(1-6)*(1-7)*(1-8)*(1-9)*(1-10)*(1-11)*(1-12))
// + 28 * ((x-1)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((2-1)*(2-3)*(2-4)*(2-5)*(2-6)*(2-7)*(2-8)*(2-9)*(2-10)*(2-11)*(2-12))
// + 31 * ((x-1)*(x-2)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((3-1)*(3-2)*(3-4)*(3-5)*(3-6)*(3-7)*(3-8)*(3-9)*(3-10)*(3-11)*(3-12))
// + 30 * ((x-1)*(x-2)*(x-3)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((4-1)*(4-2)*(4-3)*(4-5)*(4-6)*(4-7)*(4-8)*(4-9)*(4-10)*(4-11)*(4-12))
// + 31 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((5-1)*(5-2)*(5-3)*(5-4)*(5-6)*(5-7)*(5-8)*(5-9)*(5-10)*(5-11)*(5-12))
// + 30 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((6-1)*(6-2)*(6-3)*(6-4)*(6-5)*(6-7)*(6-8)*(6-9)*(6-10)*(6-11)*(6-12))
// + 31 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-8)*(x-9)*(x-10)*(x-11)*(x-12)) / ((7-1)*(7-2)*(7-3)*(7-4)*(7-5)*(7-6)*(7-8)*(7-9)*(7-10)*(7-11)*(7-12))
// + 31 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-9)*(x-10)*(x-11)*(x-12)) / ((8-1)*(8-2)*(8-3)*(8-4)*(8-5)*(8-6)*(8-7)*(8-9)*(8-10)*(8-11)*(8-12))
// + 30 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-10)*(x-11)*(x-12)) / ((9-1)*(9-2)*(9-3)*(9-4)*(9-5)*(9-6)*(9-7)*(9-8)*(9-10)*(9-11)*(9-12))
// + 31 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-11)*(x-12)) / ((10-1)*(10-2)*(10-3)*(10-4)*(10-5)*(10-6)*(10-7)*(10-8)*(10-9)*(10-11)*(10-12))
// + 30 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-12)) / ((11-1)*(11-2)*(11-3)*(11-4)*(11-5)*(11-6)*(11-7)*(11-8)*(11-9)*(11-10)*(11-12))
// + 31 * ((x-1)*(x-2)*(x-3)*(x-4)*(x-5)*(x-6)*(x-7)*(x-8)*(x-9)*(x-10)*(x-11)) / ((12-1)*(12-2)*(12-3)*(12-4)*(12-5)*(12-6)*(12-7)*(12-8)*(12-9)*(12-10)*(12-11))
const guessMaxDayOfMonth = eval(`(x) => ${formula}`)
// guessMaxDayOfMonth(1) => 31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment