Calvin hates his homework so.
He knows more than his teacher knows.
His wicked teacher has assigned
ten-thousand problems
, wastes of time.
"I'll learn nothing from this homework, bah!"
"It's only simple algebra."
"Screw homework"
, cries Calvin,
"My teacher is crazy."
"Calvin needs time to spend with the ladies."
Thankfully his computer skills wreck.
What is the sum of all values of
x
?
Details and assumptions :
The problems can be found here .
Round the total to the tenth's place.
The operators that can be used include {+, -, *, =}.
No problems with multiple values of x exist.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
That is very good. You worked really hard
How do you get syntax highlighting?
Log in to reply
Just use this:
```[your language here]
[your code here]
```
Note that the marks used are ` ( grave accents ) and not ' ( apostrophes ).
Log in to reply
This is a great question! Please post a question with 100 variables-100 equations, that would be amazing.
Thank you!
the code won´t work, that's an ArrayList and you cast it as [i], you need to use .get(i)
[This is not a solution. Just clever use of Computer Algebra Systems]
sage
Code:
1 2 3 4 |
|
You can run Sage without downloading anything at http://sagecell.sagemath.org/
[The real solution can be very hard to code. It will require a good knowledge of parsing expressions among other harder things]
What concise code.
My solution might be hard to interpret, so here's what actually happening -
First I separate LHS and RHS of the equation. Then I separate the variable and constant terms. Since we only care about coefficients of the variables, we only retain them while dispose off the variable.
Then I subtract variables on RHS from LHS. Then I subtract the constants on LHS from RHS. Then I divide the result obtained from constant subtraction with the result obtained from variable subtraction.
Here is my code -
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 |
|
My longest python code ever written (and possibly the ugliest). Now wait until I invent new WolframAlpha.
Each line must be reduced to a single numerical result; all line results must be collapsed into a sum for the final solution.
Using Python, I convert each line/equation to the form: α x + c = 0 by grouping like terms in order to count coefficients for all x and 1 ( unit ) terms. My code is "ugly" with debugging statements purposely left in to show work (for the reader's understanding besides the code comments), and with verbose calculations/inelegant constructs to divulge my mental deconstruction of the problem.
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 |
|
This solution in python works because all the equations are linear with one variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
It's probably easier using python math libraries like sympy, but here's my solution using python's standard libraries. Basically I do some string manipulations and use the eval() function to try to find the solution using binomial search
f = open('problems.txt', 'r')
lines = [x.strip() for x in f.readlines()]
def root(eq, a, b):
x = b
if eval(eq) < 0:
return root(eq,b,a)
x = (a+b)/2.0
#raw_input(eval(eq))
if (-1.0e-16 <= eval(eq) <= 1.0e-10):
return x
elif (eval(eq) > 0):
return root(eq, a, x)
else:
return root(eq, x, b)
def solve(eq):
eq = eq.split('=')
left = '(' + eq[0] + ')'
right = '(' + eq[1] + ')'
return root(left + '-' + right, 1.0e10, -1.0e10)
total_x = 0.0
for i, val in enumerate(lines):
sol = solve(val)
#print i, sol
total_x += sol
print total_x
Two solutions
First solution: Convert the equations into expressions involving complex numbers so that they can be simplified using Python's built-in eval function. For example, the first problem converts from x = 42*x + 85 - 71 to 1j-42j-85+71 (the 'j' being Python's 'i'). The real and imaginary parts of the result of this expression can be used to form a fraction enabling precise calculation of the total required for solution of the problem. Here's the complete script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Second solution: Just use sympy. Read each equation, convert it to an expression acceptable to sympy and solve it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
I used a few tricks to make the problem easier, then solved it. My solution hinges on the idea that we only need to worry about addition, subtraction, and multiplication (which can be resolved in that order), and that there are no cases of one constant times another constant (so we don't even need to worry about multiplication if no 'x' is present.)
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 |
|
Here's mine c/c++ solution! :)
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 |
|
My solution in C (and in english)
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 |
|
Last line of the output is the solution. Pipe in the input file using > in your command line.
My solution is almost identical to another solution already posted, but I will post mine anyways. I am using python 2.7 and installed sympy.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Python 2.7:
1 2 3 4 5 6 7 8 9 10 |
|
Dependencies:
Sympy (if you're on Linux: sudo pip install sympy)
Note: initially, I made a solution that used bisection search , but the answer was actually off by two due to a weird precision error. :-\ So you get bonus points if you can figure that out!
Can you post your bisection search code?
Log in to reply
I think I deleted it at some point, I'll rewrite it and post it when I get back from work in a few hours.
how do you paste python code with syntax highlighting?
Log in to reply
Just use this:
```[your language here]
[your code here]
```
Note that the marks used are ` ( grave accents ) and not ' ( apostrophes ).
How did you generate the list of equations?
Log in to reply
I made a list of empty equations like this...
1 2 3 4 5 |
|
And to make a random problem, I formatted some random integers in:
1 2 |
|
Log in to reply
Interesting. Another approach could be using a formal grammar.
I agree that this is a big fat waste of Calvin's time. In reality, there are many many teachers who are actually putting ton's of homework of stuff that dhould be done by a computer. Instead of calculation, we should be teaching our students numerical methods and how to use a computer
Log in to reply
@Agnishom Chattopadhyay – Agreed, wholeheartedly.
Let me know if you ever write a program that manipulates formal grammar, I'd love to read it!
@Agnishom Chattopadhyay – Great response, bro
Problem Loading...
Note Loading...
Set Loading...
My solution in C++:
Output: 2 3 2 8 . 1 1
Basically I parse all the possible symbols, then I save the coefficients of x in a variable and I save all the linear terms in another variable. At the end, my equation is of the form a x = b , so the answer is a b .