Loading [MathJax]/extensions/tex2jax.js

2010年7月20日火曜日

Determine the prime factors of a given positive integer

;; 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)))))
view raw p35.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿