Leap year: Difference between revisions

2,501 bytes added ,  2 months ago
(add →‎Joy)
 
(18 intermediate revisions by 10 users not shown)
Line 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,438 ⟶ 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,784 ⟶ 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,955 ⟶ 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,184 ⟶ 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
a@&leapyear'a:1900,1994,1996,1997,2000
1 0 0 0 1 0</syntaxhighlight>
Leap year selection:
<syntaxhighlight lang="k"> lys:{a@&lyp'a:x}
 
lys@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 3,338 ⟶ 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,344 ⟶ 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,542 ⟶ 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,708 ⟶ 3,769:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var isLeapYear = Fn.new { |y|
return ((y % 4 == 0) && (y % 100!= 0)) || (y % 400 == 0)
}
Line 3,764 ⟶ 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,787 ⟶ 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