Problem 30 » 履歴 » バージョン 2
Noppi, 2024/01/12 14:26
| 1 | 1 | Noppi | [ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] |
|---|---|---|---|
| 2 | # [[Problem 30]] |
||
| 3 | |||
| 4 | ## Digit Fifth Powers |
||
| 5 | <p>Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: |
||
| 6 | \begin{align} |
||
| 7 | 1634 &= 1^4 + 6^4 + 3^4 + 4^4\\ |
||
| 8 | 8208 &= 8^4 + 2^4 + 0^4 + 8^4\\ |
||
| 9 | 9474 &= 9^4 + 4^4 + 7^4 + 4^4 |
||
| 10 | \end{align}</p> |
||
| 11 | |||
| 12 | As $1 = 1^4$ is not a sum it is not included. |
||
| 13 | |||
| 14 | The sum of these numbers is $1634 + 8208 + 9474 = 19316$. |
||
| 15 | |||
| 16 | Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. |
||
| 17 | |||
| 18 | ## 各桁の5乗 |
||
| 19 | <p>驚くべきことに, 各桁を4乗した数の和が元の数と一致する数は3つしかない. |
||
| 20 | \begin{align} |
||
| 21 | 1634 &= 1^4 + 6^4 + 3^4 + 4^4\\ |
||
| 22 | 8208 &= 8^4 + 2^4 + 0^4 + 8^4\\ |
||
| 23 | 9474 &= 9^4 + 4^4 + 7^4 + 4^4 |
||
| 24 | \end{align}</p> |
||
| 25 | |||
| 26 | ただし, $1 = 1^4$ は含まないものとする. この数たちの和は 1634 + 8208 + 9474 = 19316 である. |
||
| 27 | |||
| 28 | 各桁を5乗した数の和が元の数と一致するような数の総和を求めよ. |
||
| 29 | |||
| 30 | ```scheme |
||
| 31 | 2 | Noppi | (import (scheme base) |
| 32 | (gauche base)) |
||
| 33 | |||
| 34 | (define (fifth-power n) |
||
| 35 | (fold (^[n acc] |
||
| 36 | (+ (expt n 5) acc)) |
||
| 37 | 0 |
||
| 38 | (map (cut digit->integer <>) |
||
| 39 | (string->list (number->string n))))) |
||
| 40 | |||
| 41 | (define fifth-power-list |
||
| 42 | ; (* (expt 9 5) 6) -> 354294 |
||
| 43 | ; (* (expt 9 5) 7) -> 413343 |
||
| 44 | ; なので、2 ~ 354294 まで求めればよい |
||
| 45 | (map (^n (cons n (fifth-power n))) |
||
| 46 | (iota 354293 2))) |
||
| 47 | |||
| 48 | (define digit-fifth-powers |
||
| 49 | (map (cut car <>) |
||
| 50 | (filter (^c (= (car c) (cdr c))) |
||
| 51 | fifth-power-list))) |
||
| 52 | |||
| 53 | (define answer-30 |
||
| 54 | (apply + digit-fifth-powers)) |
||
| 55 | |||
| 56 | (format #t "30: ~d~%" answer-30) |
||
| 57 | 1 | Noppi | ``` |