In quanti modi riusciamo a scrivere il crivello di Eratostene?

Gianluca Moro giangiammy@gmail.com
Gio 28 Ott 2010 13:47:11 CEST


ciao,

la domanda vera e': quanto tempo possiamo dedicare a giocare :-D
allego una soluzione.

1 ]=> (passa-crivello-eratostene 100)

;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)

ciao
giammy

~/Desktop/Dropbox/data-rem1/src-old/eratostene $ cat eratostene.scm
;
; Crivello di Eratostene - by giammy - www.giammy.com - giangiammy@gmail.com
;
; Run test:
;bash-3.2$ mit-scheme -load eratostene.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 "eratostene.scm"... done
;
;1 ]=> (passa-crivello-eratostene 100)
;
;;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 (multiplo-di? n x)
  (= (* (floor (/ x n)) n) x)
)

(define (rimuovi-multipli-di n l)
  (delete-matching-items l (lambda (x) (multiplo-di? n x)))
)

(define (crivello l)
  (if (= (length l) 0)
      ()
      (cons (car l) (crivello (rimuovi-multipli-di (car l) (cdr l)))))
)

(define (build-list n)
  (if (= n 1)
      ()
      (cons n (build-list (- n 1))))
)

(define (passa-crivello-eratostene n)
   (crivello (reverse (build-list n)))
)

(passa-crivello-eratostene 100)





-- 
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