プロジェクト

全般

プロフィール

Problem 47 » 履歴 » バージョン 1

Noppi, 2024/01/17 01:32

1 1 Noppi
[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]]
2
# [[Problem 47]]
3
4
## Distinct Primes Factors
5
<p>The first two consecutive numbers to have two distinct prime factors are:</p>
6
\begin{align}
7
14 &amp;= 2 \times 7\\
8
15 &amp;= 3 \times 5.
9
\end{align}
10
<p>The first three consecutive numbers to have three distinct prime factors are:</p>
11
\begin{align}
12
644 &amp;= 2^2 \times 7 \times 23\\
13
645 &amp;= 3 \times 5 \times 43\\
14
646 &amp;= 2 \times 17 \times 19.
15
\end{align}
16
17
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
18
19
## 異なる素因数
20
<p>それぞれ2つの異なる素因数を持つ連続する2つの数が最初に現れるのは:</p>
21
\begin{align}
22
14 &amp;= 2 \times 7\\
23
15 &amp;= 3 \times 5.
24
\end{align}
25
<p>それぞれ3つの異なる素因数を持つ連続する3つの数が最初に現れるのは:</p>
26
\begin{align}
27
644 &amp;= 2^2 \times 7 \times 23\\
28
645 &amp;= 3 \times 5 \times 43\\
29
646 &amp;= 2 \times 17 \times 19.
30
\end{align}
31
32
最初に現れるそれぞれ4つの異なる素因数を持つ連続する4つの数を求めよ. その最初の数はいくつか?
33
34
```scheme
35
(import (scheme base)
36
        (gauche base))
37
38
(define (factorize num)
39
  "異なる素因数を列挙する"
40
  (assume (exact-integer? num))
41
  (assume (<= 2 num))
42
  (let loop ([index 2] [rest num] [lis '()])
43
    (cond
44
      [(= rest 1) lis]
45
      [(< num (* index index))
46
       (cons rest lis)]
47
      [(and (zero? (mod rest index))
48
            (not (null? lis))
49
            (= index (car lis)))
50
       (loop index
51
             (div rest index)
52
             lis)]
53
      [(zero? (mod rest index))
54
       (loop index
55
             (div rest index)
56
             (cons index lis))]
57
      [else
58
        (loop (+ index 1) rest lis)])))
59
60
(define first-4factorize-sequence
61
  (let loop ([n1 645])
62
    (let ([n2 (+ n1 1)]
63
          [n3 (+ n1 2)]
64
          [n4 (+ n1 3)])
65
      (if (and (<= 4 (length (factorize n1)))
66
               (<= 4 (length (factorize n2)))
67
               (<= 4 (length (factorize n3)))
68
               (<= 4 (length (factorize n4))))
69
        (cons* n1 n2 n3 n4 '())
70
        (loop (+ n1 1))))))
71
72
(define answer-47
73
  (car first-4factorize-sequence))
74
75
(format #t "47: ~d~%" answer-47)
76
```