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.Fri, 18 Jun 2021 20:10:42 +0200How to return only the denominators of a sequence of fractions created by a formulahttps://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/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.Thu, 17 Jun 2021 22:33:58 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/Comment by Jerry Caveney for <p>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:</p>
<pre><code>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)
</code></pre>
<p>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'".</p>
<p>Any help in showing me how to do this would be greatly appreciated.</p>
https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57605#post-id-57605I 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.Fri, 18 Jun 2021 00:00:37 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57605#post-id-57605Answer by John Palmieri for <p>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:</p>
<pre><code>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)
</code></pre>
<p>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'".</p>
<p>Any help in showing me how to do this would be greatly appreciated.</p>
https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?answer=57606#post-id-57606Your 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)`.Fri, 18 Jun 2021 00:30:26 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?answer=57606#post-id-57606Comment by John Palmieri for <p>Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:</p>
<pre><code>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
</code></pre>
<p>Then you can run <code>lcm(L)</code>.</p>
https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57621#post-id-57621The following links may be overwhelming, but here are some Python tutorials from python.org: https://wiki.python.org/moin/BeginnersGuide/NonProgrammers and https://wiki.python.org/moin/BeginnersGuide/Programmers. Years ago I enjoyed https://diveintopython3.problemsolving.io/Fri, 18 Jun 2021 20:10:42 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57621#post-id-57621Comment by Jerry Caveney for <p>Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:</p>
<pre><code>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
</code></pre>
<p>Then you can run <code>lcm(L)</code>.</p>
https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57610#post-id-57610Thank 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.Fri, 18 Jun 2021 02:39:43 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57610#post-id-57610Comment by John Palmieri for <p>Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:</p>
<pre><code>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
</code></pre>
<p>Then you can run <code>lcm(L)</code>.</p>
https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57608#post-id-57608First 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.Fri, 18 Jun 2021 02:11:09 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57608#post-id-57608Comment by Jerry Caveney for <p>Your loop "computes" the answers as strings. It would be better to compute them as rational numbers. For example:</p>
<pre><code>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
</code></pre>
<p>Then you can run <code>lcm(L)</code>.</p>
https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57607#post-id-57607Thank 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!Fri, 18 Jun 2021 01:26:13 +0200https://ask.sagemath.org/question/57604/how-to-return-only-the-denominators-of-a-sequence-of-fractions-created-by-a-formula/?comment=57607#post-id-57607