The Scheme Programming Language

Prime Number Calculation Example Program


Click below to go directly to a specific section:
Description| Source Code| Program Notes

Description

This program generates prime numbers.

Source Code

;
; primes
; By Ozan Yigit
;
(define  (interval-list m n)
  (if (> m n)
      '()
      (cons m (interval-list (+ 1 m) n))))

(define (sieve l)
  (define (remove-multiples n l)
    (if (null? l)
         '()
         (if  (= (modulo (car l) n) 0)      ; division test
              (remove-multiples n (cdr l))
              (cons (car l)
                    (remove-multiples n (cdr l))))))

  (if (null? l)
      '()
      (cons (car l)
             (sieve (remove-multiples (car l) (cdr l))))))

(define (primes<= n)
  (sieve (interval-list 2 n)))

; (primes<= 300)

Program Notes

This program was found at the ftp site:
ftp://ftp.cs.indiana.edu/pub/scheme-repository/code/num/primes.scm It was written by Ozan Yigit.
[Back] [Home]

Last modified: 01:30 PM on 11/25/1996