Leap year: Difference between revisions

2,240 bytes added ,  2 months ago
(→‎{{header|RPL}}: move before Ruby + typos)
 
(12 intermediate revisions by 8 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,197 ⟶ 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)</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,547 ⟶ 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,713 ⟶ 3,769:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var isLeapYear = Fn.new { |y|
return ((y % 4 == 0) && (y % 100!= 0)) || (y % 400 == 0)
}
Line 3,769 ⟶ 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,792 ⟶ 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