Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Great start. Now let's add leap years. =)


    days y m = 28 + (m + (m `div` 8)) `mod` 2 + 2 `mod` m + 2 * (1 `div` m) 
               + ((2 `div` m) `mod` 2) * (leap y)
      where leap y = multiple 4 y - multiple 100 y + multiple 400 y
              where multiple int y = ((y + (int - 1)) `mod` int + 1) `div` int


but that's trival, isn't that already solved?

    function f(x) { return 28 + maybeLeap(x) + (...); }
I remember it's something related with "(x % 4) == 0" and "not ends_with(00)"


OP is trying to express the day count as a pure math expression. maybeLeap() probably won't do here.


Except for every 400th year.


how about this?

        leap = function(x) { 
          if ((x % 4) != 0) {
            return 0;
          }
          return Number(
            (x % 100) != 0 || (x % 400) == 0
          );
        }

        function(x) { return 28 + leap(x) + (...) };




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: