First time here? Check out the FAQ!

Ask Your Question
1

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

asked 3 years ago

Jerry Caveney gravatar image

updated 3 years ago

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.

Preview: (hide)

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 ( 3 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 3 years ago

updated 3 years ago

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

Preview: (hide)
link

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 ( 3 years ago )

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 ( 3 years ago )

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 ( 3 years ago )

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 ( 3 years ago )

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: 3 years ago

Seen: 567 times

Last updated: Jun 18 '21