In quanti modi riusciamo a scrivere il crivello di Eratostene?
Gianluca Moro
giangiammy@gmail.com
Ven 29 Ott 2010 02:26:24 CEST
ciao,
2010/10/29 Massimo Masson <max@studiomasson.it>:
> Il 28/10/2010 13.47, Gianluca Moro ha scritto:
> Eccolo:
>
>>>> primes = lambda l: l and l[:1] + primes([n for n in l if n % l[0]])
>>>> primes(range(2,100))
>
mmm ... non conosco granche' python (e non mi e' chiaro come finisca la
ricorsione), ma a naso l'algoritmo dovrebbe essere questo:
$ cat eratostene2.scm
;
; Crivello di Eratostene versione 2 - by giammy - www.giammy.com -
giangiammy@gmail.com
;
;
; $ mit-scheme -load eratostene2.scm
;MIT/GNU Scheme running under MacOSX
;Type `^C' (control-C) followed by `H' to obtain information about interrupts.
;
;Copyright (C) 2010 Massachusetts Institute of Technology
;This is free software; see the source for copying conditions. There is NO
;warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;
;Image saved on Thursday October 28, 2010 at 12:06:49 PM
; Release 9.0.1 || Microcode 15.1 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118
; Edwin 3.116
;;Loading "eratostene2.scm"... done
;
;1 ]=> (go)
;
;;Value 2: (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97)
;
;1 ]=>
;
(define (% a b) (not (= (remainder a b) 0)))
(define (primes l) (if (null? l) () (cons (car l) (primes (filter
(lambda (x) (% x (car l))) l)))))
(define (go) (primes (iota 99 2)))
Non e' piu' chiaro cosi'???? :-D
notte
giammy
--
Gianluca Moro --- Enjoy CloudUSB: http://www.cloudusb.net/
http://www.giammy.com --- http://www.math.unipd.it/~giammy/
Diaspora Forum: http://diasporadoc.info/forum/
Just remember, there's a right way and a wrong way to do everything
and the wrong way is to keep trying to make everybody else do it the
right way
Maggiori informazioni sulla lista
blug