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.Sat, 07 Sep 2013 02:49:53 -0500Plot with prime numbershttp://ask.sagemath.org/question/10519/plot-with-prime-numbers/I am trying to create a visualization of how common it is for a number to be prime. I am fairly new with the program and was hoping to get some assistant with any veteran users. My code I wrote is:
def com_primes(k):
prime_list = []
for n in range(1,k+1):
for m in range(1,n+1):
if Integer(n)/Integer(n) & Integer(n)/1 in ZZ:
if is_prime(m):
prime_list.append((n,m))
return point(prime_divisor_list)
but the code is obviously wrong. If anyone could help me, that would be much appreciated.Fri, 06 Sep 2013 10:39:58 -0500http://ask.sagemath.org/question/10519/plot-with-prime-numbers/Answer by vdelecroix for <p>I am trying to create a visualization of how common it is for a number to be prime. I am fairly new with the program and was hoping to get some assistant with any veteran users. My code I wrote is: </p>
<pre><code>def com_primes(k):
prime_list = []
for n in range(1,k+1):
for m in range(1,n+1):
if Integer(n)/Integer(n) & Integer(n)/1 in ZZ:
if is_prime(m):
prime_list.append((n,m))
return point(prime_divisor_list)
</code></pre>
<p>but the code is obviously wrong. If anyone could help me, that would be much appreciated.</p>
http://ask.sagemath.org/question/10519/plot-with-prime-numbers/?answer=15430#post-id-15430Sage is useful because there are many ready-to-use tools. In particular to know if an integer is prime or to get its list of divisors.
sage: P1 = [] # list of pairs (n,n) when n is prime
sage: P2 = [] # list of pairs (n,d) where d|n and n is not prime
sage: for n in xsrange(1,100):
....: if n.is_prime():
....: P1.append((n,n))
....: else:
....: P2.extend([(n,d) for d in n.divisors()[1:]])
sage: G1 = point2d(P1,color='red',pointsize=20)
sage: G2 = point2d(P2,color='blue',pointsize=5)
sage: (G1 + G2).show()
Note that I used xsrange instead of xrange. This is because xsrange produces Sage integers from which you can use .is_prime() and .divisors() whereas xrange produces Python integers.
Concerning your problem "how common it is for a number to be prime" you may have a look at [the prime number theorem](http://en.wikipedia.org/wiki/Prime_number_theorem) which describes the distribution of primes among the integers.Sat, 07 Sep 2013 02:49:53 -0500http://ask.sagemath.org/question/10519/plot-with-prime-numbers/?answer=15430#post-id-15430Answer by John Palmieri for <p>I am trying to create a visualization of how common it is for a number to be prime. I am fairly new with the program and was hoping to get some assistant with any veteran users. My code I wrote is: </p>
<pre><code>def com_primes(k):
prime_list = []
for n in range(1,k+1):
for m in range(1,n+1):
if Integer(n)/Integer(n) & Integer(n)/1 in ZZ:
if is_prime(m):
prime_list.append((n,m))
return point(prime_divisor_list)
</code></pre>
<p>but the code is obviously wrong. If anyone could help me, that would be much appreciated.</p>
http://ask.sagemath.org/question/10519/plot-with-prime-numbers/?answer=15427#post-id-15427Line 5: `Integer(n)/Integer(n)` should probably have `Integer(m)` in the denominator.
Line 5: replace `&` with `in ZZ and`. Actually, `Integer(n)/1` will always be in ZZ. Maybe you should just have `if Integer(n)/Integer(m) in ZZ:` or `if Integer(m).divides(n):` but I'm not quite sure what you're looking for.
Last line: replace `prime_divisor_list` with `prime_list`.
Last line: should be indented four spaces.
So:
def com_primes(k):
prime_list = []
for n in range(1,k+1):
for m in range(1,n+1):
if Integer(m).divides(n):
if is_prime(m):
prime_list.append((n,m))
return point(prime_list)
Fri, 06 Sep 2013 16:15:03 -0500http://ask.sagemath.org/question/10519/plot-with-prime-numbers/?answer=15427#post-id-15427