Problem52 - Project Euler

30分プログラム、その328。Problem52 - Project Euler

125874を2倍すると251748となる. これは元の数125874と同じ数を含む.
2x, 3x, 4x, 5x, 6xがxと同じ数を含むような最小の正整数xを求めよ.

普通に1から順に調べてみた。数字を桁ごとに分解してソートして比較することで、条件を満たすか調べてる。

使い方

gosh> (loop 1)
....
142857

ソースコード

#! /opt/local/bin/gosh
;; -*- mode:scheme; coding:utf-8 -*-
;;
;; problem52.scm -
;;
;; Copyright(C) 2008 by mzp
;; Author: MIZUNO Hiroki / mzpppp at gmail dot com
;; http://howdyworld.org
;;
;; Timestamp: 2008/06/27 22:39:58
;;
;; This program is free software; you can redistribute it and/or
;; modify it under MIT Lincence.
;;

(use srfi-1)

(define (int->list n)
  (unfold (cut = <> 0)
	  (cut remainder <> 10)
	  (cut quotient  <> 10)
	  n))
(define (list->int xs)
  (fold (lambda (x y) (+ (* 10 y) x)) 0 xs))

(define (satisfy x)
  (apply = (map (lambda (i) (list->int (sort (int->list (* i x)))))
		(iota 6 1))))

(define (loop n)
  (print n)
  (if (satisfy n)
      n
      (loop (+ n 1))))