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

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

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

いまさら聞けないアルゴリズム その1「ハノイの塔」 Part.2

前回に引き続きまして、Haskell版「ハノイの塔」も書いてみた。

{-ハノイの塔-}
main :: IO ()
main = putStr $ concat $ [write x | x <- hanoi "A" "B" "C" 3]  

hanoi :: String -> String -> String -> Int -> [(String, String)]
hanoi _ _ _ 0 = []
hanoi a b c n =  hanoi a c b (n - 1) ++ [(a, b)] ++ hanoi c b a (n - 1)

write :: (String, String) -> String
write x =  (fst x) ++ " -> " ++ (snd x) ++ "\n"

実行結果

A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B

やっぱり、再帰は関数型言語の方が断然美しく書ける感じだ。
というか、ひさびさにHaskell書いたら若干忘れかけてた(^ω^;)

いまさら聞けないアルゴリズム その1「ハノイの塔」

なんの変哲もないC#で書いた「ハノイの塔」です。
それ以上でもそれ以下でもありません。もちろん模範解答というわけでもない。

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        private static void Main(string[] args)
        {
            List<string> result = Hanoi(3, "A", "B", "C");
            Console.WriteLine(result.Count + "回移動");

            foreach (string s in result){
                Console.WriteLine(s);
            }
            Console.Read();
        }

        private static List<string> Hanoi(int n, string a, string b, string c)
        {
            List<String> result = new List<string>();
            Hanoi(n, a, b, c, ref result);
            return result;
        }

        private static void Hanoi(int n, string a, string b, string c, ref List<String> result)
        {
            if (n > 0){
                Hanoi(n - 1, a, c, b, ref result);
                result.Add(a + "から" + c + "へ" + n.ToString() + "を移動");
                Hanoi(n - 1, b, a, c, ref result);
            }
        }
    }
}

稀に再帰も書けないような自称プログラマな人に出会うことがあるんだけど、
正直勘弁してもらいたい。企業が未経験な人を採ること自体は別によいんだけど、
教育をしないのは問題かと。IT業界大丈夫ですかね?きっと大丈夫じゃないね・・。