home
>
Project Euler
>
ForNext
Shut the fuck up and write some code
Problem 9
特別なピタゴラス数
ピタゴラス数(ピタゴラスの定理を満たす自然数)とは で以下の式を満たす数の組である.
例えば, である.
となるピタゴラスの三つ組が一つだけ存在する.
これらの積 を計算しなさい.
Special Pythagorean triplet
A Pythagorean triplet is a set of three natural numbers, , for which,
.
For example, .
There exists exactly one Pythagorean triplet for which .
Find the product .
考え方
なので
これを
に代入して
この式を満たす、a, b を探す
VBScript
JScript
Perl
PHP
Python
Ruby
PowerShell
Scala
更新日 : 2013.01.23
scala> (1 to 9) res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> (1 to 9). | map(a => (a + 1 to 10)) res1: scala.collection.immutable.IndexedSeq[scala.collection.immutable.Range.Inclusive] = Vector(Range(2, 3, 4, 5, 6, 7, 8, 9, 10), Range(3, 4, 5, 6, 7, 8, 9, 10), Range(4, 5, 6, 7, 8, 9, 10), Range(5, 6, 7, 8, 9, 10), Range(6, 7, 8, 9, 10), Range(7, 8, 9, 10), Range(8, 9, 10), Range(9, 10), Range(10)) scala> (1 to 9). | map(a => (a + 1 to 10).map(b => (a, b))) res2: scala.collection.immutable.IndexedSeq[scala.collection.immutable.IndexedSeq[(Int, Int)]] = Vector(Vector((1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10)), Vector((2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (2,10)), Vector((3,4), (3,5), (3,6), (3,7), (3,8), (3,9), (3,10)), Vector((4,5), (4,6), (4,7), (4,8), (4,9), (4,10)), Vector((5,6), (5,7), (5,8), (5,9), (5,10)), Vector((6 scala> (1 to 9). | flatMap(a => (a + 1 to 10).map(b => (a, b))) res3: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (2,10), (3,4), (3,5), (3,6), (3,7), (3,8), (3,9), (3,10), (4,5), (4,6), (4,7), (4,8), (4,9), (4,10), (5,6), (5,7), (5,8), (5,9), (5,10), (6,7), (6,8), (6,9), (6,10), (7,8), (7,9), (7,10), (8,9), (8,10), (9,10)) scala> (1 to 9). | flatMap(a => (a + 1 to 10).map(b => (a, b))). | collect{case (a,b) if a % 3 == 0 || b % 3 == 0 => (a, b)} res4: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,3), (1,6), (1,9), (2,3), (2,6), (2,9), (3,4), (3,5), (3,6), (3,7), (3,8), (3,9), (3,10), (4,6), (4,9), (5,6), (5,9), (6,7), (6,8), (6,9), (6,10), (7,9), (8,9), (9,10)) scala> (1 to 499). | flatMap(a => (a + 1 to 500).map(b => (a, b))). | collect{case (a,b) if 1000 * (a + b) - (a * b) == 500000 => (a, b, 1000 - (a + b))} res5: scala.collection.immutable.IndexedSeq[(Int, Int, Int)] = Vector((200,375,425)) scala> (1 to 499). | flatMap(a => (a + 1 to 500).map(b => (a, b))). | filter(t => 1000 * (t._1 + t._2) - (t._1 * t._2) == 500000) res6: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((200,375)) scala> (1 to 499). | flatMap(a => (a + 1 to 500).map(b => (a, b))). | filter(t => 1000 * (t._1 + t._2) - (t._1 * t._2) == 500000). | flatMap(t => List(t._1, t._2, 1000 - (t._1 + t._2))) res7: scala.collection.immutable.IndexedSeq[Int] = Vector(200, 375, 425) scala> (1 to 499). | flatMap(a => (a + 1 to 500).map(b => (a, b))). | filter(t => 1000 * (t._1 + t._2) - (t._1 * t._2) == 500000). | flatMap(t => List(t._1, t._2, 1000 - (t._1 + t._2))). | reduceLeft(_*_) res8: Int = 31875000
F#
更新日 : 2013.01.23
> [1..9] - ;; val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9] > [1..9] - |> List.map(fun i -> [i..9]) - ;; val it : int list list = [[1; 2; 3; 4; 5; 6; 7; 8; 9]; [2; 3; 4; 5; 6; 7; 8; 9]; [3; 4; 5; 6; 7; 8; 9]; [4; 5; 6; 7; 8; 9]; [5; 6; 7; 8; 9]; [6; 7; 8; 9]; [7; 8; 9]; [8; 9]; [9]] > [1..9] - |> List.map(fun i -> [i..9] |> List.map(fun j -> (i, j))) - ;; val it : (int * int) list list = [[(1, 1); (1, 2); (1, 3); (1, 4); (1, 5); (1, 6); (1, 7); (1, 8); (1, 9)]; [(2, 2); (2, 3); (2, 4); (2, 5); (2, 6); (2, 7); (2, 8); (2, 9)]; [(3, 3); (3, 4); (3, 5); (3, 6); (3, 7); (3, 8); (3, 9)]; [(4, 4); (4, 5); (4, 6); (4, 7); (4, 8); (4, 9)]; [(5, 5); (5, 6); (5, 7); (5, 8); (5, 9)]; [(6, 6); (6, 7); (6, 8); (6, 9)]; [(7, 7); (7, 8); (7, 9)]; [(8, 8); (8, 9)]; [(9, 9)]] > [1..9] - |> List.map(fun i -> [i..9] |> List.map(fun j -> (i, j))) - |> List.collect id - ;; val it : (int * int) list = [(1, 1); (1, 2); (1, 3); (1, 4); (1, 5); (1, 6); (1, 7); (1, 8); (1, 9); (2, 2); (2, 3); (2, 4); (2, 5); (2, 6); (2, 7); (2, 8); (2, 9); (3, 3); (3, 4); (3, 5); (3, 6); (3, 7); (3, 8); (3, 9); (4, 4); (4, 5); (4, 6); (4, 7); (4, 8); (4, 9); (5, 5); (5, 6); (5, 7); (5, 8); (5, 9); (6, 6); (6, 7); (6, 8); (6, 9); (7, 7); (7, 8); (7, 9); (8, 8); (8, 9); (9, 9)] > [1..9] - |> List.map(fun i -> [i..9] |> List.map(fun j -> (i, j))) - |> List.collect id - |> List.filter(fun (a, b) -> a % 3 = 0 || b % 3 = 0) - ;; val it : (int * int) list = [(1, 3); (1, 6); (1, 9); (2, 3); (2, 6); (2, 9); (3, 3); (3, 4); (3, 5); (3, 6); (3, 7); (3, 8); (3, 9); (4, 6); (4, 9); (5, 6); (5, 9); (6, 6); (6, 7); (6, 8); (6, 9); (7, 9); (8, 9); (9, 9)] > [1..499] - |> List.map(fun a -> [a+1..600] |> List.map(fun b -> (a, b))) - |> List.collect id - |> List.filter(fun (a, b) -> 1000 * (a + b) - (a * b) = 500000) - ;; val it : (int * int) list = [(200, 375)] > [1..499] - |> List.map(fun a -> [a+1..600] |> List.map(fun b -> (a, b))) - |> List.collect id - |> List.filter(fun (a, b) -> 1000 * (a + b) - (a * b) = 500000) - |> List.collect(fun (a, b) -> [a; b; 1000 - (a + b)]) - ;; val it : int list = [200; 375; 425] > [1..499] - |> List.map(fun a -> [a+1..600] |> List.map(fun b -> (a, b))) - |> List.collect id - |> List.filter(fun (a, b) -> 1000 * (a + b) - (a * b) = 500000) - |> List.collect(fun (a, b) -> [a; b; 1000 - (a + b)]) - |> List.reduce(*) - ;; val it : int = 31875000