Statistics

Content View Hits : 21527
Scheme and Eratostenes (updated) PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Gurito   

This is just a simple implementation I did of the Sieve of Eratostenes --an algorithm which finds prime numbers in a given interval-- in the Scheme programming language.

  1. ;***********************************************************************
  2. ;This program was made by David Vazquez Landa
  3. ;It may be copied, changed and redistributed
  4. ;
  5. ;Author: David Vázquez Landa
  6. ;Version: 1.1
  7. ;execution: (eratostenes N)
  8. ;Where N is a Natural number bigger than 0
  9. ;***********************************************************************
  10.  
  11. ;Changelog
  12. ;version 1.1:
  13. ; + Added tests to the functions in order to check the parameters with
  14. ;   which they're called.
  15. ; + Added a function to append "1" to the primes list
  16.  
  17. ;-----------------------------------------------------------------------
  18. ;eratostenes finds the prime numbers in a given interval
  19. ;always between 0 and some natural number.
  20. ;The program is not perfect and can be very slow due to its
  21. ;recursive nature.
  22. ;-----------------------------------------------------------------------
  23.  
  24. ;                  ----------------------------
  25. ;Make a list of value-pairs of the form (<value>,<boolean) with the
  26. ;"value" part ranging from 0 to n and the "boolean" part defined as #t
  27. ;for (true)
  28. (define list-true
  29.   (lambda (n)
  30.     (if (not (number? n))
  31.         "Invalid Input"
  32.         (if (= n 2)
  33.             (list (list n #t))
  34.             (append
  35.              (list-true (- n 1))
  36.              (list (list n #t)))))))
  37.  
  38. ;                       --------------------------
  39.  
  40. ;This program receives a list of value-pairs of the form
  41. ;(<value>,<boolean>) and returns the first pair marked as true
  42. ;if it exists.
  43. (define first-true
  44.   (lambda (n)
  45.     (if (not (list? n))
  46.         "Invalid Input"
  47.         (if (null? n)
  48.             ; if the first value is true
  49.             '()
  50.             (if (cadr (car n))
  51.                 (car n)
  52.                 (first-true (cdr n)))))))
  53.  
  54. ;(printf "Test first-true~n")
  55. ;(first-true '((0 #f) (1 #f) (2 #t)))
  56. ;(printf "Expected is: ((2 #t))~n")
  57.  
  58. ;                   ----------------------------
  59.  
  60. ;Erase the multiples of a value from a list
  61. ;This function looks for all the multiples of a given value
  62. ;in a value-pair list and returns the same list with the
  63. ;<boolean> part of the pair set to #f for the <value> part of
  64. ;each multiple found.
  65. ;Parameters:
  66. ;   x -> the value to look for. Number
  67. ;   n -> the value-pair list in where to look
  68. (define mark-multiples
  69.   (lambda (x n)
  70.     (if (or (not (list? n))
  71.             (not (number? x)))
  72.         "invalid input"
  73.         (if (empty? n)
  74.             '()
  75.             (if (or (not (= (modulo (caar n) x)
  76.                             0)) ;if it's not multiple...
  77.                     (= (caar n) x)) ;if it's the same number...
  78.                 (append (list (car n)) (mark-multiples x (cdr n)))
  79.                 (append (list (list (caar n) #f))
  80.                         (mark-multiples x (cdr n))))))))
  81.  
  82. ;(first-true (mark-multiples 5 (list-true 10)))
  83.  
  84. ;                          ------------------------
  85.  
  86. ;The main function: eratostenes
  87. ;The number "1" is a special case. Therefore I can decide whether
  88. ;to append it to the list or not. Here, it is not included
  89. (define eratostenes
  90.   (lambda (n)
  91.     (let walk ((l (list-true n)) (x (car (list-true n))))
  92.       (if (null? l) '()
  93.           (begin
  94.           (if (or (< n (sqr (car x)))
  95.                   (null? (first-true l)))
  96.               l
  97.               (append (list (car l))
  98.                       (walk (mark-multiples (car x) (cdr l))
  99.                             (first-true (mark-multiples (car x) (cdr l)))))))))))
  100.  
  101. ;                          ------------------------
  102.  
  103. ;The same as eartostenes, it just appends "1" to the list of primes.
  104. (define eratostenesOne
  105.   (lambda (L)
  106.     (if (null? L)
  107.         '()
  108.         (append (list '(1 #t))
  109.                 (eratostenes L)))))<p> </p><p>;            --------------------------
  110. ; Print trues
  111. ; This program takes a list containing value-pairs of the
  112. ; form (&lt;value&gt;,&lt;boolean&gt;) and prints a list containing
  113. ; the values for those value-pairs which contain #t in
  114. ; the &lt;boolean&gt; part.
  115.  
  116. (define printTrues
  117.   (lambda (l)
  118.     (if (null? l)
  119.         '()
  120.         (if (cadar l)
  121.             (append (list (caar l))
  122.                     (printTrues (cdr l)))
  123.             (printTrues (cdr l))))))</p><p> </p><p>;            --------------------------
  124.  
  125. ;Try the program :)      
  126. (printf "A simple test, find the prime numbers contained in 0 to 10: ~n")
  127. (eratostenesOne 10)
  128. (printf "Print the prime numbers (just numbers) contained in 0 to 100: ~n")
  129. (printTrues (eratostenesOne 100))
  130. (printf "Print the number of prime numbers contained in 0 to 100: ~n")
  131. (length (printTrues(eratostenesOne 100)))</p> 
Last Updated on Tuesday, 31 March 2009 15:15