Ask Your Question
1

How to return only the denominators of a sequence of fractions created by a formula

asked 2021-06-17 22:33:58 +0100

Jerry Caveney gravatar image

updated 2021-06-18 00:23:52 +0100

With essential help from slelievre, I managed to achieve coding to return the fractional 'density' (if that is the right term) of primes between n^2 and (n+1)^2:

for n in range(1, 51):
    a = n^2 + 1
    b = (n + 1)^2 + 1
    c = 2*n
    nprimes = sum(1 for p in prime_range(a, b))
    d = f"{nprimes}/{c}"
    print(d)

Now I want to extract just the denominators of this sequence so that I can find the LCM of all those denominators and then convert all the fractions to have the same LCM denominator. (I cannot just use 2n to find the LCM because the fractions need to be reduced.) I found the command x.denominator(), but it seems to work for only a single fraction of integers. At least that's what I understand the error message to mean when I try d.denominator() or print(d.denominator()), the error message reading "AttributeError: 'str' object has no attribute 'denominator'".

Any help in showing me how to do this would be greatly appreciated.

edit retag flag offensive close merge delete

Comments

I have now worked out the LCM by hand (but I still want to be able to do it through SageMath), and the value is a bit over 6 sextillion (6*10^21). I could reduce that number substantially by lowering the range of n down to 30 or 40, but I'd really prefer not to do that. Will I likely run into any problems in the coding I've asked about in my question with the LCM being that large? I'd appreciate anyone's thoughts on this.

Jerry Caveney gravatar imageJerry Caveney ( 2021-06-18 00:00:37 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-06-18 00:30:26 +0100

updated 2021-06-18 02:09:07 +0100

Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:

def my_function(k=51):
    # L = list in which to collect the answers
    L = []
    for n in range(1, k):
        a = n**2 + 1
        b = (n+1)**2 + 1
        c = 2*n
        nprimes = sum(1 for p in prime_range(a, b))
        # QQ(...) converts the argument to a rational number
        # L.append(...) appends the argument to L
        L.append(QQ(nprimes/c).denominator())
    return L

Then you can run lcm(L).

edit flag offensive delete link more

Comments

Thank you so much. This certainly looks like what I need, but I can't get it to work. I apologize for my ignorance. I first copied your coding into SM, but it did not evaluate to anything. So I tried to run lcm(L), in a variety of configurations (e.g., with and without the print() command) and with various indentations (from none to 2). Whenever I did this I either got a syntax error, which I could generally manage to fix, but then got an error message that "name 'L' is not defined".

At that point, I tried to go back and print L without using lcm, again with the same variety of configurations, but I continue to get either no evaluation or the error "name 'L' is not defined". Please tell me what I'm doing wrong. Thank you!

Jerry Caveney gravatar imageJerry Caveney ( 2021-06-18 01:26:13 +0100 )edit

First there was a typo: "primes" -> "nprimes". Now fixed. Second, do you understand basic Python syntax? This defines a function, so you have to run "my_function(35)", replacing 35 with whatever number you want. Or LIST = my_function(35) to define LIST as the output of the function.

John Palmieri gravatar imageJohn Palmieri ( 2021-06-18 02:11:09 +0100 )edit

Thank you, everything works beautifully now. I'm learning Python syntax mostly by trial and error. I've worked on the SM tutorial, but thus far it doesn't seem to have gotten to the sort of knowledge I need for the things I'm trying to do. Hopefully I'll eventually come across what I need in it.

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

The following links may be overwhelming, but here are some Python tutorials from python.org: https://wiki.python.org/moin/Beginner... and https://wiki.python.org/moin/Beginner.... Years ago I enjoyed https://diveintopython3.problemsolvin...

John Palmieri gravatar imageJohn Palmieri ( 2021-06-18 20:10:42 +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-06-17 22:33:58 +0100

Seen: 445 times

Last updated: Jun 18 '21