Ask Your Question
1

Solve, elegant way ?

asked 2018-11-29 09:31:33 +0100

ortollj gravatar image

updated 2018-11-29 11:04:07 +0100

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-12-14 01:58:38 +0100

borostack gravatar image

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[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 24

edit flag offensive delete link more

Comments

Thank you borostack

ortollj gravatar imageortollj ( 2018-12-16 17:36:08 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2018-11-29 09:31:33 +0100

Seen: 725 times

Last updated: Dec 14 '18