[PYTHON MATRIX-SIG] Fun: hacking some Numerics.

Rob.Hooft@embl-heidelberg.de Rob.Hooft@embl-heidelberg.de
Thu, 15 Aug 1996 18:29:43 +0200 (MET DST)


I'm running python-1.4b2 + Numeric on an ALPHA. Just for fun I was hacking
together a script to do dumb factorization. I have a few problems that could
be due to this being a 64bit python, but I don't have 1.4b2 on any other
machine here to test. Please help.

My script and the output are appended below. Two questions with simpler
cases are extracted from the script:

1)
       nu[160]NumPy% python
       Python 1.4b2 (Aug 12 1996) [C]
       Copyright 1991-1996 Stichting Mathematisch Centrum, Amsterdam
       >>> from Numeric import *
       >>> array([], 'l')   
       EmptyArray
       >>> array([], 'l')[0]
       4294967296
       >>> array([], 'l')[1]
       Traceback (innermost last):
         File "<stdin>", line 1, in ?
       IndexError: index out of bounds
       >>> for i in array([], 'l'):
       ...     print i
       ... 
       4294967296
2)
       nu[161]NumPy% python
       Python 1.4b2 (Aug 12 1996) [C]
       Copyright 1991-1996 Stichting Mathematisch Centrum, Amsterdam
       >>> from Numeric import *
       >>> where([0,1,1,0,1,1],[0,0,0,0,0,0],[1,1,1,1,1,1])
       1 0 0 1 0 0
       >>> where([0,1,2,0,1,2],[0,0,0,0,0,0],[1,1,1,1,1,1])
       1 0 0 0 0 0

Thanks in advance,

Rob.
------------------------------------------------------------------------
from Numeric import *
import math

def sieve(max):
	numbers=range(max+1)
	size=int(math.sqrt(max))
	if size<5:
		trials=[2,3]
	else:
		trials=sieve(size)
	for i in trials:
		try:
			j=i*i
			while 1:
				numbers[j]=0
				j=j+i
		except IndexError:
			pass
	return filter(lambda x:x>1,numbers)

def asieve(max):
	numbers=arange(max+1)
	# one is not prime
        numbers[1]=0
	size=int(math.sqrt(max))
	if size<5:
		trials=[2,3]
	else:
		trials=asieve(size)
	print "We need to try all in "+`trials`
	for i in trials:
		print "Filtering by %d"%i
		cond=numbers%i
		cond[i]=1
		print cond
		print numbers
		numbers=where(cond,numbers,0)
		print numbers
	return nonzero(numbers)

def factor(num,upto=1000000):
	for p in sieve(upto):
		if num%p==0:
			print "Can divide by %d"%p

def afactor(num,upto=1000000):
	for p in asieve(upto):
		if num%p==0:
			print "Can divide by %d"%p

factor(129753308,upto=99)
afactor(129753308,upto=99)
---------------------------------------------------------------
Can divide by 2
Can divide by 29
We need to try all in [2, 3]
Filtering by 2
0 0 1 1 0 1 0 1 0 1
0 0 2 3 4 5 6 7 8 9
0 0 2 3 0 5 0 7 0 9
Filtering by 3
0 0 2 1 0 2 0 1 0 0
0 0 2 3 0 5 0 7 0 9
0 0 0 0 0 0 0 0 0 0
We need to try all in array([], 'l')
Filtering by 4294967296
 1  0  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      98 99
 0  0  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      98 99
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
--------------------------------------------------------------------
-- 
=== Rob.Hooft@EMBL-Heidelberg.DE   http://www.Sander.EMBL-Heidelberg.DE/rob/ ==
==== In need of protein modeling?  http://www.Sander.EMBL-Heidelberg.DE/whatif/
Validation of protein structures?  http://biotech.EMBL-Heidelberg.DE:8400/ ====
== PGPid 0xFA19277D == Use Linux!  Free Software Rules The World! =============

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================