Ask Your Question
1

How to reduce a list of fractions

asked 2021-08-17 19:01:08 +0100

Jerry Caveney gravatar image

updated 2021-08-18 23:10:39 +0100

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.

edit retag flag offensive close merge delete

Comments

1

n+1 is coprime with n => these fractions are reduced

FrédéricC gravatar imageFrédéricC ( 2021-08-17 19:08:48 +0100 )edit

Thank you! I didn't pay attention to the output of Sage. I'm trying to divide each term in the list by the term before it, but that's not what I asked for in Sage. Alas, I can't figure out how to ask for what I want. I tried using a(n) and a(n+1) and also creating the list L, but I couldn't get either of those to produce what I'm looking for.

(The result I am looking for is [1/2, 2/3, 9/8, 8/15]. )

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-17 20:16:39 +0100 )edit

This is a python question. Learn python first ?

FrédéricC gravatar imageFrédéricC ( 2021-08-17 20:23:28 +0100 )edit

a[0] will give 6198089008491993412801/6198089008491993412800, for example. for n in range(len(a)-1): ...

John Palmieri gravatar imageJohn Palmieri ( 2021-08-17 20:53:24 +0100 )edit

Thank you, FrédéricC. I went to Python, which I have been studying, slowly, and (of course) with some help from the Stack Overflow forum, figured out to do this in Python.

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-18 02:02:39 +0100 )edit

2 Answers

Sort by » oldest newest most voted
3

answered 2021-08-18 00:16:50 +0100

slelievre gravatar image

updated 2021-08-18 14:50:30 +0100

Use square brackets to take the term of some index in a list.

So a[n + 1] / a[n] instead of a(n + 1) / a(n).

sage: a = [6198089008491993412800,
....:     3099044504245996706400,
....:     2066029669497331137600,
....:     2324283378184497529800,
....:     1239617801698398682560]
sage: c = [a[n + 1] / a[n] for n in range(len(a) - 1)]
sage: c
[1/2, 2/3, 9/8, 8/15]

Added after I saw your answer using zip:

Another solution with zip would be:

sage: c = [x / y for x, y in zip(a[1:], a[:-1])]

See also the pairwise recipe in the itertools documentation, also available via the more-itertools package, and, starting with Python 3.10, directly from itertools:

edit flag offensive delete link more

Comments

Thank you! (I added my answer before I saw that you had answered.)

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-18 02:14:51 +0100 )edit

Thanks also for the Sage method using zip. Just noticed now that you had added that to your answer.

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-18 23:18:59 +0100 )edit

You can accept an answer by clicking the "accept" button (with a check mark) next to it. This will mark your question as solved in the list of questions.

slelievre gravatar imageslelievre ( 2021-08-19 13:26:46 +0100 )edit

Forgot about that. Done. Thank you.

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-20 01:37:45 +0100 )edit
1

answered 2021-08-18 02:06:46 +0100

Jerry Caveney gravatar image

updated 2021-08-18 23:13:09 +0100

Note: I added this answer before I saw that FrédéricC had provided an answer using Sage, which is perfect. I have not deleted my answer, since I think it might help other beginners like me understand Python in addition to Sage.

Another solution is to use Python:

import fractions

a = [insert list, deleting last item]

b =  [insert list deleting first item]

for x, y in zip(b, a):
    print(fractions.Fraction(x, y))
edit flag offensive delete link more

Comments

To display blocks of code or error messages in Ask Sage, skip a line above and below, and do one of the following (all give the same result):

  • indent all code lines with 4 spaces
  • select all code lines and click the "code" button (the icon with '101 010')
  • select all code lines and hit ctrl-K

For instance, typing

If we define `f` by

    def f(x, y, z):
        return x * y * z

then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:

    TypeError: f() takes exactly 3 arguments (1 given)

produces:

If we define f by

def f(x, y, z):
    return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)

You can edit your question and answer to do that.

slelievre gravatar imageslelievre ( 2021-08-18 14:51:30 +0100 )edit

I've tried to edit all my mistakes (though I'm not sure, should Sage output be treated as code? I'm guessing no). If mistakes still remain, please let me know and I'll correct them. Thanks again.

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-18 17:11:59 +0100 )edit

Thanks for editing! One way to deal with the input and output is to start input lines with the Sage prompt and include output lines in the code block, without prompt (as in my answer).

slelievre gravatar imageslelievre ( 2021-08-18 19:21:45 +0100 )edit

Corrected, and once more thanks. If you see anything else that should be fixed, please let me know. I've learned a lot from your (and others') responses to this question!

Jerry Caveney gravatar imageJerry Caveney ( 2021-08-18 23:20:32 +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

1 follower

Stats

Asked: 2021-08-17 19:01:08 +0100

Seen: 453 times

Last updated: Aug 18 '21