challenge 除算・余剰を使わずに閏年

ある西暦が閏年か否かを判定するプログラムを書いてください。 ただし、除算・余剰を求める演算子、組み込み関数、ライブラリ関数等を使用してはいけません。 また、閏年は以下のように定義されています。 1. 西暦年が4で割り切れる年は閏年 2. ただし、西暦年が100で割り切れる年は平年 3. ただし、西暦年が400で割り切れる年は閏年

Posted feedbacks - Erlang

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
-module(leap_year).
-import(lists).
-export([is_leap_year/1]).

list_multiple_of(B,M,[]) ->
    if
        B > M -> [];
        true -> list_multiple_of(B,M,[B])
    end;
list_multiple_of(B,M,L) ->
    C = lists:nth(1,L),
    if
        C + B > M -> L;
        true -> list_multiple_of(B,M,[C + B|L])
    end.

list_leap_year(M) ->
    ((list_multiple_of(4,M,[]) -- list_multiple_of(100,M,[])) ++ list_multiple_of(400,M,[])) -- list_multiple_of(1000,M,[]).

is_leap_year(Y) ->
    length(lists:filter(fun(X) -> X == Y end, list_leap_year(Y))) > 0.

Index

Feed

Other

Link

Pathtraq

loading...