プロジェクト

全般

プロフィール

操作

ホーム - Project Euler

Problem 52

Permuted Multiples

It can be seen that the number, $125874$, and its double, $251748$, contain exactly the same digits, but in a different order.

Find the smallest positive integer, $x$, such that $2x$, $3x$, $4x$, $5x$, and $6x$, contain the same digits.

置換倍数

125874を2倍すると251748となる. これは元の数125874と順番は違うが同じ数を含む.

2x, 3x, 4x, 5x, 6x が x と同じ数を含むような最小の正整数 x を求めよ.

(import (scheme base)
        (gauche base))

(define (number->list num)
  (assume (exact-integer? num))
  (assume (<= 0 num))
  (if (zero? num)
    '(0)
    (let loop ([rest num] [lis '()])
      (if (zero? rest)
        lis
        (loop (div rest 10)
              (cons (mod rest 10)
                    lis))))))

(define perm-num?
  (case-lambda
    [(num1 num2)
     (let ([num-list1 (number->list num1)]
           [num-list2 (number->list num2)])
       (if (not (= (length num-list1)
                   (length num-list2)))
         #f
         (let loop ([rest-1 num-list1]
                    [rest-2 num-list2])
           (cond
             [(null? rest-1) #t]
             [(find (cut = <> (car rest-1))
                    rest-2)
              (loop (cdr rest-1)
                    (delete (car rest-1) rest-2))]
             [else #f]))))]
    [(num1 num2 . num3)
     (and (perm-num? num1 num2)
          (apply perm-num? num2 num3))]))

(define find-6digit
  (let loop ([num1 102345])
    (if (< 165987 num1)
      (errorf "解が見つかりませんでした~%")
      (let ([num2 (* num1 2)]
            [num3 (* num1 3)]
            [num4 (* num1 4)]
            [num5 (* num1 5)]
            [num6 (* num1 6)])
        (if (perm-num? num1 num2 num3 num4 num5 num6)
          num1
          (loop (+ num1 1)))))))

(define answer-52 find-6digit)

(format #t "52: ~d~%" answer-52)

Noppi2024/01/18に更新 · 2件の履歴