# 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).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

edit retag close merge delete

Sort by » oldest newest most voted

Well, 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)
print 'the age of Albane is', solution.rhs().subs(m=32)


The output is :

$\left[x = \frac{1}{4} \, m, a = \frac{3}{4} \, m\right]$

the age of Albane is 24

more