ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 09 Mar 2018 19:58:43 -0600given a prime, finding where it is the list of primeshttp://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/ I'm writing a program that gives as an output the prime factorization of a number, and then I'm putting the primes into a matrix based on what prime number it is, (i.e 541 is the 100th prime, so if 541^2 divides my integer, then there would be a 2 in the 100th spot of my vector that represents my number)
Is there a function that takes as its input a prime and gives as an output where it is in the list of primes.
(i.e we want f(541) = 100)
Thanks in advance!Sat, 10 Feb 2018 12:30:56 -0600http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/Answer by Emmanuel Charpentier for <p>I'm writing a program that gives as an output the prime factorization of a number, and then I'm putting the primes into a matrix based on what prime number it is, (i.e 541 is the 100th prime, so if 541^2 divides my integer, then there would be a 2 in the 100th spot of my vector that represents my number) </p>
<p>Is there a function that takes as its input a prime and gives as an output where it is in the list of primes.
(i.e we want f(541) = 100) </p>
<p>Thanks in advance!</p>
http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/?answer=41068#post-id-41068 prime_pi?
Type: PrimePi
String form: prime_pi
File: /usr/local/sage-8/local/lib/python2.7/site-packages/sage/functions/prime_pi.pyx
Docstring:
The prime counting function, which counts the number of primes less
than or equal to a given value.
INPUT:
* "x" - a real number
* "prime_bound" - (default 0) a real number < 2^32, "prime_pi"
will make sure to use all the primes up to "prime_bound"
(although, possibly more) in computing "prime_pi", this can
potentially speedup the time of computation, at a cost to memory
usage.
OUTPUT:
integer -- the number of primes <= "x"
So, given your number x,an obvious "brute-force" one-liner is :
def foo(x): return map(lambda(t):prime_pi(t[0],sqrt(x)), factor(x))
bar=Integer(round(10^10*random()))
bar
7218184858
foo(bar)
[1, 1211, 31354]
HTH,
Sun, 11 Feb 2018 14:31:36 -0600http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/?answer=41068#post-id-41068Comment by JRHales for <pre><code>prime_pi?
Type: PrimePi
String form: prime_pi
File: /usr/local/sage-8/local/lib/python2.7/site-packages/sage/functions/prime_pi.pyx
Docstring:
The prime counting function, which counts the number of primes less
than or equal to a given value.
INPUT:
* "x" - a real number
* "prime_bound" - (default 0) a real number < 2^32, "prime_pi"
will make sure to use all the primes up to "prime_bound"
(although, possibly more) in computing "prime_pi", this can
potentially speedup the time of computation, at a cost to memory
usage.
OUTPUT:
integer -- the number of primes <= "x"
</code></pre>
<p>So, given your number x,an obvious "brute-force" one-liner is :</p>
<pre><code>def foo(x): return map(lambda(t):prime_pi(t[0],sqrt(x)), factor(x))
bar=Integer(round(10^10*random()))
bar
7218184858
foo(bar)
[1, 1211, 31354]
</code></pre>
<p>HTH,</p>
http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/?comment=41459#post-id-41459This work! Thanks a ton!Fri, 09 Mar 2018 19:58:43 -0600http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/?comment=41459#post-id-41459Answer by dan_fulea for <p>I'm writing a program that gives as an output the prime factorization of a number, and then I'm putting the primes into a matrix based on what prime number it is, (i.e 541 is the 100th prime, so if 541^2 divides my integer, then there would be a 2 in the 100th spot of my vector that represents my number) </p>
<p>Is there a function that takes as its input a prime and gives as an output where it is in the list of primes.
(i.e we want f(541) = 100) </p>
<p>Thanks in advance!</p>
http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/?answer=41067#post-id-41067This is a solution that puts first all "possibly relevant small primes" for the task in a list, than asks for the index of a prime in the list, in a dialog with the sage interpreter:
sage: pList = prime_range(10**6)
sage: pList.index(541)
99
(So it is the pythonically $99$.th place.)Sun, 11 Feb 2018 14:26:14 -0600http://ask.sagemath.org/question/41058/given-a-prime-finding-where-it-is-the-list-of-primes/?answer=41067#post-id-41067