zurück   Startseite   Blog Buchregal Buchregal, eBooks Twitter E-Mail

 

Primefactors and prime numbers

(define (prime-factors n)
  (let ((root (sqrt n)))
    (let loop ((p 2))
      (cond
       ((> p root) (list n))
       ((= 0 (remainder n p)) (cons p (prime-factors (/ n p))))
       (else (loop (+ p 1)))))))
;Value: prime-factors


(define (length lst)
    (if (null? lst)
       0
       (+ 1 (length (cdr lst)))))
;Value: length


(prime-factors 11)
;Value 13: (11)


(define (primefacsto n m)

  (if (< n m)
  
  (begin
     (if (< (length (prime-factors n)) 2)
            (begin (display "; ")(display n) (display ": ")(display "- prime number -" )) 
            (begin (display "; ")(display n) (display ": ") (display (prime-factors n)))
            )
    (newline)
    (primefacsto (+ n 1) m) 
  )
  #t
  )
)


(primefacsto 1 1000)
; 1: - prime number -
; 2: - prime number -
; 3: - prime number -
; 4: (2 2)
; 5: - prime number -
; 6: (2 3)
; 7: - prime number -
; 8: (2 2 2)
; 9: (3 3)
; 10: (2 5)
; 11: - prime number -
; 12: (2 2 3)
; 13: - prime number -
; 14: (2 7)
; 15: (3 5)
; 16: (2 2 2 2)
; 17: - prime number -
; 18: (2 3 3)
; 19: - prime number -
; 20: (2 2 5)
; 21: (3 7)
; 22: (2 11)
; 23: - prime number -
; 24: (2 2 2 3)
...

 

 

25. März 2019     | mit scheme | scheme | lisp | old data

zurück

 

     

Startseite | Impressum | Datenschutz