-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_from_outputs.js
148 lines (118 loc) · 3.75 KB
/
polynomial_from_outputs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const { SlashCommandBuilder } = require('discord.js')
function factorial(n) {
a = 1
for (let i = 1; i <= n; i++) {
a *= i
}
return a
}
function allEqualInArray(inputArray) {
for (let i = 1; i < inputArray.length; i++) {
if (inputArray[i] !== inputArray[i - 1]) {
return false
}
}
return true
}
function formatEquation(equation) {
equation = equation
.replace(/ /g, '')
.replace(/-/g, '+-')
let eqnt = ''
for (let term of equation.split('+')) {
if (term.split('*')[0] !== '0') {
eqnt += `+${term}`
}
}
equation = eqnt.slice(1)
equation = equation
.replace(/ /g, '')
.replace(/\+\-/g, '-')
.replace(/\*/g, ' * ')
.replace(/ \* \* /g, ' ** ')
.replace(/\+/g, ' + ')
.replace(/\//g, ' / ')
.replace(/-/g, ' - ')
while (equation[0] === ' ') {
equation = equation.slice(1)
}
if (equation[0] === '+') {
equation = equation.slice(2)
}
if (equation[0] === '-') {
equation = '-' + equation.slice(2)
}
return equation
}
function findFormula(inputs) {
function recursiveGet(inputValues) {
let polynomialDegree = 0
let tList = inputValues
while (!allEqualInArray(tList)) {
let t2List = []
polynomialDegree++
for (let i = 1; i < tList.length; i++) {
t2List.push(tList[i] - tList[i - 1])
}
tList = t2List
}
if (polynomialDegree === 0) {
return ''
}
let commonAdditive = tList[0]
let coefficient = Math.round(commonAdditive / factorial(polynomialDegree) * 1000) / 1000
let equation = `+ ${coefficient !== 1 ? coefficient + '*' : ''} x ${polynomialDegree !== 1 ? '** ' + polynomialDegree.toString() : ''} `
let newValues = []
for (let i = 0; i < inputValues.length - 1; i++) {
let eqnt = equation.replaceAll(' ', '').replaceAll('x', `(${i + 1})`)
eqnt = eqnt.substring(1)
newValues.push(inputValues[i] - eval(eqnt))
}
if (allEqualInArray(newValues)) {
return equation
} else {
return equation + recursiveGet(newValues)
}
}
let result = recursiveGet(inputs)
.replaceAll(' ', '')
.slice(1)
if (result.length === 0) {
result = '0 * x'
}
let constant = inputs[0] - eval(result.replaceAll('x', '(1)'))
if (parseInt(constant) === 0) {
return formatEquation(result)
}
return formatEquation(`${result !== '0 * x' ? result : ''} + ${constant}`)
}
module.exports = {
data: new SlashCommandBuilder()
.setName('polynomial_from_outputs')
.setDescription('Create a polynomial from the given inputs')
.addStringOption((option) => option
.setName('outputs')
.setDescription('The outputs produced by the polynomial, p(x), from p(1) to p(n)')
.setRequired(true)
),
async execute(interaction) {
let outputs = interaction.options.getString('outputs')
.replaceAll(' ', '')
.split(',')
outputs = outputs.map(str => {
return parseInt(str, 10);
})
let final_formula = findFormula(outputs)
let formula_ans = '```' + 'js\n' + 'f(x) = ' + final_formula + '```'
let formula_tests = '```js\nNOTE: May have slight inaccuracies\n\n'
for (let i = 1; i <= outputs.length; i++) {
formula_tests += `f(${i}) = ${Math.round(eval(final_formula.replaceAll('x', `(${i})`)) * 10) / 10}\n`
}
formula_tests += '```'
if (formula_ans.length + formula_tests.length > 2000) {
await interaction.reply(formula_ans)
} else {
await interaction.reply(formula_ans + formula_tests)
}
}
}