Leap year: Difference between revisions

2,540 bytes added ,  2 months ago
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 4 users not shown)
Line 1,447:
 
=={{header|Clojure}}==
A simple approach:
<syntaxhighlight lang="clojure">(defn leap-year? [y]
(and (zero? (mod y 4)) (or (pos? (mod y 100)) (zero? (mod y 400)))))</syntaxhighlight>
(or (pos? (mod y 100))
(zero? (mod y 400)))))
}</syntaxhighlight>
A slightly terser, if slightly less obvious approach:
<syntaxhighlight lang="clojure">(defn leap-year? [y]
(condp #(zero? (mod %2 %1)) y
400 true
100 false
4 true
false))
</syntaxhighlight>
 
=={{header|CLU}}==
Line 2,226 ⟶ 2,238:
1996 2000</syntaxhighlight>
 
=={{header|Koka}}==
Chain of boolean expressions
<syntaxhighlight lang="koka">
pub fun is-leap-year(year: int)
(( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) )) && return 0</syntaxhighlight>
 
If-Then-Else
<syntaxhighlight lang="koka">
pub fun is-leap-year'(year: int)
year % (if year % 100 == 0 then 400 else 4) == 0</syntaxhighlight>
 
This approach use the buit-in libraries to create the february 28th date and the adds a day to it, which if it's in a leap year the next day wil be the 29th.
<syntaxhighlight lang="koka"> import std/time
import std/time/date
import std/time/time
 
pub fun is-leap-year''(year: int)
Date(year, 2, 28).time.add-days(1).day == 29</syntaxhighlight>
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">fun isLeapYear(year: Int) = year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)</syntaxhighlight>
Line 3,576 ⟶ 3,606:
}</syntaxhighlight>
 
Defining a bash function <tt>is_leap</tt> which accepts a YEAR argument (defaulting to zero), and uses no IO redirection, nor any extra processes.
<syntaxhighlight lang="shbash">is_leap() (( year=${1-0}, year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 )))
</syntaxhighlight>
local year=$(( 10#${1:?'Missing year'} ))
(( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) )) && return 0
return 1
}</syntaxhighlight>
 
Using the cal command: ''(note that this invokes two processes with IO piped between them and is relatively heavyweight compared to the above shell functions: leap and is_leap)''
Line 3,798 ⟶ 3,825:
return rem(Y/4)=0;
];</syntaxhighlight>
 
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
!yamlscript/v0
 
defn main(year):
say: "$year is $when-not(leap-year(year) 'not ')a leap year."
 
# Either one works:
 
defn leap-year(year):
((year % 4) == 0) && (((year % 100) > 0) || ((year % 100) == 0))
 
defn leap-year(year):
and:
zero?: (year % 4)
or:
pos?: (year % 100)
zero?: (year % 400)
</syntaxhighlight>
 
=={{header|Yorick}}==
Line 3,821 ⟶ 3,868:
@mod(year, @as(inttype, 4)) == 0);
}</syntaxhighlight>
 
Alternative (inspired by the C solution):
 
<syntaxhighlight lang="Zig">
/// The type that holds the current year, i.e. 2016
pub const Year = u16;
 
/// Returns true for years with 366 days
/// and false for years with 365 days.
pub fn isLeapYear(year: Year) bool {
// In the western Gregorian Calendar leap a year is
// a multiple of 4, excluding multiples of 100, and
// adding multiples of 400. In code:
//
// if (@mod(year, 4) != 0)
// return false;
// if (@mod(year, 100) != 0)
// return true;
// return (0 == @mod(year, 400));
 
// The following is equivalent to the above
// but uses bitwise operations when testing
// for divisibility, masking with 3 as test
// for multiples of 4 and with 15 as a test
// for multiples of 16. Multiples of 16 and
// 100 are, conveniently, multiples of 400.
const mask: Year = switch (year % 100) {
0 => 0b1111,
else => 0b11,
};
return 0 == year & mask;
}
 
test "isLeapYear" {
try testing.expectEqual(false, isLeapYear(2095));
try testing.expectEqual(true, isLeapYear(2096));
try testing.expectEqual(false, isLeapYear(2100));
try testing.expectEqual(true, isLeapYear(2400));
}
</syntaxhighlight>
 
=={{header|zkl}}==
15

edits