プロジェクト

全般

プロフィール

Problem 63 » 履歴 » バージョン 2

Noppi, 2024/01/28 11:09

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 63]]
3
4
## Powerful Digit Counts
5
The $5$-digit number, $16807=7^5$, is also a fifth power. Similarly, the $9$-digit number, $134217728=8^9$, is a ninth power.
6
7
How many $n$-digit positive integers exist which are also an $n$th power?
8
9
## べき乗の桁の個数
10
$5$桁の数 $16807=7^5$は自然数を5乗した数である. 同様に$9$桁の数 $134217728=8^9$も自然数を9乗した数である.
11
12
自然数を $n$ 乗して得られる $n$ 桁の正整数は何個あるか?
13
14
```scheme
15 2 Noppi
(import (scheme base)
16
        (gauche base)
17
        (scheme inexact))
18
19
(define (digit-num num)
20
  (assume (exact-integer? num))
21
  (assume (not (negative? num)))
22
  (if (zero? num)
23
    1
24
    (+ (floor->exact (log num 10))
25
       1)))
26
27
(define (powerful-digit-list)
28
  (let loop1 ([base 1]
29
              [rest (iota 9 1)]
30
              [result '()])
31
    (if (null? rest)
32
      (reverse result)
33
      (let loop2 ([index (car rest)]
34
                  [rest rest]
35
                  [result result])
36
        (cond
37
          [(< 9 index)
38
           (loop1 (+ base 1)
39
                  rest
40
                  result)]
41
          [(< (digit-num (expt index base)) base)
42
           (loop2 (+ index 1)
43
                  (delete index rest)
44
                  result)]
45
          [(= (digit-num (expt index base)) base)
46
           (loop2 (+ index 1)
47
                  rest
48
                  (cons `(,index . ,base) result))]
49
          [else
50
            (loop1 (+ base 1) rest result)])))))
51
52
(define answer-63
53
  (length (powerful-digit-list)))
54
55
(format #t "63: ~d~%" answer-63)
56 1 Noppi
```