Leap year: Difference between revisions

3,839 bytes added ,  2 months ago
(added Zig)
 
(21 intermediate revisions by 11 users not shown)
Line 1,277:
Press any key to continue . . .</pre>
 
 
=={{header|bc}}==
<syntaxhighlight lang="bc">define l(y) {
if (y % 100 == 0) y /= 100
if (y % 4 == 0) return(1)
return(0)
}</syntaxhighlight>
 
=={{header|BCPL}}==
Line 1,322 ⟶ 1,329:
1997 is not a leap year.
2000 is a leap year.</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Leap ← 0=4|100÷˜⍟(0=|)¨⊢</syntaxhighlight>
Or:
<syntaxhighlight lang="bqn">Leap ← -˝0=4‿100‿400|⌜⊢</syntaxhighlight>
Test:
<syntaxhighlight lang="bqn">Leap 1900‿1996‿1998‿1999‿2000‿2024‿2100</syntaxhighlight>
{{out}}
<pre>⟨ 0 1 0 0 1 1 0 ⟩</pre>
 
=={{header|Bracmat}}==
Line 1,431 ⟶ 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 1,777 ⟶ 1,805:
 
<pre>true</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func leapyear y .
if y mod 4 = 0 and (y mod 100 <> 0 or y mod 400 = 0)
return 1
.
return 0
.
print leapyear 2000
</syntaxhighlight>
 
=={{header|Ela}}==
Line 1,948 ⟶ 1,987:
 
=={{header|Fōrmulæ}}==
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Leap_year}}
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Solution'''
In '''[https://formulae.org/?example=Leap_year this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Leap year 01.png]]
 
In a more concise way:
 
[[File:Fōrmulæ - Leap year 02.png]]
 
[[File:Fōrmulæ - Leap year 03.png]]
 
[[File:Fōrmulæ - Leap year 04.png]]
 
=={{header|GAP}}==
Line 2,148 ⟶ 2,196:
<syntaxhighlight lang="javascript">// Month values start at 0, so 1 is for February
var isLeapYear = function (year) { return new Date(year, 1, 29).getDate() === 29; };</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE leapyear == dup 100 div null rotate choice 4 rem null.</syntaxhighlight>
 
=={{header|jq}}==
Line 2,174 ⟶ 2,225:
 
=={{header|K}}==
=== K3 ===
<syntaxhighlight lang="k"> leapyear:{(+/~x!'4 100 400)!2}
{{works with|Kona}}
Leap year predicate:
<syntaxhighlight lang="k"> lyp:{(+/~x!'4 100 400)!2}
 
lyp'1996+!6
1 0 0 0 1 0</syntaxhighlight>
Leap year selection:
<syntaxhighlight lang="k"> lys:{a@&lyp'a:x}
 
alys@&leapyear'a:1900,1994,1996,1997,2000
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)</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 2,941 ⟶ 3,018:
: (isLeapYear 1700)
-> NIL</pre>
 
=={{header|PL/0}}==
{{trans|Tiny BASIC}}
<syntaxhighlight lang="pascal">
var isleap, year;
 
procedure checkifleap;
begin
isleap := 0;
if (year / 4) * 4 = year then
begin
if year - (year / 100) * 100 <> 0 then isleap := 1;
if year - (year / 400) * 400 = 0 then isleap := 1
end;
end;
 
begin
year := 1759;
while year <= 2022 do
begin
call checkifleap;
if isleap = 1 then ! year;
year := year + 1
end
end.
</syntaxhighlight>
{{out}}
<pre>
1760
1764
1768
1772
1776
1780
1784
1788
1792
1796
1804
1808
1812
1816
1820
1824
1828
1832
1836
1840
1844
1848
1852
1856
1860
1864
1868
1872
1876
1880
1884
1888
1892
1896
1904
1908
1912
1916
1920
1924
1928
1932
1936
1940
1944
1948
1952
1956
1960
1964
1968
1972
1976
1980
1984
1988
1992
1996
2000
2004
2008
2012
2016
2020
</pre>
 
=={{header|PL/I}}==
Line 3,235 ⟶ 3,405:
C SETON LR</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP 100 MOD 4 400 IFTE MOD NOT
≫ '<span style="color:blue">LEAP?</span>' STO
 
2000 <span style="color:blue">LEAP?</span>
2001 <span style="color:blue">LEAP?</span>
2020 <span style="color:blue">LEAP?</span>
2100 <span style="color:blue">LEAP?</span>
{{out}}
<pre>
4: 1
3: 0
2: 1
1: 0
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'date'
Line 3,241 ⟶ 3,427:
 
The leap? method is aliased as gregorian_leap? And yes, there is a julian_leap? method.
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
<syntaxhighlight lang="RPL">
≪ DUP 100 MOD 4 400 IFTE MOD NOT ≫
'LEAP?' STO
 
2000 LEAP?
2001 LEAP?
2020 LEAP?
2100 LEAP?
</syntaxhighlight>
{{out}}
<pre>
4:1
3:0
2:1
1:0
</pre>
 
=={{header|Rust}}==
Line 3,439 ⟶ 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,605 ⟶ 3,769:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var isLeapYear = Fn.new { |y|
return ((y % 4 == 0) && (y % 100!= 0)) || (y % 400 == 0)
}
Line 3,661 ⟶ 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,684 ⟶ 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