home > Project Euler >

ForNext

Shut the fuck up and write some code

Problem 14

最長のコラッツ数列

正の整数に以下の式で繰り返し生成する数列を定義する.

n → n/2 (n が偶数)
n → 3n + 1 (n が奇数)

13からはじめるとこの数列は以下のようになる.

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
13から1まで10個の項になる. この数列はどのような数字からはじめても最終的には 1 になると考えられているが, まだそのことは証明されていない(コラッツ問題)

さて, 100万未満の数字の中でどの数字からはじめれば最長の数列を生成するか.

注意: 数列の途中で100万以上になってもよい

Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

更新日 : 2013.01.28

まづは、コラッツ数列がどんなものか見てみる

scala> def collatz_list(n: Long): List[Long] = {
     |     if      (n     == 1) List(1L)
     |     else if (n % 2 == 0) n::collatz_list(n / 2)
     |     else                 n::collatz_list(3 * n + 1)
     | }
collatz_list: (n: Long)List[Long]

scala> collatz_list(1)
res0: List[Long] = List(1)

scala> collatz_list(2)
res1: List[Long] = List(2, 1)

scala> collatz_list(3)
res2: List[Long] = List(3, 10, 5, 16, 8, 4, 2, 1)

scala> collatz_list(4)
res3: List[Long] = List(4, 2, 1)

scala> collatz_list(5)
res4: List[Long] = List(5, 16, 8, 4, 2, 1)

scala> collatz_list(6)
res5: List[Long] = List(6, 3, 10, 5, 16, 8, 4, 2, 1)

scala> collatz_list(7)
res6: List[Long] = List(7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1)

scala> collatz_list(8)
res7: List[Long] = List(8, 4, 2, 1)

scala> collatz_list(9)
res8: List[Long] = List(9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1)

scala> collatz_list(10)
res9: List[Long] = List(10, 5, 16, 8, 4, 2, 1)

途中から、同じ数列になるので、毎回作成せずに、辞書に保存しておく

scala> def add_map(lst:List[Long], map:collection.mutable.Map[Long, List[Long]]):Unit = {
     |     if (!lst.isEmpty) {
     |         if (!map.contains(lst.head)) {
     |             map += lst.head -> lst
     |             add_map(lst.tail, map)
     |         }
     |     }
     | }
add_map: (lst: List[Long], map: scala.collection.mutable.Map[Long,List[Long]])Unit
scala> def collatz_list(n:Long, map:collection.mutable.Map[Long, List[Long]]): List[Long] = {
     |     if (n == 1)
     |         List(1L)
     |     else {
     |         if (map.contains(n)) {
     |             map(n)
     |         } else {
     |             if (n % 2 == 0) n::collatz_list(n / 2,     map)
     |             else            n::collatz_list(3 * n + 1, map)
     |         }
     |     }
     | }
collatz_list: (n: Long, map: scala.collection.mutable.Map[Long,List[Long]])List[Long]
scala> val map = collection.mutable.Map[Long, List[Long]] ()
map: scala.collection.mutable.Map[Long,List[Long]] = Map()
scala> var lst = collatz_list(1, map)
lst: List[Long] = List(1)

scala> add_map(lst, map)

scala> map
res11: scala.collection.mutable.Map[Long,List[Long]] = Map(1 -> List(1))
scala> var lst = collatz_list(2, map)
lst: List[Long] = List(2, 1)

scala> add_map(lst, map)

scala> map
res13: scala.collection.mutable.Map[Long,List[Long]] = Map(1 -> List(1), 2 -> List(2, 1))
scala> var lst = collatz_list(3, map)
lst: List[Long] = List(3, 10, 5, 16, 8, 4, 2, 1)

scala> add_map(lst, map)

scala> map
res15: scala.collection.mutable.Map[Long,List[Long]] = Map(16 -> List(16, 8, 4, 2, 1), 5 -> List(5, 16, 8, 4, 2, 1), 10 -> List(10, 5, 16, 8, 4, 2, 1), 8 -> List(8, 4, 2, 1), 3 -> List(3, 10, 5, 16, 8, 4, 2, 1), 4 -> List(4, 2, 1), 1 -> List(1), 2 -> List(2, 1))

まとめ

scala> for (i <- 1 to 9) {
     |     var lst = collatz_list(i, map)
     |     println(lst.length)
     |     add_map(lst, map)
     | }
1
2
8
3
6
9
17
4
20
scala> var max_num = 0
max_num: Int = 0

scala> var max_idx = 0
max_idx: Int = 0

scala> for (i <- 1 to 99) {
     |     var lst = collatz_list(i, map)
     |     if (lst.length > max_num) {
     |         max_num = lst.length
     |         max_idx = i
     |     }
     |     add_map(lst, map)
     | }

scala> println(max_idx + ":" + max_num)
97:119
scala> var max_num = 0
max_num: Int = 0

scala> var max_idx = 0
max_idx: Int = 0

scala> for (i <- 1 to 9999) {
     |     var lst = collatz_list(i, map)
     |     if (lst.length > max_num) {
     |         max_num = lst.length
     |         max_idx = i
     |     }
     |     add_map(lst, map)
     | }

scala> println(max_idx + ":" + max_num)
6171:262
scala> var max_num = 0
max_num: Int = 0

scala> var max_idx = 0
max_idx: Int = 0

scala> for (i <- 1 to 999999) {
     |     var lst = collatz_list(i, map)
     |     if (lst.length > max_num) {
     |         max_num = lst.length
     |         max_idx = i
     |     }
     |     add_map(lst, map)
     | }

scala> println(max_idx + ":" + max_num)
837799:525

F#

更新日 : 2013.01.28

今回は、Scala 版とは違って、辞書にコラッツ数列そのものを格納するのではなく、
次の数だけ格納してみる。

> let rec collatz_list(n:int64): List<int64> =
-     if   (n      = 1L) then [1L]
-     elif (n % 2L = 0L) then n::collatz_list(n / 2L)
-     else                    n::collatz_list(3L * n + 1L)
- ;;

val collatz_list : int64 -> List<int64>
> collatz_list 1L;;
val it : List<int64> = [1L]
> collatz_list 9L;;
val it : List<int64> =
  [9L; 28L; 14L; 7L; 22L; 11L; 34L; 17L; 52L; 26L; 13L; 40L; 20L; 10L; 5L; 16L;
   8L; 4L; 2L; 1L]
> let rec add_map(lst:List<int64>, map:Map<int64, int64> byref) =
-     if not (List.isEmpty lst) then
-         if lst.Head <> 1L then
-             if not (Map.containsKey lst.Head map) then
-                 map <- map |> Map.add lst.Head lst.Tail.Head
-                 add_map(lst.Tail, &map)
- ;;

val add_map : List<int64> * byref<Map<int64,int64>> -> unit
> let rec collatz_list(n:int64, map:Map<int64, int64>): List<int64> =
-     if (n = 1L) then [1L]
-     else
-         if (Map.containsKey n map) then
-             n::collatz_list(map.[n], map)
-         else
-             if (n % 2L = 0L) then n::collatz_list(n / 2L,      map)
-             else                  n::collatz_list(3L * n + 1L, map)
- ;;

val collatz_list : int64 * Map<int64,int64> -> List<int64>
> let mutable map:Map<int64, int64> = Map.empty;;

val mutable map : Map<int64,int64> = map []

> let lst = collatz_list(1L, map);;

val lst : List<int64> = [1L]

> add_map(lst, &map);;
val it : unit = ()
> map;;
val it : Map<int64,int64> = map []
>
- let lst = collatz_list(2L, map);;

val lst : List<int64> = [2L; 1L]

> add_map(lst, &map);;
val it : unit = ()
> map;;
val it : Map<int64,int64> = map [(2L, 1L)]
>
- let lst = collatz_list(3L, map);;

val lst : List<int64> = [3L; 10L; 5L; 16L; 8L; 4L; 2L; 1L]

> add_map(lst, &map);;
val it : unit = ()
> map;;
val it : Map<int64,int64> =
  map
    [(2L, 1L); (3L, 10L); (4L, 2L); (5L, 16L); (8L, 4L); (10L, 5L); (16L, 8L)]
> let mutable map:Map<int64, int64> = Map.empty;;

val mutable map : Map<int64,int64> = map []

> for  i in [1L..9L] do
-     let lst = collatz_list(i, map)
-     printf "%d\n" (List.length lst)
-     add_map(lst, &map)
- ;;
1
2
8
3
6
9
17
4
20
val it : unit = ()
> let mutable map:Map<int64, int64> = Map.empty;;

val mutable map : Map<int64,int64> = map []

> let mutable max_num = 0;;

val mutable max_num : int = 0

> let mutable max_idx = 0L;;

val mutable max_idx : int64 = 0L

> for  i in [1L..99L] do
-     let lst = collatz_list(i, map)
-     if (List.length lst) > max_num then
-         max_num <- (List.length lst)
-         max_idx <- i
-     add_map(lst, &map)
- ;;
val it : unit = ()
> printf "%d:%d\n" max_idx max_num;;
97:119
val it : unit = ()
>
- let mutable map:Map<int64, int64> = Map.empty;;

val mutable map : Map<int64,int64> = map []

> let mutable max_num = 0;;

val mutable max_num : int = 0

> let mutable max_idx = 0L;;

val mutable max_idx : int64 = 0L

> for  i in [1L..9999L] do
-     let lst = collatz_list(i, map)
-     if (List.length lst) > max_num then
-         max_num <- (List.length lst)
-         max_idx <- i
-     add_map(lst, &map)
- ;;
val it : unit = ()
> printf "%d:%d\n" max_idx max_num;;
6171:262
val it : unit = ()
>
- let mutable map:Map<int64, int64> = Map.empty;;

val mutable map : Map<int64,int64> = map []

> let mutable max_num = 0;;

val mutable max_num : int = 0

> let mutable max_idx = 0L;;

val mutable max_idx : int64 = 0L

> for  i in [1L..999999L] do
-     let lst = collatz_list(i, map)
-     if (List.length lst) > max_num then
-         max_num <- (List.length lst)
-         max_idx <- i
-     add_map(lst, &map)
- ;;
val it : unit = ()
> printf "%d:%d\n" max_idx max_num;;
837799:525
val it : unit = ()

だめだ
めちゃ遅くて話にならない

C

C++

C++Builder

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system