This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PwnCC's profile picture

Question about formulas

Started by PwnCC, 03 November 2017 - 10:08 AM
PwnCC #1
Posted 03 November 2017 - 11:08 AM
Hello! I had a question on how to resolve an integer in lua.
For example if I say
a = 6
a = 3 x b
How would I find the value for b in Lua?
KingofGamesYami #2
Posted 03 November 2017 - 11:43 AM
Programs do not work that way.
PwnCC #3
Posted 03 November 2017 - 11:58 AM
There must be a way
SquidDev #4
Posted 03 November 2017 - 12:01 PM
So it is possible to solve such equations by putting them in a matrix and using Gaussian elimination. In your above example, you'd represent the equations as a matrix:

#  a  |  b  |  result
# ----|-----|---------
#  1  |  0  |  6	   (a = 6)
# -1  |  3  |  0	   (0 = -a + 3b)
Basically each column corresponds to a variable's coefficients, and each row corresponds to an equation. You want to reduce your matrix to reduced row echelon form, by adding or subtracting different rows together. If you add the first row to the second row, you get the following:

#  a  |  b  |  result
# ----|-----|---------
#  1  |  0  |  6	   (a = 6)
#  0  |  3  |  6	  (3b = 6)
We can then divide the second row by 3 (or more generally, the value in the second column) to get the "solution":

#  a  |  b  |  result
# ----|-----|---------
#  1  |  0  |  6	   (a = 6)
#  0  |  1  |  2	   (b = 2)
The Gaussian Elimination wikipedia page has some pseudocode for an algorithmic way to reduce your matrix. You may also wish to look at Urn's implementation of such an algorithm.

Note, if you're trying to save 10 minutes in your homework this isn't going to be super efficient. If you're just interested, I'd definitely recommend implementing something like this.
Edited on 03 November 2017 - 11:02 AM
PwnCC #5
Posted 03 November 2017 - 12:26 PM
It's not for homework, in fact it's for a new crypto currency. No spoilers