It looks like you're new here. If you want to get involved, click one of these buttons!
Hey Codea,
I'm attempting to code a program that will factor polynomials. Firstly, though, I have created a program that finds the factors of the inputted number. If you know how to factor, this basically will be part of the "x-box" method. This is just the beginning of a big project I have in mind.
-- Factor
function setup()
parameter.integer("n", 0, 300, 0)
parameter.action("Factors", factor)
print("Factors of "..n.." are: ")
factorsList = { }
end
function factor()
i = 1
while(i <= n) do
if(n%i==0) then
print(i)
table.insert(factorsList, i)
end
i = i + 1
end
drawFactors = true
end
function draw()
background(40, 40, 50)
fill(172, 22, 22, 255)
rect(WIDTH/4, HEIGHT/4, 500, 400)
fill(255, 255, 255, 255)
fontSize(50)
text(n, WIDTH/2+50, 550)
for i = 1, #factorsList do
text(factorsList[i], WIDTH/2-50, HEIGHT/2)
end
end
I would like to draw the factors in a list format (with commas and such), however, they are overlapping each other. Also, in the print command, I want to have it in pairs. I'm trying to do for i,v in pairs(factorsList) do print(i,v) end
but v doesn't have any value. Is there a way to have pairs with a single variable like i? So if the input was 10, the output (print and/or text) should read 1 10, 2 5.
Comments
@Zacharias Here's your code modified a little.
@dave1707 Thanks a lot! It serves its purpose well. Now I just have to figure how to use this with factoring polynomials. Should be fun
@Zacharias - your loop only needs to run from 2 (1 is always a factor) to the square root of n. That should make it much faster.
For example, take the number 24. Loop from 2 to 4 (integer of square root of 5).
2 is a factor, so 12 must be as well (since 24/2=12).
3 is a factor, so 8 must be too.
4 is a factor, so 6 must be too
Factors are 1, 2, 3, 4, 6, 8, 12
NB if you are going to want to factor primes, there are faster methods.
@Zacharias I believe a good way to do is something I learned last year in math class, I don't know if it is the best way or it even works, but search up the Gausse-Jordan method. It deals with matrices and solving and factoring polynomials. That's how I know graphing calculators are able to do that.
@Ignatz I see what you mean. When I figure out how to do the rest of the factoring and what not, I'll come back and optimize it.
@CamelCoder Thanks for the suggestion. It looks complex, and I'd like to use my already obtained knowledge of math for what I'm coding. Feel free to try it out, though.
[resolved]
[resolved]
Going back to Ignatz's idea, I'd go from 2 up to the square root on n, which gives you the first half of the factors, and then take that list and work backwards through it, dividing n by each factor, to get the second half of the list. Much, much faster than going all the way up to n if n is large.
So I added pseudo code to clarify parts and added some aesthetic (not much) to the program. I'm confused with changing the loop to 2, math.sqrt(ac) and what to do inside, so I left it for later. Here is what I have:
I also need to do complex polynomial so where a > 1. That program can only solve simple polynomials.
I was sort of getting a headache from making my game, i really do hate 3D. I thought I would take a break and make my own version of this calculator. I added a UI just because I didn't like the parameters. The UI is sort of bad because I in all spent about an hour on this program, so it will probably have errors. I would like to post the code, but I do not know how to post code on this forum. Can u please tell me what to do so I can post it
@CamelCoder To post code in the forum, just select all of your code then copy it. After that, just paste the code in the forum. Put 3 ~'s on a line before and after your code so it formats correctly.
@CamelCoder If your code is too big then you can post it in github, or split your code into 2 or more posts.
@CamelCoder - but there is little point in posting your code if it is rushed and has errors and doesn't work properly. Why would we want to look at it?
Here's an example that uses the quadratic equation to solve for X. Just change the X2, X1, X0 values and see the results as you change the values. An example is shown when the program is started.
EDIT: Corrected an error.
@Ignatz Although it was rushed, I found it to have few errors. I don't know the cause because I didn't look into it that much. It still works great for the most part. [Edit] I fixed the error it had, now I hope you think it is worth looking at.
Here is the first part:
Here is the second part:
@CamelCoder Nice job. One question, you don't allow the first variable for x^2 to be negative.
@dave1707 Thanks! Ya, I just didn't want to mess up the technique I used. Honestly, what I did was just thought about how I would factor a normal equation, and told the computer to do the same. That's probably why it's not too efficient. If you want, feel free to change it, I think that the leading coefficient shouldn't be negative.
@CamelCoder I'm not going to modify your code, but the leading coefficient could be negative as much as it could be positive. If the leading coefficient is negative, then there are 2 sets of answers. They're the same set of numbers, just with the signs flipped. Maybe I'll try writing something similar and see what I come up with. It's always good to see different coding styles.
Assuming you wrote this for fun, I have no criticism, because it's good practice - but if intended for practical use, I would far rather enter a series of numbers such as 43,-345,12,3 and have the program factor them. This allows as many numbers as you like, positive or negative, and if you want to enter 187, you don't have to press an arrow key 187 separate times.
@Ignatz I think 99% of the programs written here are for fun or by new coders just trying to learn how to write programs. If it's a program someone wants to make some money from, they may write it in Codea, but they're not going to post it here. Any program written can be improved upon by someone else. Pressing the up or down arrows to change a number could take awhile depending on the number, but it was an interesting idea anyways. Maybe a change could be if you press and hold the up/down arrows, the number starts changing until you lift your finger.
@Ignatz I didn't intend on making a UI aside from the parameters, but I just wanted to go a bit further, that's why it is awful. I will modify the code to make it possible to hold the arrow and zoom through the numbers. [Edit] I have added the ability to hold on the arrows to get to the number quicker
Here's another version. Tap a box and input the values then tap Solve.
@dave1707 I'm sorry to say, but every equation I would type in would give me no solution found.
@CamelCoder Try 3x^2+13x-10 or 6x^2+11x-10 or -6x^2+19x-15.
I'm glad my little post sparked some inspiration. @CamelCoder I enjoyed your UI. I have lots of diff ideas of projects and learning how to do UI like yours seems prudent. Thanks @dave1707 for that program. Some don't work but it's definitely better than mine
@Zacharias I didn't post the program to be better than yours, I posted it to show a different style and for others to learn from it. Everyone has a different style of coding and I still learn from others even though I've been coding for what seems like forever. You said some don't work, but not every equation will have factors. Do you have any examples of equations that should work and don't. As for your code sparking interest, yes it did. I'm working on a program that will factor equations such as x^3+x^2+x+#=0 and even higher orders.
Here's a program that will factor third order equations like 1x3-5x2+2x+8 . Just enter 1,-5,2,8 in the parameter text area and press Factor. I included 6 examples to try. If there are more factors then the screen will show, just slide them up. I didn't spend a lot of time making it fancy, so if you want you can do it.
@dave1707 with your quadratic factoring calculator, you didn't allow for -ve value, so I just (inelegantly) tweaked it:
@MattthewLXXIII Thanks for the fix. I think I ran into that error when I did the next program for third order equations. The way I fixed it there was in the function vals() where I always created - values instead of checking for v<0 before creating them.