| 1 | initial version |
Sage 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.
| 2 | No.2 Revision |
Sage 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 which describes the distribution of primes among the integers.
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.