This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,4 @@
Determine whether a given year is a leap year in the Gregorian calendar.
'''See Also'''
* [[wp:Leap year|Leap year (wiki)]]

View file

@ -0,0 +1,2 @@
---
note: Date and time

View file

@ -0,0 +1,19 @@
MODE YEAR = INT, MONTH = INT, DAY = INT;
PROC year days = (YEAR year)DAY: # Ignore 1752 CE for the moment #
( month days(year, 2) = 28 | 365 | 366 );
PROC month days = (YEAR year, MONTH month) DAY:
( month | 31,
28 + ABS (year MOD 4 = 0 AND year MOD 100 /= 0 OR year MOD 400 = 0),
31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
PROC is leap year = (YEAR year)BOOL: year days(year)=366;
test:(
[]INT test cases = (1900, 1994, 1996, 1997, 2000);
FOR i TO UPB test cases DO
YEAR year = test cases[i];
printf(($g(0)" is "b("","not ")"a leap year."l$, year, is leap year(year)))
OD
)

View file

@ -0,0 +1,7 @@
function leapyear( year )
{
if ( year % 100 == 0 )
return ( year % 400 == 0 )
else
return ( year % 4 == 0 )
}

View file

@ -0,0 +1,6 @@
public function isLeapYear(year:int):Boolean {
if (year % 100 == 0) {
return (year % 400 == 0);
}
return (year % 4 == 0);
}

View file

@ -0,0 +1,21 @@
-- Incomplete code, just a sniplet to do the task. Can be used in any package or method.
-- Adjust the type of Year if you use a different one.
function Is_Leap_Year (Year : Integer) return Boolean is
begin
if Year rem 100 = 0 then
return Year rem 400 = 0;
else
return Year rem 4 = 0;
end if;
end Is_Leap_Year;
-- Should be faster without conditional and only one remainder (the other two are really binary and)
function Is_Leap_Year (Year : Integer) return Boolean is
begin
return (Year rem 4 = 0) and ((Year rem 100 /= 0) or (Year rem 16 = 0));
end Is_Leap_Year;
-- To improve speed a bit more, use with
pragma Inline (Is_Leap_Year);

View file

@ -0,0 +1,8 @@
on leap_year(y)
if (y mod 100 is equal to 0) then
return (y mod 400 is equal to 0)
end if
return (y mod 4 is equal to 0)
end leap_year
leap_year(1900)

View file

@ -0,0 +1,6 @@
MsgBox % "The year 1600 was " . (IsLeapYear(1600) ? "" : "not ") . "a leap year"
IsLeapYear(Year)
{
Return, !Mod(Year, 100) && !Mod(Year, 400) && !Mod(Year, 4)
}

View file

@ -0,0 +1,15 @@
;AutoIt Version: 3.2.10.0
$Yr=1600
ConsoleWrite ($Yr &" is ")
if isLeapYear($Yr) Then
ConsoleWrite ("leap year")
Else
ConsoleWrite ("not leap year")
EndIf
Func isLeapYear($year)
If IsInt($year/100) Then
Return IsInt($year/400)
EndIf
Return IsInt($year/4)
EndFunc

View file

@ -0,0 +1,25 @@
DECLARE FUNCTION diy% (y AS INTEGER)
DECLARE FUNCTION isLeapYear% (yr AS INTEGER)
DECLARE FUNCTION year% (date AS STRING)
PRINT isLeapYear(year(DATE$))
FUNCTION diy% (y AS INTEGER)
IF y MOD 4 THEN
diy = 365
ELSEIF y MOD 100 THEN
diy = 366
ELSEIF y MOD 400 THEN
diy = 365
ELSE
diy = 366
END IF
END FUNCTION
FUNCTION isLeapYear% (yr AS INTEGER)
isLeapYear = (366 = diy(yr))
END FUNCTION
FUNCTION year% (date AS STRING)
year% = VAL(RIGHT$(date, 4))
END FUNCTION

View file

@ -0,0 +1,12 @@
REPEAT
INPUT "Enter a year: " year%
IF FNleap(year%) THEN
PRINT ;year% " is a leap year"
ELSE
PRINT ;year% " is not a leap year"
ENDIF
UNTIL FALSE
END
DEF FNleap(yr%)
= (yr% MOD 4 = 0) AND ((yr% MOD 400 = 0) OR (yr% MOD 100 <> 0))

View file

@ -0,0 +1,15 @@
( leap-year
=
. mod$(!arg.100):0
& `(mod$(!arg.400):0) { The backtick skips the remainder of the OR operation,
even if the tested condition fails. }
| mod$(!arg.4):0
)
& 1600 1700 1899 1900 2000 2006 2012:?tests
& whl
' ( !tests:%?test ?tests
& ( leap-year$!test&out$(!test " is a leap year")
| out$(!test " is not a leap year")
)
)
& ;

View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdbool.h>
bool is_leap_year(int year)
{
return !(year % 4) && year % 100 || !(year % 400);
}
int main()
{
int test_case[] = {1900, 1994, 1996, 1997, 2000}, key, end;
for (key = 0, end = sizeof(test_case)/sizeof(test_case[0]); key < end; ++key) {
int year = test_case[key];
printf("%d is %sa leap year.\n", year, is_leap_year(year) ? "" : "not ");
}
}

View file

@ -0,0 +1,2 @@
(defn leap-year? [y]
(and (zero? (mod y 4)) (or (pos? (mod y 100)) (zero? (mod y 400)))))

View file

@ -0,0 +1,4 @@
(defun leap-year-p (year)
(destructuring-bind (fh h f)
(mapcar #'(lambda (n) (zerop (mod year n))) '(400 100 4))
(or fh (and (not h) f))))

View file

@ -0,0 +1,13 @@
import std.algorithm;
bool leapYear(in uint y) pure nothrow {
return (y % 4) == 0 && (y % 100 || (y % 400) == 0);
}
void main() {
auto good = [1600, 1660, 1724, 1788, 1848, 1912, 1972, 2032,
2092, 2156, 2220, 2280, 2344, 2348];
auto bad = [1698, 1699, 1700, 1750, 1800, 1810, 1900, 1901,
1973, 2100, 2107, 2200, 2203, 2289];
assert(filter!leapYear(bad ~ good).equal(good));
}

View file

@ -0,0 +1,21 @@
function IsLeapYear(y : Integer) : Boolean;
begin
Result:= (y mod 4 = 0)
and ( ((y mod 100) <> 0)
or ((y mod 400) = 0) );
end;
const good : array [0..13] of Integer =
[1600,1660,1724,1788,1848,1912,1972,2032,2092,2156,2220,2280,2344,2348];
const bad : array [0..13] of Integer =
[1698,1699,1700,1750,1800,1810,1900,1901,1973,2100,2107,2200,2203,2289];
var i : Integer;
PrintLn('Checking leap years');
for i in good do
if not IsLeapYear(i) then PrintLn(i);
PrintLn('Checking non-leap years');
for i in bad do
if IsLeapYear(i) then PrintLn(i);

View file

@ -0,0 +1,19 @@
program TestLeapYear;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
Year: Integer;
begin
Write('Enter the year: ');
Readln(Year);
if IsLeapYear(Year) then
Writeln(Year, ' is a Leap year')
else
Writeln(Year, ' is not a Leap year');
Readln;
end.

View file

@ -0,0 +1,2 @@
isLeap y | y % 100 == 0 = y % 400 == 0
| else = y % 4 == 0

View file

@ -0,0 +1,7 @@
-module(gregorian).
-export([leap/1]).
leap(Year) when Year rem 4 /= 0 -> false;
leap(Year) when Year rem 100 /= 0 -> true;
leap(Year) when Year rem 400 == 0 -> true;
leap(_) -> false.

View file

@ -0,0 +1,3 @@
function isLeapYear(integer year)
return remainder(year,4)=0 and remainder(year,100)!=0 or remainder(year,400)=0
end function

View file

@ -0,0 +1,2 @@
USING: calendar prettyprint ;
2011 leap-year? .

View file

@ -0,0 +1,4 @@
: leap-year? ( y -- ? )
dup 400 mod 0= if drop true exit then
dup 100 mod 0= if drop false exit then
4 mod 0= ;

View file

@ -0,0 +1,17 @@
program leap
implicit none
write(*,*) leap_year([1900, 1996, 1997, 2000])
contains
pure elemental function leap_year(y) result(is_leap)
implicit none
logical :: is_leap
integer,intent(in) :: y
is_leap = (mod(y,4)==0 .and. .not. mod(y,100)==0) .or. (mod(y,400)==0)
end function leap_year
end program leap

View file

@ -0,0 +1,8 @@
IsLeapYear := function(n)
return (n mod 4 = 0) and ((n mod 100 <> 0) or (n mod 400 = 0));
end;
# alternative using built-in function
IsLeapYear := function(n)
return DaysInYear(n) = 366;
end;

View file

@ -0,0 +1,3 @@
func isLeap(year int) bool {
return year%400 == 0 || year%4 == 0 && year%100 != 0
}

View file

@ -0,0 +1 @@
(1900..2012).findAll {new GregorianCalendar().isLeapYear(it)}.each {println it}

View file

@ -0,0 +1,9 @@
import Data.List
import Control.Monad
import Control.Arrow
leaptext x b | b = show x ++ " is a leap year"
| otherwise = show x ++ " is not a leap year"
isleapsf j | 0==j`mod`100 = 0 == j`mod`400
| otherwise = 0 == j`mod`4

View file

@ -0,0 +1 @@
isleap = foldl1 ((&&).not).flip map [400, 100, 4]. ((0==).).mod

View file

@ -0,0 +1,6 @@
*Main> mapM_ (putStrLn. (ap leaptext isleap)) [1900,1994,1996,1997,2000]
1900 is not a leap year
1994 is not a leap year
1996 is a leap year
1997 is not a leap year
2000 is a leap year

View file

@ -0,0 +1,8 @@
procedure main(arglist)
every y := !([2000,1900,2012]|||arglist) do
write("The year ",y," is ", leapyear(y) | "not ","a leap year.")
end
procedure leapyear(year) #: determine if year is leap
if (numeric(year) % 4 = 0 & year % 100 ~= 0) | (numeric(year) % 400 = 0) then return
end

View file

@ -0,0 +1 @@
isLeap=: 0 -/@:= 4 100 400 |/ ]

View file

@ -0,0 +1,2 @@
isLeap 1900 1996 1997 2000
0 1 0 1

View file

@ -0,0 +1 @@
new java.util.GregorianCalendar().isLeapYear(year)

View file

@ -0,0 +1,5 @@
public static boolean isLeapYear(int year){
if(year % 100 == 0)
return year % 400 == 0;
return year % 4 == 0;
}

View file

@ -0,0 +1 @@
var isLeapYear = function (year) { return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0); };

View file

@ -0,0 +1,2 @@
// Month values start at 0, so 1 is for February
var isLeapYear = function (year) { return new Date(year, 1, 29).getDate() === 29; };

View file

@ -0,0 +1,4 @@
leapyear:{(+/~x!'4 100 400)!2}
a@&leapyear'a:1900,1994,1996,1997,2000
1996 2000

View file

@ -0,0 +1,10 @@
if leap(1996)then
print "leap"
else
print "ordinary"
end if
wait
function leap(n)
leap=date$("2/29/";n)
end function

View file

@ -0,0 +1,14 @@
year = 1908
select case
case year mod 400 = 0
leapYear = 1
case year mod 4 = 0 and year mod 100 <> 0
leapYear = 1
case else
leapYear = 0
end select
if leapYear = 1 then
print year;" is a leap year."
else
print year;" is not a leap year."
end if

View file

@ -0,0 +1,6 @@
to multiple? :n :d
output equal? 0 modulo :n :d
end
to leapyear? :y
output ifelse multiple? :y 100 [multiple? :y 400] [multiple? :y 4]
end

View file

@ -0,0 +1,5 @@
leap_year(Year) :-
( mod(Year, 4) =:= 0, mod(Year, 100) =\= 0 ->
true
; mod(Year, 400) =:= 0
).

View file

@ -0,0 +1,3 @@
function isLeapYear(year)
return year%4==0 and (year%100~=0 or year%400==0)
end

View file

@ -0,0 +1,3 @@
function TrueFalse = isLeapYear(year)
TrueFalse = (eomday(year,2) == 29);
end

View file

@ -0,0 +1,2 @@
ILY(X) ;IS IT A LEAP YEAR?
QUIT ((X#4=0)&(X#100'=0))!((X#100=0)&(X#400=0))

View file

@ -0,0 +1 @@
isLeapYear[y_]:= y~Mod~4 == 0 && (y~Mod~100 != 0 || y~Mod~400 == 0)

View file

@ -0,0 +1,2 @@
leapyearp(year) := is(mod(year, 4) = 0 and
(mod(year, 100) # 0 or mod(year, 400) = 0))$

View file

@ -0,0 +1,7 @@
<?php
function isLeapYear(year) {
if (year % 100 == 0) {
return (year % 400 == 0);
}
return (year % 4 == 0);
}

View file

@ -0,0 +1,4 @@
<?php
function isLeapYear(year) {
return (date('L', mktime(0, 0, 0, 2, 1, year)) === '1')
}

View file

@ -0,0 +1,7 @@
sub leap {
my $yr = $_[0];
if ($yr % 100 == 0) {
return ($yr % 400 == 0);
}
return ($yr % 4 == 0);
}

View file

@ -0,0 +1 @@
sub leap {!($_[0] % 100) ? !($_[0] % 400) : !($_[0] % 4)}

View file

@ -0,0 +1,6 @@
use Date::Manip;
Date_LeapYear($year);
use Date::Manip::Base;
my $dmb = new Date::Manip::Base;
$dmb->leapyear($year);

View file

@ -0,0 +1,2 @@
(de isLeapYear (Y)
(bool (date Y 2 29)) )

View file

@ -0,0 +1,10 @@
leap_year(L) :-
partition(is_leap_year, L, LIn, LOut),
format('leap years : ~w~n', [LIn]),
format('not leap years : ~w~n', [LOut]).
is_leap_year(Year) :-
R4 is Year mod 4,
R100 is Year mod 100,
R400 is Year mod 400,
( (R4 = 0, R100 \= 0); R400 = 0).

View file

@ -0,0 +1,4 @@
?- leap_year([1900,1994,1996,1997,2000 ]).
leap years : [1996,2000]
not leap years : [1900,1994,1997]
L = [1900,1994,1996,1997,2000].

View file

@ -0,0 +1,2 @@
import calendar
calendar.isleap(year)

View file

@ -0,0 +1,4 @@
def is_leap_year(year):
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0

View file

@ -0,0 +1,8 @@
import datetime
def is_leap_year(year):
try:
datetime.date(year, 2, 29)
except ValueError:
return False
return True

View file

@ -0,0 +1,7 @@
isLeapYear <- function(year) {
ifelse(year%%100==0, year%%400==0, year%%4==0)
}
for (y in c(1900, 1994, 1996, 1997, 2000)) {
print(paste(y, " is ", ifelse(isLeapYear(y), "", "not "), "a leap year.", sep=""))
}

View file

@ -0,0 +1,3 @@
leapyear: procedure; parse arg yr
return yr//400==0 | (yr//100\==0 & yr//4==0)

View file

@ -0,0 +1 @@
leapyear: return arg(1)//400==0 | (arg(1)//100\==0 & arg(1)//4==0)

View file

@ -0,0 +1,2 @@
(define (leap-year? y)
(and (zero? (modulo y 4)) (or (positive? (modulo y 100)) (zero? (modulo y 400)))))

View file

@ -0,0 +1,4 @@
require 'date'
def leap_year?(year)
Date.new(year).leap?
end

View file

@ -0,0 +1,3 @@
[2000, 1900, 1800, 1700, 1600, 1500, 1400].each do |year|
print year, (leap_year? year) ? " is" : " is not", " a leap year.\n"
end

View file

@ -0,0 +1,4 @@
require 'date'
def leap_year?(year)
Date.new(year, 1, 1, Date::GREGORIAN).leap?
end

View file

@ -0,0 +1,2 @@
//use Java's calendar class
new java.util.GregorianCalendar().isLeapYear(year)

View file

@ -0,0 +1,8 @@
def isLeapYear(year:Int)=if (year%100==0) year%400==0 else year%4==0;
//or use Java's calendar class
def isLeapYear(year:Int):Boolean = {
val c = new java.util.GregorianCalendar
c.setGregorianChange(new java.util.Date(Long.MinValue))
c.isLeapYear(year)
}

View file

@ -0,0 +1,4 @@
(define (leap-year? n)
(apply (lambda (a b c) (or a (and (not b) c)))
(map (lambda (m) (zero? (remainder n m)))
'(400 100 4))))

View file

@ -0,0 +1,7 @@
proc isleap1 {year} {
return [expr {($year % 4 == 0) && (($year % 100 != 0) || ($year % 400 == 0))}]
}
isleap1 1988 ;# => 1
isleap1 1989 ;# => 0
isleap1 1900 ;# => 0
isleap1 2000 ;# => 1

View file

@ -0,0 +1,7 @@
proc isleap2 year {
return [expr {[clock format [clock scan "$year-02-29" -format "%Y-%m-%d"] -format "%m-%d"] eq "02-29"}]
}
isleap2 1988 ;# => 1
isleap2 1989 ;# => 0
isleap2 1900 ;# => 0
isleap2 2000 ;# => 1