---
title: "sicp练习1.21用smallest-divisor寻找最小因子"
author: "hotcharm"
date: "2011-05-22T14:36:43Z"
tags: []
categories: ["编程语言"]
slug: "sicp-1-21-smallest-divisor"
draft: false
lastmod: "2011-05-22T14:36:43Z"
id: 1054398
url: "https://hotcharm.iteye.com/blog/1054398"
---

```
(define (square x)
  (* x x))

(define (smallest-divisor n)
  (find-divisor n 2))

(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))

(define (divides? a b)
  (= (remainder b a) 0))
  
(smallest-divisor 199)

(smallest-divisor 1999)

(smallest-divisor 19999)
```

得出的结论是199的最小因子是199；1999的最小因子是1999；而19999的最小因子是17