Project Euler

Project Euler Problem 62

steloflute 2012. 10. 5. 13:10
http://projecteuler.net/problem=62

Problem 62

30 January 2004

The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.


Answer:
127035954683


* Racket


#lang racket
(define (number->list n)
  (let loop ([n n]
             [lst '()])
    (if (> n 0)
        (let-values ([(q r) (quotient/remainder n 10)])
          (loop q (cons r lst)))
        ; else
        lst)))

(define (list->number lst)
  (let loop ([n 0]
             [ls lst])
    (if (> (length ls) 0)
        (let ([v (first ls)])
          (loop (+ (* n 10) v) (rest ls)))
        ; else
        n)))

(struct cell (cube count))
(define cubes (make-hash))
(let/ec break
  (let loop ([n 1])
    (define cube (* n n n))
    (define cube-sorted (list->number (sort (number->list cube) >)))   
    (hash-set! cubes cube-sorted (if (hash-has-key? cubes cube-sorted)
                                     (let ([v (hash-ref cubes cube-sorted)])
                                       (cell (cell-cube v) (add1 (cell-count v))))
                                     (cell cube 1)))
    (define v (hash-ref cubes cube-sorted))
    (when (= 5 (cell-count v))
      (printf "~v^3=~v\n" n (cell-cube v))
      (break)
      )
    (loop (add1 n))))




8384^3=127035954683

'Project Euler' 카테고리의 다른 글

Project Euler Problem 64  (0) 2012.10.07
Project Euler Problem 63  (0) 2012.10.05
Project Euler Problem 61  (0) 2012.10.04
(Haskell) Project Euler Solutions  (0) 2012.06.12
Project Euler Problem 54  (0) 2012.06.09