Loops using while


; Print the prime factors of an integer n
(define (factors n)
  (let ((divisor 2))
    (while (> n 1)
      (while (integer? (/ n divisor))
        (write divisor) (newline)
        (set! n (/ n divisor)) )
      (set! divisor (1+ divisor)) ) ))


> (factors 120)
2
2
2
3
5
#[undefined]

Contents    Page-10    Prev    Next    Page+10    Index