I have a couple of double-conversion UPSs (i.e. the good kind) powering up two racks on my experiment. These are sophisticated instruments, with inbuilt battery checks and reporting of lots of power quality parameters. The trouble is to read them out into a computer and control them with serial commands, since the serial command set appears to be proprietary.
Enter the NUT project ! It includes drivers for a huge number of UPS systems, and there's a Python wrapper around it all called PyNUT. Code coming soon ...
Monday, May 27, 2013
Wednesday, May 22, 2013
Prime number density
Prime numbers are almost 'physical'. Spooky.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pylab as plt | |
import numpy as np | |
def primes(x): | |
p = [] | |
mask = range(2,x-1) | |
while len(mask)>0: | |
p.append(mask[0]) | |
del mask[::p[-1]] | |
return p | |
def prime_density(x): | |
p = primes(x) | |
#print p | |
Z = [(1 - 1./pi) for pi in p if pi < x/2.] | |
z = reduce( lambda x,y: x*y, Z ) | |
rho = (len(primes(x+2000)) - len(primes(x-2000)) )/4e3 | |
print z,rho," ",x | |
return (z,rho) | |
pd = [] | |
y = np.arange(3.4,8,0.2) | |
x = map(int,10**y) | |
for xi in x: | |
try: pd.append(prime_density(xi)) | |
except KeyboardInterrupt: break | |
z = np.array([pdi[0] for pdi in pd]) | |
rho = np.array([pdi[1] for pdi in pd]) | |
x = x[:len(z)] | |
f = 1./(0.5772 + np.log(x)) | |
plt.figure() | |
plt.xlabel("N") | |
plt.ylabel("Density of primes") | |
plt.semilogx(x,rho,'bo',label="Measured") | |
plt.semilogx(x,f,'g',label="1/(\gamma + log x)") | |
plt.semilogx(x,z,'r',label="product formula") | |
plt.semilogx(x,(z+f)/2.,'b',label="average formula") | |
plt.legend(loc="upper right") | |
plt.show() |
Listing primes
Naive prime testing compared to the Sieve of Eratosthenes.
cProfile says (for N = 100 000):
naive testing = 7.912 CPU seconds
sieve method = 0.064 CPU seconds
Sieving is fast !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test2(x): | |
""" Sieve of Eratosthenes """ | |
p = [] | |
mask = range(2,x-1) | |
while len(mask)>0: | |
p.append(mask[0]) | |
del mask[::p[-1]] | |
return p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test1(x): | |
""" Naive testing by divisibility """ | |
p = [2,3,5,7] | |
for i in xrange(7,x-1): | |
isprime = True | |
for pi in p: | |
isprime = ( isprime and (i%pi) ) | |
if not isprime: break | |
if isprime: p.append(i) | |
return p |
naive testing = 7.912 CPU seconds
sieve method = 0.064 CPU seconds
Sieving is fast !
Subscribe to:
Posts (Atom)