Jump to content

Hello people of LTT forums(and to the Java Programmers of LTT forums)

 

So, just out of random boredness, is there any way to solve a simple equation in Java, for example "x-7=10" where "x=17?" I've tried thinking about using a string and extracting x from it, but I have no idea how to do this in Java, and I've been trying for half an hour or so now. Any ideas?

PC SETUP: | i5-4440 3.1 GHz | MSI Z97s Krait | EVGA GTX 970 | G.Skill Value 8GB | WDC Blue 500GB | NZXT S340

MOBILE SETUP: | Apple iPhone 6 | iOS 10b4 | Apple Watch Stainless Steel w/ Black Sport Band | WatchOS 3b4

 

Steam: chargerjake | Origin: chargerjake

 

Link to comment
https://linustechtips.com/topic/313068-solve-a-simple-equation-in-java/
Share on other sites

Link to post
Share on other sites

Not really played around with java before, but can't you just assign variables to all of the numbers and add them together like that

CPU: i5-4690k GPU: 280x Toxic PSU: Coolermaster V750 Motherboard: Z97X-SOC RAM: Ripjaws 1x8 1600mhz Case: Corsair 750D HDD: WD Blue 1TB

How to Build A PC|Windows 10 Review Follow the CoC and don't be a scrub~soaringchicken

 

Link to post
Share on other sites

This is a fairly complicated question and you have to use a recursive parser and go back and forth between different things. This is not something a beginner programmer can do if you want it to understand the equation itself.

 

You could do a pseudo answer where the program assumes certain things, but I don't think that's what you're looking for.

Link to post
Share on other sites

Sorry for my lack of knowledge around this but couldn't you try something like this??

 

EDIT: Just tried this in C# and it worked :D Got X=17

int variable = 0;int  number = 7;int answer = 10; while (variable - number != answer){variable++;}System.out.println ("X = " + variable);

My Current Build: 

Intel i5 3570K @ 4.4GHz 1.11V, Cooler Master Hyper 212 EVO, Asrock Z77 Extreme4, Corsair Vengeance 8GB 1600MHz, Samsung 840 EVO 250GB, Asus GTX 760 DCII Overclocked, Corsair CX600M

Link to post
Share on other sites

As Lotus said before, looks like that he wants to do a program that can solve linear equations passed through one terminal/GUI.

What do you mean??

My Current Build: 

Intel i5 3570K @ 4.4GHz 1.11V, Cooler Master Hyper 212 EVO, Asrock Z77 Extreme4, Corsair Vengeance 8GB 1600MHz, Samsung 840 EVO 250GB, Asus GTX 760 DCII Overclocked, Corsair CX600M

Link to post
Share on other sites

If I'm not wrong, he wants to input a string, like "23-12+7-12=x+6y" and get values of X and Y. That can be implemented with techniques like finding the operators location, comparing their precedence and execute them in order, or breaking stuff into tokens and building a resolution tree, or recursive algorithm (something like a modified merge-sort can be used to solve that).

Ohh ok, that makes more sense. I would have no idea where to start with that except for making individual functions for each possible template you might use :P

My Current Build: 

Intel i5 3570K @ 4.4GHz 1.11V, Cooler Master Hyper 212 EVO, Asrock Z77 Extreme4, Corsair Vengeance 8GB 1600MHz, Samsung 840 EVO 250GB, Asus GTX 760 DCII Overclocked, Corsair CX600M

Link to post
Share on other sites

You can solve equations using Newton's Method. If you have a function set to zero, i.e.  f(x)=0, then you can approximate to find the root (solve for x) by the formula: xn+1 = xn - f(xn)/f'(xn), where x0 is the initial guess and xn is the nth approximation of the root, and f'(x) is the first derivative of the function we are trying to solve. 

 

So in your example, we have f(x)=x-17=0, f'(x)=d/dx (x-17) = 1, and we can choose any x0 as your first approximation, lets say x0=5

 

x1 = x0 - f(x0)/f'(x0) = 5 - (5-17)/1 = 5 - -12 = 5+12 = 17

 

Since this is a linear equation, any value of our initial guess of x0 will yield the solution. For more complicated equations such as quadratics, you would repeat the process until your xn and xn+1 are sufficiently close to one another that it is in your acceptable range of error. For something with an integer root, you will get that exact integer as a solution after a number of processes. 

Link to post
Share on other sites

If you want to type the equation and it spits the answer like @gabrielcarvfer suggested, this will be quite longer than few lines.

 

You need to write a parser of the equation, then use some solving method (Newton's as suggested by Tuero is a good example .... and tons of work).

 

I know wolfram Alpha have an API that you may want to explore. I could be wrong, but I think its a rest API so you could use any language.

Link to post
Share on other sites

Sorry for my lack of knowledge around this but couldn't you try something like this??

 

EDIT: Just tried this in C# and it worked :D Got X=17

int variable = 0;int  number = 7;int answer = 10; while (variable - number != answer){variable++;}System.out.println ("X = " + variable);

you are kidding, right?

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to post
Share on other sites

you are kidding, right?

Nope, I'm being totally serious. I do have a lack of knowledge around this and the code I posted would work to solve the equation he posted :) Thanks for asking anyway!

My Current Build: 

Intel i5 3570K @ 4.4GHz 1.11V, Cooler Master Hyper 212 EVO, Asrock Z77 Extreme4, Corsair Vengeance 8GB 1600MHz, Samsung 840 EVO 250GB, Asus GTX 760 DCII Overclocked, Corsair CX600M

Link to post
Share on other sites

Sorry for my lack of knowledge around this but couldn't you try something like this??

 

EDIT: Just tried this in C# and it worked :D Got X=17

int variable = 0;int  number = 7;int answer = 10; while (variable - number != answer){variable++;}System.out.println ("X = " + variable);
Lol, wth? That actually works xD. I just rewrote this in GML to work with any equation you give it and pastebinned it:

http://pastebin.com/BQJpEL6V

Edit: I just did some testing, it doesn't seem to take kindly to some division and multiplication or non-integers :/

"My game vs my brains, who gets more fatal errors?" ~ Camper125Lv, GMC Jam #15

Link to post
Share on other sites

Nope, I'm being totally serious. I do have a lack of knowledge around this and the code I posted would work to solve the equation he posted :) Thanks for asking anyway!

 

 

 

Sorry for my lack of knowledge around this but couldn't you try something like this??

 

EDIT: Just tried this in C# and it worked :D Got X=17

int variable = 0;int  number = 7;int answer = 10; while (variable - number != answer){variable++;}System.out.println ("X = " + variable);
int  number = 7;int answer = 10;int variable = answer + number;

?!?!?!?!?!?!??!?!?

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to post
Share on other sites

Why not use a stack?

 

E.G:

 

2 + 4

---------

|     2  |

---------

|    +   |

---------

|    4    |

---------

 

Then use the operator to tell you what to do, caluclate the end result, and push it back in.

 

---------

|    6    |

---------

 

This will solve only one variable that is unknown.

 

Before, we could right write it like this: 2 + 4 = X

 

Because we know that both sides have to be equal this give us a large hint.

 

---------

|     2  |

---------

|    +   |

---------

|    4    |

---------

|     =  |

---------

|    X   |

---------

 

Then we solve it, and push the answer back in.

 

But, what about doing it with a variable on the left?

 

---------

|     2  |

---------

|    +   |

---------

|    X    |

---------

|     =  |

---------

|    6   |

---------

 

We know basic maths, therefore we can do the opposite to the right side.

 

Simplify both sides.

 

Find where the unknown variable is.

 

We find the first number on the right, and find its operator. (In this case it is "2+").

 

Then we find the = operator, and the number to the left, do the opposite to it, then push it back onto the stack.

 

Then we are left with:

---------

|    X   |

---------

|     =  |

---------

|    4   |

---------

 

This is the way I would solve 1 unknown variable.

Link to post
Share on other sites

You can use a stack, but how would you solve something like (3-4*5)*(4*-2+5)-(1-7*9)*x = 10? You have to check precedence to solve things correctly, check multiple operators (like in the 4*-2 case) in a way that you get the right result.

Working with polish or reverse polish notation is much easier to solve that kind of thing, but with the common notation it's not that easy.

 

You are right, reverse polsh notation would be easier, but, it was just an idea. Using normal notation is also not that hard. You can always use a different notation for making a digit a negative number.

 

Thanks.

Link to post
Share on other sites

I think you guys are making it a bit complicated. I just want to solve something simple, like x+4=5, in one terminal/cmd line. So I'd run my program and enter something like 4+x=10, and it would return that x = 6. It is kind of a facepalm, but I do like what @Wtalk2 said, its just this doesnt seem right. I am familiar with the Scanner class, and do know how to get string, double, and integer input from the user, I just don't know how to do it with strings and extract X out of it.

PC SETUP: | i5-4440 3.1 GHz | MSI Z97s Krait | EVGA GTX 970 | G.Skill Value 8GB | WDC Blue 500GB | NZXT S340

MOBILE SETUP: | Apple iPhone 6 | iOS 10b4 | Apple Watch Stainless Steel w/ Black Sport Band | WatchOS 3b4

 

Steam: chargerjake | Origin: chargerjake

 

Link to post
Share on other sites

Actually, I got a lead on something.

import java.util.Scanner;public class EquationSolver {	Scanner in = new Scanner(System.in);		public static void main(String[] args) {		Scanner in = new Scanner(System.in);		double var = in.nextDouble();		System.out.println(f(var));	}		public static double f(double x){		double y;		y = x * 3;//func here		return y;	}}

This asks the user for a number, and then applies the function f(x) = y to it, and then returns y.

PC SETUP: | i5-4440 3.1 GHz | MSI Z97s Krait | EVGA GTX 970 | G.Skill Value 8GB | WDC Blue 500GB | NZXT S340

MOBILE SETUP: | Apple iPhone 6 | iOS 10b4 | Apple Watch Stainless Steel w/ Black Sport Band | WatchOS 3b4

 

Steam: chargerjake | Origin: chargerjake

 

Link to post
Share on other sites

Generally, you just have to evaluate all constants and group everything in terms of the independent variable.

 

once you're at that stage, put it into an augmented matrix and then row reduce until it's in reduced row echelon form. You can solve for variables at that point.

 

As far as I know, that's pretty much how it works for systems that do not turn into a sparse matrix.

 

It's definitely nowhere near as easy as that sounds to implement.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×