If you don't know what Synthetic Division is, you can learn more about it here.

This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.

# [REQUEST]Synthetic Division in CC Lua

Started by KingofGamesYami, 22 October 2015 - 01:46 PMPosted 22 October 2015 - 03:46 PM

I've been trying to do this for 3 or 4 days now, and it's stalling my current project. I would like a function which, when passed a table of arguments in the format [power] = coefficient and a number, it will use synthetic division to create a new table in the same format, which it returns.

If you don't know what Synthetic Division is, you can learn more about it here.

If you don't know what Synthetic Division is, you can learn more about it here.

Posted 23 October 2015 - 03:14 AM

Can you provide sample input/output for this function?

Posted 23 October 2015 - 05:12 AM

Yes, of course!

If that isn't clear enough, please let me know.

```
--[[ just me making an equation up
(x+3)(x^2+3x+1)
x^3+6x^2+9x+3
]]--
--#notice this is in the format ax^b, where tbl[ b ] = a
local input_tbl = {
[0]=3,
9,
6,
1,
}
local input_num = -3 --#negative three is a real solution to the problem; I will always pass it real solutions.
--#some function
local result = {
[0]=1,
3,
1,
}
```

If that isn't clear enough, please let me know.

Edited on 23 October 2015 - 03:16 AM

Posted 23 October 2015 - 10:14 AM

`--[[ just me making an equation up (x+3)(x^2+3x+1) x^3+6x^2+9x+3 ]]--`

Wanted to point out your math is wrong, it should be 10x not 9x.

And here's it implemented in lua: https://gist.github.com/Xtansia/e4e30318b890ea1dfdfe

synthetic_division returns the result polynomial table and the remainder.

Also added a function for converting the polynomial coefficient table into a string of the polynomial.

Posted 23 October 2015 - 01:59 PM

Thanks! The only problem I've discovered is the polynomial_tostring doesn't like it if something is missing.

For example, this table was generated by your polynomial division:

For example, this table was generated by your polynomial division:

```
{
[3] = 1,
[2] = 6,
[0] = -1,
}
```

Edited on 23 October 2015 - 11:59 AM

Posted 23 October 2015 - 02:13 PM

This seems suspiciously like you're trying to get help with school homework!

Posted 23 October 2015 - 02:18 PM

Thanks! The only problem I've discovered is the polynomial_tostring doesn't like it if something is missing.

Oh, right fixed the gist.

Line 25 just needed 'or 0' added to the end, like so:

```
local coeff = polynomial[i] or 0
```

Posted 23 October 2015 - 02:29 PM

This seems suspiciously like you're trying to get help with school homework!

Is it forbidden? As long as we can find a cheap way to connect it to programming.

Edited on 23 October 2015 - 12:29 PM

Posted 23 October 2015 - 05:28 PM

This seems suspiciously like you're trying to get help with school homework!

It's more of a project furthering my own understanding of what I've been taught… I learned synthetic division last year in Honors Algebra II, and I'm now applying it to polynomials in Honors Pre-Calculus.

In case anyone is wondering, my current progress can be found here -> http://pastebin.com/zxtG1xBK

**Edit**: I've now added imaginary solutions! Well, mostly…

**Edit #2**: Why does negative zero exist in lua? Ewwe. Fixed it.

Edited on 23 October 2015 - 04:08 PM

Posted 23 October 2015 - 10:19 PM

I recently did this and because I am becoming increasingly ahead of my Honors class (the highest they offer here as well) and she knows I program my teacher is starting to challenge me to program a TI-83 to do what we do in class.

Also -0 is a thing I noticed. Also Java sometimes likes to pretend nil == 0 which screws things up…

Also -0 is a thing I noticed. Also Java sometimes likes to pretend nil == 0 which screws things up…

Edited on 24 October 2015 - 01:54 AM

Posted 24 October 2015 - 12:34 AM

Add an extra check to work around that:I recently did this and because I am becoming increasingly ahead of my Honors class (the highest they offer here as well) and she knows I program my teacher is starting to challenge me to program a TI-83 to do what we do in class.

Also -0 is a thing I noticed. Also Java sometimes like to pretend nil == 0 which screws things up…

```
x = 0
if x and x == 0 then
Blabla
end
```

My lua is rusty since I was tempting to put a colon for the then, python is my latest interest :P/>Posted 24 October 2015 - 01:49 AM

My Java is rusty, but don't certain number types initialise as 0, not nil? Certainly int etc arrays do.

Back in school they had us graphing quadratics; plotting out points for random values of x and guessing what the line between them looked like didn't sit well with me, so instead I wrote a program to draw the graphs properly and printed the output (which didn't sit well with the teacher).

Back in school they had us graphing quadratics; plotting out points for random values of x and guessing what the line between them looked like didn't sit well with me, so instead I wrote a program to draw the graphs properly and printed the output (which didn't sit well with the teacher).

Posted 24 October 2015 - 03:56 AM

Pneumaticraft has(had) a drone method that grabbed the fuel, and 0 could be 0, -0, nil, and some insanely small number (both positive and negative) which forced me to use some very interesting system checks.