ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 16 Dec 2018 17:36:08 +0100Solve, elegant way ?https://ask.sagemath.org/question/44496/solve-elegant-way/Hi
SageMath version: 8.4, OS:W10
to solve this little problem, first I tried this code:
#PB: Maxime is 32 years old.
#It is twice as old as Albane was when he was the age Albane has now.
#What is Albane's age?
# m = age of Maxime a = age of Albane, x = age difference
forget()
var(' a_0 m_0 a_1 m_1 x')
assume(a_0,'real')
eq0=m_0==m_1/2
eq1=2*(a_0)==m_0
S_0=solve([eq0.substitute(m_1=32),eq1],a_0)
S_1=solve([eq0.substitute(m_1=32),eq1],a_0,m_0)
show("S_0 : ",S_0,"S_1 : ",S_1)
question 1 : in the code above what is wrong , why S_0 does not give a solution ?
afterward I tried this code below, which gives the good answer:
#PB: Maxime is 32 years old.
#It is twice as old as Albane was when he was the age Albane has now.
#What is Albane's age?
# m_i = age of Maxime a_i = age of Albane, x = age difference
forget()
var(' a_0 m_0 x')
m_1=32
m_0=m_1/2
eq1=2*(a_0)==m_0
a_0=solve([eq1],a_0)[0].rhs()
x=m_0-a_0
a_1=m_1-x
show(" actual age of Albane : ",a_1)
question 2 : in order to serve me as an example, could someone show me a most elegant way to solve this little PB with SageMath ?
[problem source](https://www.futura-sciences.com/sciences/questions-reponses/mathematiques-jeu-mathematique-question-age-rend-fou-10175/#xtor=EPR-69-[QUESTIONMATH]-20181127)Thu, 29 Nov 2018 09:31:33 +0100https://ask.sagemath.org/question/44496/solve-elegant-way/Answer by borostack for <p>Hi</p>
<p>SageMath version: 8.4, OS:W10</p>
<p>to solve this little problem, first I tried this code:</p>
<pre><code>#PB: Maxime is 32 years old.
#It is twice as old as Albane was when he was the age Albane has now.
#What is Albane's age?
# m = age of Maxime a = age of Albane, x = age difference
forget()
var(' a_0 m_0 a_1 m_1 x')
assume(a_0,'real')
eq0=m_0==m_1/2
eq1=2*(a_0)==m_0
S_0=solve([eq0.substitute(m_1=32),eq1],a_0)
S_1=solve([eq0.substitute(m_1=32),eq1],a_0,m_0)
show("S_0 : ",S_0,"S_1 : ",S_1)
</code></pre>
<p>question 1 : in the code above what is wrong , why S_0 does not give a solution ?</p>
<p>afterward I tried this code below, which gives the good answer:</p>
<pre><code>#PB: Maxime is 32 years old.
#It is twice as old as Albane was when he was the age Albane has now.
#What is Albane's age?
# m_i = age of Maxime a_i = age of Albane, x = age difference
forget()
var(' a_0 m_0 x')
m_1=32
m_0=m_1/2
eq1=2*(a_0)==m_0
a_0=solve([eq1],a_0)[0].rhs()
x=m_0-a_0
a_1=m_1-x
show(" actual age of Albane : ",a_1)
</code></pre>
<p>question 2 : in order to serve me as an example, could someone show me a most elegant way to solve this little PB with SageMath ?</p>
<p><a href="https://www.futura-sciences.com/sciences/questions-reponses/mathematiques-jeu-mathematique-question-age-rend-fou-10175/#xtor=EPR-69-[QUESTIONMATH]-20181127">problem source</a></p>
https://ask.sagemath.org/question/44496/solve-elegant-way/?answer=44664#post-id-44664Well, I wrote this shorter code :
#PB: Maxime is 32 years old.
#It is twice as old as Albane was when he was the age Albane has now.
#What is Albane's age?
# m = age of Maxime a = age of Albane, x = age difference
var ('m, x, a')
e1 = m - a == x
e2 = m == 2 * (a-x)
solution = solve([e1,e2],x,a)
show(solution[0])
print 'the age of Albane is', solution[0][1].rhs().subs(m=32)
The output is :
> $\left[x = \frac{1}{4} \, m, a = \frac{3}{4} \, m\right]$
> the age of Albane is 24Fri, 14 Dec 2018 01:58:38 +0100https://ask.sagemath.org/question/44496/solve-elegant-way/?answer=44664#post-id-44664Comment by ortollj for <p>Well, I wrote this shorter code :</p>
<pre><code>#PB: Maxime is 32 years old.
#It is twice as old as Albane was when he was the age Albane has now.
#What is Albane's age?
# m = age of Maxime a = age of Albane, x = age difference
var ('m, x, a')
e1 = m - a == x
e2 = m == 2 * (a-x)
solution = solve([e1,e2],x,a)
show(solution[0])
print 'the age of Albane is', solution[0][1].rhs().subs(m=32)
</code></pre>
<p>The output is :</p>
<blockquote>
<p>$\left[x = \frac{1}{4} \, m, a = \frac{3}{4} \, m\right]$</p>
<p>the age of Albane is 24</p>
</blockquote>
https://ask.sagemath.org/question/44496/solve-elegant-way/?comment=44683#post-id-44683Thank you borostackSun, 16 Dec 2018 17:36:08 +0100https://ask.sagemath.org/question/44496/solve-elegant-way/?comment=44683#post-id-44683