Why should I switch to Python? - Infinity of Primes
Charles Boncelet
boncelet at udel.edu
Fri Apr 7 03:41:54 CEST 2000
Harald Hanche-Olsen wrote:
> Since this is a posted to a computer language group, after all, I
> include my own simple implementation of the Sieve in Haskell:
>
(Haskell code deleted)
>
> I wonder if someone can come up with an equally immediate solution
> (i.e., a straight translation from the mathematical formulation) in
> python?
>
> Indeed, I would be tempted to say that Haskell is better suited for
> learning (some kinds of) mathematics than python.
I don't know any Haskell, but a quick Python sieve is easy:
def sieve(n):
"""find primes up to n by sieve of Erasthones"""
primes = [2]
marked = [0]*n
for p in primes:
i = p
while(i < n):
marked[i] = 1
i = i + p
for q in range(p,n):
if marked[q] == 0:
primes.append(q)
break
return primes
>
> ------
> Charles Boncelet, University of Delaware,
> On sabbatical at ADFA, Canberra Australia,
> Home Page: http://www.ece.udel.edu/~boncelet/
More information about the Python-list
mailing list