ようこそ。睡眠不足なプログラマのチラ裏です。

Project Euler第1問目

Project Eulerはじめました。(冷やし中華的な意味で)


問題

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.


10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、これらの合計は 23 になる。
同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。
http://projecteuler.net/index.php?section=problems&id=1


Haskellでの解答

{-Project Euler Q001-}
main :: IO()
main = return (answer [1..999]) >>= print

answer :: [Int] -> Int
answer = sum . map judge

judge :: Int -> Int
judge x
    | x `mod` 3 ==  0 || x `mod` 5 ==  0 = x
    | otherwise = 0 


C#3.0での解答

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            (from num in Enumerable.Range(1, 999)
             where num % 3 == 0 || num % 5 == 0
             select num).Aggregate(Enumerable.Repeat(default(int), 1), (nums, i)
                                   => from num in nums
                                      select num + i
                                   ).ToList().ForEach(i => Console.WriteLine(i.ToString()));
            Console.ReadLine();
        }
    }
}

実行結果

233168

暇をみて、解けるものだけでも解いていこうと思います。へっぽこなりに(^ω^;)