This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; P35 (**) Determine the prime factors of a given positive integer. | |
;; Construct a flat list containing the prime factors in ascending order. | |
;; Example: | |
;; * (prime-factors 315) | |
;; (3 3 5 7) | |
#lang racket | |
(provide prime-factors) | |
(require "p31.ss") | |
(define (prime-factors n) | |
(let loop ((x n) (y 2) (acc '())) | |
(cond | |
((= x 1) | |
(reverse acc)) | |
((and (is-prime? y) (zero? (modulo x y))) | |
(loop (/ x y) 2 (cons y acc))) | |
(else | |
(loop x (+ y 1) acc))))) |
0 件のコメント:
コメントを投稿