---
title: "用clojure解决euler problem 9"
author: "hotcharm"
date: "2012-07-08T22:25:00Z"
tags: []
slug: "clojure-euler-problem-9"
draft: false
lastmod: "2012-07-15T18:28:54Z"
id: 1594602
url: "https://hotcharm.iteye.com/blog/1594602"
---

问题描述：

A Pythagorean triplet is a set of three natural numbers,`a`![<](http://projecteuler.net/images/symbol_lt.gif)`b`![<](http://projecteuler.net/images/symbol_lt.gif)`c`, for which,

`a`2+`b`2=`c`2

For example, 32+ 42= 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which`a`+`b`+`c`= 1000.  
Find the product`abc`.

解决方案：

```
(ns euler-problem-9.core)
(defn square
  [n]
  (* n n))

(defn pythagorean-triplet-sum-1000
  []
  (loop [a 1 b 1]
    (if (= (+ (square a) (square b)) (square (- 1000 a b)))
      (* a b (- 1000 a b))
      (if (and (= a 333) (= b 499))
        "no answer"
        (if (= b 499)
          (recur (inc a) 1)
          (recur a (inc b)))))))
(pythagorean-triplet-sum-1000)

(defn pythagorean-triplet-sum-1000-a-b-c
  []
  (loop [a 1 b 1]
    (if (= (+ (square a) (square b)) (square (- 1000 a b)))
      (list a b (- 1000 a b))
      (if (and (= a 333) (= b 499))
        "no answer"
        (if (= b 499)
          (recur (inc a) 1)
          (recur a (inc b)))))))
(pythagorean-triplet-sum-1000-a-b-c)
```

abc的乘积为：31875000

a，b，c分别为200，375 ，425