페리 수열(farey sequence)

52
페리 수열 / farey sequence = 0 1 , 1 2 ,…, 페리 farey , 수열 sequence ,…, ohye ohyecloudy , cloudy .com ,…, 0+1 1+2 ohyecloudy / ohyecloudy.com 2013-06-08

Upload: -

Post on 27-Jun-2015

4.667 views

Category:

Technology


3 download

DESCRIPTION

페리 수열(farey sequence) 특징을 알아보고 관련된 project euler 문제 71, 72, 73을 풀어본다. 풀이 소스 코드를 제거하고 올림. (project euler에서 풀이를 공유하는 건 권장하지 않기 때문)

TRANSCRIPT

Page 1: 페리 수열(Farey sequence)

페리 수열 / farey sequence

𝐹𝑝𝑟𝑒𝑠𝑒𝑛𝑡𝑖𝑜𝑛 =0

1,1

2,… ,

페리

farey,

수열

sequence, … ,

ohye

ohyecloudy,cloudy

.com,… ,

0 + 1

1 + 2

ohyecloudy / ohyecloudy.com

2013-06-08

Page 2: 페리 수열(Farey sequence)

어쩌다가 이런 주제로…

• Project Euler 삽질이 이끌었다.

• 힘으로 푼 뒤 알아 보니 페리 수열

Page 3: 페리 수열(Farey sequence)

페리 수열 특징을 알아본다.

• + 관련된 Project Euler 문제 풀이

• 물론 Clojure로

Page 4: 페리 수열(Farey sequence)

페리 수열?

Page 5: 페리 수열(Farey sequence)

페리 수열 𝐹𝑛은 0 ≤ ℎ ≤ 𝑘 ≤ 𝑛 , gcd ℎ, 𝑘 = 1을

만족하는 ℎ

𝑘를 오름차순으로 나열한 수열

Page 6: 페리 수열(Farey sequence)

페리 수열 𝐹𝑛은 0 ≤ ℎ ≤ 𝑘 ≤ 𝑛 , gcd ℎ, 𝑘 = 1을

만족하는 ℎ

𝑘를 오름차순으로 나열한 수열

기약분수irreducible fraction

진분수proper fraction

Page 7: 페리 수열(Farey sequence)

𝐹1 =0

1,1

1

𝐹2 =0

1,1

2,1

1

𝐹3 =0

1,1

3,1

2,2

3,1

1

𝐹4 =0

1,1

4,1

3,1

2,2

3,3

4,1

1

Page 8: 페리 수열(Farey sequence)

어떤 특징을 가진 수열일까?

• 기본적인 3가지 특징만 소개

• 문제 풀면서 추가로~

Page 9: 페리 수열(Farey sequence)

𝐹𝑞에서

𝑎

𝑏<𝑝

𝑞<𝑐

𝑑인 𝑝

𝑞는 𝑎+𝑐

𝑏+𝑑로 구할 수 있다

1

Page 10: 페리 수열(Farey sequence)

𝐹𝑞에서

𝑎

𝑏<𝑝

𝑞<𝑐

𝑑인 𝑝

𝑞는 𝑎+𝑐

𝑏+𝑑로 구할 수 있다

1

알고 있는 항member

Page 11: 페리 수열(Farey sequence)

𝐹𝑞에서

𝑎

𝑏<𝑝

𝑞<𝑐

𝑑인 𝑝

𝑞는 𝑎+𝑐

𝑏+𝑑로 구할 수 있다

1

새롭게 추가되는 항

이상하게 더하고 있어.

Page 12: 페리 수열(Farey sequence)

1

3+1

2=2

5

• 패기 넘치는 분수 더하기

• 바보 셈freshman sum

• mediant

Page 13: 페리 수열(Farey sequence)

𝐹1 =0

1,1

1

𝐹2 =0

1,1

2,1

1

𝐹3 =0

1,1

3,1

2,2

3,1

1

0 + 1

1 + 1=1

2

0 + 1

1 + 2=1

3

1 + 1

2 + 1=2

3

Page 14: 페리 수열(Farey sequence)

𝐹𝑛은 𝐹𝑛−1을 포함한다.

2

Page 15: 페리 수열(Farey sequence)

𝐹1 =0

1,1

1

𝐹2 =0

1,1

2,1

1

𝐹3 =0

1,1

3,1

2,2

3,1

1

𝐹4 =0

1,1

4,1

3,1

2,2

3,3

4,1

1

Page 16: 페리 수열(Farey sequence)

𝐹1 =0

1,1

1

𝐹2 =0

1,1

2,1

1

𝐹3 =0

1,1

3,1

2,2

3,1

1

𝐹4 =0

1,1

4,1

3,1

2,2

3,3

4,1

1

Page 17: 페리 수열(Farey sequence)

𝑎+𝑐

𝑏+𝑑는 𝐹𝑏+𝑑에서 처음으로 나타난다.

3

Page 18: 페리 수열(Farey sequence)

𝑎+𝑐

𝑏+𝑑는 𝐹𝑏+𝑑에서 처음으로 나타난다.

3

분모와 같다

Page 19: 페리 수열(Farey sequence)

𝐹1 =0

1,1

1

𝐹2 =0

1,1

2,1

1

𝐹3 =0

1,1

3,1

2,2

3,1

1

𝐹4 =0

1,1

4,1

3,1

2,2

3,3

4,1

1

Page 20: 페리 수열(Farey sequence)

기본적인 특징들

•𝑎

𝑏<𝑝

𝑞<𝑐

𝑑인 𝑝

𝑞=𝑎+𝑐

𝑏+𝑑

• 𝐹𝑛은 𝐹𝑛−1을 포함

•𝑎+𝑐

𝑏+𝑑는 𝐹𝑏+𝑑에서 처음으로 나타남

Page 21: 페리 수열(Farey sequence)

이제 문제 풀이

• + 풀이에 필요한 추가적인 특징 설명

Page 22: 페리 수열(Farey sequence)

Consider the fraction, 𝑛 𝑑 , where 𝑛 and 𝑑 are positive integers. If 𝑛 < 𝑑 and HCF 𝑛, 𝑑 = 1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for 𝑑 ≤ 8 in ascending order of size, we get:

18 ,17 ,16 ,15 ,14 ,27 ,13 ,38 ,25 ,37 ,12 ,47 ,35 ,58 ,23 ,57 ,34 ,45 ,56 ,67 ,78

It can be seen that 2 5 is the fraction immediately to the left of 3 7 .

By listing the set of reduced proper fractions for 𝑑 ≤ 1,000,000 in ascending order of size, find the numerator of the fraction immediately to the left of 3 7 .

http://projecteuler.net/problem=71

Pro

ject E

ule

r #71

Page 23: 페리 수열(Farey sequence)

𝐹1,000,000에서 3

7보다 작은 수 중 가장 큰 수를 구하라.

Page 24: 페리 수열(Farey sequence)

𝐹8 = ⋯ ,25 ,37 ,⋯

𝐹12 = ⋯ ,25 ,512 ,37 ,⋯

𝐹19 = ⋯ ,512 ,819 ,37 ,⋯

𝐹1,000,000 = ⋯ , 𝑥,37 ,⋯

계산 필요 없다

Page 25: 페리 수열(Farey sequence)

(defn rhs-farey-pair [[a b c d]]

(let [mediant-n (+ a c)

mediant-d (+ b d)]

[mediant-n mediant-d c d]))

(rhs-farey-pair [2 5 3 7])

[5 12 3 7]

25 ,37

512 ,37

Page 26: 페리 수열(Farey sequence)

Consider the fraction, 𝑛 𝑑 , where 𝑛 and 𝑑 are positive integers. If 𝑛 < 𝑑 and HCF 𝑛, 𝑑 = 1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for 𝑑 ≤ 8 in ascending order of size, we get:

18 ,17 ,16 ,15 ,14 ,27 ,13 ,38 ,25 ,37 ,12 ,47 ,35 ,58 ,23 ,57 ,34 ,45 ,56 ,67 ,78

It can be seen that there are 3 fractions between 1 3 and 1 2 .

How many fractions lie between 1 3 and 1 2 in the sorted set of reduced proper fractions for 𝑑 ≤ 12,000?

http://projecteuler.net/problem=73

Pro

ject E

ule

r #73

Page 27: 페리 수열(Farey sequence)

𝐹12,000에서 1

3보다 크고

1

2보다 작은 항의 개수를

구하라.

Page 28: 페리 수열(Farey sequence)

𝐹12,000을 구하려면 𝐹11,999을 구해야 한다.

뒤에 붙는 형식이 아니라 중간중간 삽입 stack overflow 냄새

Page 29: 페리 수열(Farey sequence)

바로 다음 항을 구할 수 있나?

• 𝑛이 주어졌을 때.

Page 30: 페리 수열(Farey sequence)

𝐹12,000 =01 ,112,000 , 𝑥,⋯

이 두 항은 알 수 있다.

항상 0 1 ,1𝑛

Page 31: 페리 수열(Farey sequence)

𝑎

𝑏<𝑐

𝑑<𝑝

𝑞

즉, 이 두 항으로 다음 항을 구하는 방법

Page 32: 페리 수열(Farey sequence)

𝑎

𝑏<𝑐

𝑑=𝑎 + 𝑞

𝑏 + 𝑞<𝑝

𝑞

𝒌𝑐

𝒌𝑑=𝑎+𝑝

𝑏+𝑞를 만족하는 𝒌가 존재

𝑐

𝑑는 기약분수이기 때문

Page 33: 페리 수열(Farey sequence)

𝑝

𝑞=𝑘𝑐 − 𝑎

𝑘𝑑 − 𝑏

𝒌𝑐

𝒌𝑑=𝑎 + 𝑝

𝑏 + 𝑞

𝑛

Page 34: 페리 수열(Farey sequence)

max (𝑘) =𝑛 + 𝑏

𝑑

𝑘𝑑 − 𝑏 ≤ 𝑛

𝑘 ≤𝑛 + 𝑏

𝑑

Page 35: 페리 수열(Farey sequence)

𝐹𝑛 =…,𝑎

𝑏,𝑐

𝑑,𝑘𝑐−𝑎

𝑘𝑑−𝑏,…

𝑘 =𝑛 + 𝑏

𝑑

Page 36: 페리 수열(Farey sequence)

𝐹4 =0

1,1

4,1

3,1

2, 𝑥, …

𝑎

𝑏=1

3,𝑐

𝑑=1

2, 𝑘 =

4+3

2= 3

𝑝

𝑞=2

3

Page 37: 페리 수열(Farey sequence)

(defn next-term [[a b c d n]]

(let [k (int (/ (+ n b) d))

p (- (* k c) a)

q (- (* k d) b)]

[c d p q n]))

(next-term [1 3 1 2 4])

[1 2 2 3 4]

1

3,1

2 1

2,2

3

Page 38: 페리 수열(Farey sequence)

(defn farey-seq [n]

(take-while

#(<= % 1)

(map (fn [[a b _ _ _]] (/ a b))

(iterate next-term [0 1 1 n n]))))

𝐹2 =

0

1,1

2,1

1, 3

2,2

1, ⋯

진분수 가분수improper fraction

Page 39: 페리 수열(Farey sequence)

Consider the fraction, 𝑛 𝑑 , where 𝑛 and 𝑑 are positive integers. If 𝑛 < 𝑑 and HCF 𝑛, 𝑑 = 1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for 𝑑 ≤ 8 in ascending order of size, we get:

18 ,17 ,16 ,15 ,14 ,27 ,13 ,38 ,25 ,37 ,12 ,47 ,35 ,58 ,23 ,57 ,34 ,45 ,56 ,67 ,78

It can be seen that there are 21 elements in this set.

How many elements would be contained in the set of reduced proper fractions for 𝑑 ≤ 1,000,000?

http://projecteuler.net/problem=72

Pro

ject E

ule

r #72

Page 40: 페리 수열(Farey sequence)

|𝐹1,000,000| − 2를 구하라.

Page 41: 페리 수열(Farey sequence)

0

1,1

1 제외

|𝐹1,000,000| − 2를 구하라.

Page 42: 페리 수열(Farey sequence)

𝐹1,000,000을 구해서 개수를 센다면

• 세월아 네월아.

• 개수만 빠르게 구할 수 있을까?

Page 43: 페리 수열(Farey sequence)

𝐹𝑛에서는 𝐹𝑛−1에 없는 어떤 항이 추가되나?

gcd 𝑛, 𝑎 = 1을 만족하는 𝑎

𝑛 가 추가

Page 44: 페리 수열(Farey sequence)

𝐹𝑛 = 𝐹𝑛−1 + 𝜑(𝑛)

Euler's totient function 1 ≤ 𝑘 ≤ 𝑛, gcd 𝑛, 𝑘 = 1을 만족하는 정수 𝑘 개수

Page 45: 페리 수열(Farey sequence)

𝐹𝑛 = 1 + 𝜑(𝑚)

𝑛

𝑚=1

𝐹1 = 2이고 𝜑 1 = 1이기 때문

Page 46: 페리 수열(Farey sequence)

𝜑 𝑛 = 𝑛 (1 −1

𝑝)

𝑝|𝑛

𝑛을 나머지 없이 나누는 소수prime

Page 47: 페리 수열(Farey sequence)

𝜑 2 = 𝜑 21 = 2 1 −1

2= 1

𝜑 3 = 𝜑 31 = 3 1 −1

3= 2

𝜑 4 = 𝜑 22 = 4 1 −1

2= 2

𝜑 36 = 𝜑 2232 = 36 1 −1

21 −1

3= 12

Page 48: 페리 수열(Farey sequence)

(defn euler-totient [n]

(* n

(apply

*

(map #(- 1 (/ 1 %))

(filter

#(zero? (mod n %))

(take-while #(<= % n) primes))))))

Page 49: 페리 수열(Farey sequence)

(defn farey-seq-length [n]

(+ 1

(apply +

(map euler-totient

(range 1 (inc n))))))

Page 50: 페리 수열(Farey sequence)

마무리

Page 51: 페리 수열(Farey sequence)

• 페리 수열 𝐹𝑛은 0 ≤ ℎ ≤ 𝑘 ≤ 𝑛 , gcd ℎ, 𝑘 = 1

을 만족하는 ℎ

𝑘를 오름차순으로 나열한 수열.

• 바보 셈으로 다음 항을 구하는 재미있는 특징.

• Project Euler 문제는 어떤 수열에 관련된 문제인지 안 가르쳐 줘

Page 52: 페리 수열(Farey sequence)

CC BY-NC-SA 3.0

Reference Farey sequence – wikipedia 페리 수열 – wikipedia mediant – wikipedia 바보 셈에서 페리수열로 – 네이버캐스트

@ohyecloudy