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.Tue, 17 Aug 2021 19:01:08 +0200How to reduce a list of fractionshttps://ask.sagemath.org/question/58505/how-to-reduce-a-list-of-fractions/I have a list of integers. I want to divide (n+1) by n. I can do this with no problem, but I can't get the resulting fractions in reduced form. My list has 50 items, but I'll only do a list of 5 here to avoid spamming.
sage: a = [6198089008491993412800, 3099044504245996706400, 2066029669497331137600, 2324283378184497529800, 1239617801698398682560]
sage: c = [(n + 1)/n for n in a]
sage: print (c)
[6198089008491993412801/6198089008491993412800, 3099044504245996706401/3099044504245996706400, 2066029669497331137601/2066029669497331137600, 2324283378184497529801/2324283378184497529800, 1239617801698398682561/1239617801698398682560]
I don't know why Sage doesn't automatically reduce these fractions, but since it doesn't, I've tried multiplying by 1 and using reduce() and simplify(), and have searched everywhere I could think of for an answer, but nothing works. I apologize in advance for asking such a rudimentary question.Jerry CaveneyTue, 17 Aug 2021 19:01:08 +0200https://ask.sagemath.org/question/58505/Use of / operator when defining a functionhttps://ask.sagemath.org/question/43906/use-of-operator-when-defining-a-function/ Hello. I need a clarification on the behaviour of fractions. More precisely, I don't understand the behaviour of the / operator when used inside a def().
I noticed that / finds the integer part of the fraction when used inside a def:
def sumA():
a=randint(2,10)
b=randint(2,10)
c=randint(3,10)
d=randint(4,10)
return [a,b,c,d,a/b + c/d]
Then sumA() the following result:
[8, 3, 9, 8, 3]
That is: 8/3 + 9/8 = 3. How can I make a/b + c/d work like a sum of fractions inside this function?
Oddly enough, the behaviour is different when generating random numbers a and b and adding 1/a + 1/b.
def sumB():
a=randint(2,10)
b=randint(2,10)
return [a,b,1/a + 1/b]
This function adds the fractions in the correct way. The result of sumB() is
[5, 9, 14/45]
Finally, I noticed that writing a*1/b + c*1/d instead of a/b + c/d makes everything work like fractions:
def sumC():
a=randint(2,10)
b=randint(2,10)
c=randint(3,10)
d=randint(4,10)
return [a,b,c,d,a*1/b + c*1/d]
The result of sumC() is
[6, 7, 5, 6, 71/42]
jcarrilloWed, 10 Oct 2018 21:07:28 +0200https://ask.sagemath.org/question/43906/formatting fractions involving square rootshttps://ask.sagemath.org/question/32383/formatting-fractions-involving-square-roots/How can I tell sage that I want the result to be shown as $\frac{1}{\sqrt{3}}$ rather than $\frac13\sqrt3$ when I type <code>show(1/sqrt(3))</code>?BjörnSat, 30 Jan 2016 13:48:36 +0100https://ask.sagemath.org/question/32383/