Long year: Difference between revisions
Added Easylang
(Added Easylang) |
|||
(18 intermediate revisions by 11 users not shown) | |||
Line 10:
{{trans|C++}}
<
F p(year)
R (year + (year I/ 4) - (year I/ 100) + (year I/ 400)) % 7
Line 17:
L(year) 2000..2100
I is_long_year(year)
print(year, end' ‘ ’)</
{{out}}
Line 25:
=={{header|Action!}}==
<
RETURN ((y+(y/4)-(y/100)+(y/400)) MOD 7)
Line 50:
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Long_year.png Screenshot from Atari 8-bit computer]
Line 64:
=={{header|Ada}}==
The Ada calendar package handles dates for years 1901 through 2399. This program outputs all the long years within that range.
<
-- Calculate long years
-- Reference: https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year
Line 98:
end loop;
end Main;
</syntaxhighlight>
{{output}}
<pre>
Line 114:
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<
# finding years where 1st Jan or 31st Dec are Thursdays #
# returns the day of the week of the specified date (d/m/y), Sunday = 1 #
Line 137:
IF is long year( year ) THEN print( ( " ", whole( year, 0 ) ) ) FI
OD
END</
{{out}}
<pre>
Line 145:
=={{header|ALGOL-M}}==
<
COMMENT
Line 202:
END
</syntaxhighlight>
{{output}}
<pre>
Line 211:
=={{header|ALGOL W}}==
Uses the Day_of_week procedure from the Day_of_the_week task.
<
% this is equivalent to finding years where %
% 1st Jan or 31st Dec are Thursdays %
Line 236:
if isLongYear( year ) then writeon( I_W := 5, S_W := 0, year )
end for_year
end.</
{{out}}
<pre>
long years 2000-2099: 2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099
</pre>
=={{header|Amazing Hopper}}==
Task solves with Amazing Hopper flavour Basico:
<syntaxhighlight lang="c">
#include <basico.h>
#proto esañolargo(_X_)
algoritmo
año=1800, c=5
imprimir ("Long (53 week) years between 1800 and 2100:\n\n" )
iterar grupo ( ++año, #( año<=2100 ), \
cuando ( #( es año largo( año )==4 || es año largo( año-1 )==3 ) ){ \
imprimir ( año, " ", solo si( #( c==0 ) , NL; c=6 ), --c ) } )
terminar
subrutinas
es año largo (y)
retornar ' #( (y + floor(y / 4) - floor(y / 100) + floor(y / 400)) % 7 ) '
</syntaxhighlight>
{{out}}
<pre>
Long (53 week) years between 1800 and 2100:
1801 1807 1812 1818 1824 1829
1835 1840 1846 1852 1857 1863
1868 1874 1880 1885 1891 1896
1903 1908 1914 1920 1925 1931
1936 1942 1948 1953 1959 1964
1970 1976 1981 1987 1992 1998
2004 2009 2015 2020 2026 2032
2037 2043 2048 2054 2060 2065
2071 2076 2082 2088 2093 2099
</pre>
=={{header|APL}}==
<
isolongyear ← {(4 = dec31weekday ⍵) ∨ 3 = dec31weekday ⍵ - 1}</
{{Out}}
Line 255 ⟶ 295:
=={{header|AppleScript}}==
<
-- ISO8601 weeks begin on Mondays and belong to the year in which they have the most days.
-- A year which begins on a Thursday, or which begins on a Wednesday and is a leap year,
Line 272 ⟶ 312:
end repeat
return longYears</
{{output}}
Line 279 ⟶ 319:
On the other hand, since the cycle repeats every 400 years, it's possible to cheat with a precalculated look-up list:
<
return (y mod 400 is in {4, 9, 15, 20, 26, 32, 37, 43, 48, 54, 60, 65, 71, 76, 82, 88, 93, 99, 105, 111, 116, 122, 128, 133, 139, 144, 150, 156, 161, 167, 172, 178, 184, 189, 195, 201, 207, 212, 218, 224, 229, 235, 240, 246, 252, 257, 263, 268, 274, 280, 285, 291, 296, 303, 308, 314, 320, 325, 331, 336, 342, 348, 353, 359, 364, 370, 376, 381, 387, 392, 398})
end isLongYear
Line 288 ⟶ 328:
end repeat
return longYears</
=={{header|Arturo}}==
Line 294 ⟶ 334:
{{trans|Nim}}
<
date: to :date .format: "dd/MM/yyyy" ~"01/01/|year|"
Line 303 ⟶ 343:
print "Years with 53 weeks between 2000 and 2100:"
print select 2000..2100 => longYear?</
{{out}}
Line 311 ⟶ 351:
=={{header|AutoHotkey}}==
<
A := Mod(y + floor(y/4) - floor(y/100) + floor(y/400), 7)
y--, B := Mod(y + floor(y/4) - floor(y/100) + floor(y/400), 7)
return A=4 || B=3
}</
Examples:<
y := 1999+A_Index
res .= Long_year(y) ? Y " ": ""
}
MsgBox % "Long Years 2000-2100 : " res
return</
{{out}}
<pre>Long Years 2000-2100 : 2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099 </pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LONG_YEAR.AWK
BEGIN {
Line 358 ⟶ 398:
return(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 373 ⟶ 413:
==={{header|Applesoft BASIC}}===
{{trans|Commodore BASIC}}
<
20 DEF FN WD(Y) = FN M7(Y + INT (Y / 4) - INT (Y / 100) + INT (Y / 400))
30 DEF FN LY(Y) = (4 = FN WD(Y)) OR (3 = FN WD(Y - 1))
Line 381 ⟶ 421:
70 PRINT : FOR Y = S TO E
80 IF FN LY(Y) THEN PRINT S$Y;:S$ = " "
90 NEXT Y</
==={{header|ASIC}}===
{{trans|Commodore BASIC}}
<
REM Long year
CLS
Line 428 ⟶ 468:
WD = WD MOD 7
RETURN
</syntaxhighlight>
{{out}}
<pre>
Line 439 ⟶ 479:
==={{header|BASIC256}}===
<
return (y + int(y/4) - int(y/100) + int(y/400)) mod 7
end function
Line 450 ⟶ 490:
if isLongYear(y) then print y
next y
end</
{{out}}
<pre>2004
Line 473 ⟶ 513:
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
REM The function as per specification.
Line 492 ⟶ 532:
NEXT
PRINT
ENDPROC</
{{out}}
<pre>
Line 499 ⟶ 539:
The long years between 2100 and 2200 are 2105 2111 2116 2122 2128 2133 2139 2144 2150 2156 2161 2167 2172 2178 2184 2189 2195
</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans| Commodore BASIC}}
<syntaxhighlight lang="qbasic">10 cls
20 rem WD(Y) = WEEKDAY OF Y-12-31, 0 = SUNDAY
30 def fnwd(Y) = (Y + INT(Y / 4) - INT(Y / 100) + INT(Y / 400)) mod 7
40 rem LY(Y) = NONZERO IF Y IS LONG
50 def fnly(Y) = (4 = FNWD(Y)) OR (3 = FNWD(Y-1))
60 print "**** LIST OF ISO LONG YEARS ****"
70 input "START YEAR? ",s
80 input "END YEAR? ",e
90 print
100 for y = s to e
110 if fn ly(y) then print y,
120 next y
130 print
140 end</syntaxhighlight>
{{out}}
<pre>Similar as Commodore BASIC entry.</pre>
==={{header|Commodore BASIC}}===
<
110 DEF FNM7(N) = N - 7*INT(N / 7)
120 :
Line 518 ⟶ 578:
250 : IF FNLY(Y) THEN PRINT Y,
260 NEXT Y
270 PRINT</
{{Out}}
Line 533 ⟶ 593:
==={{Header|FreeBASIC}}===
<
return ( y + int(y/4) - int(y/100) + int(y/400) ) mod 7
end function
Line 545 ⟶ 605:
print islongyear(1998)
print islongyear(2020)
print islongyear(2021)</
{{out}}
<pre>
Line 552 ⟶ 612:
false
</pre>
==={{header|Gambas}}===
{{trans|BASIC256}}
<syntaxhighlight lang="vbnet">Public Sub Main()
For y As Integer = 2000 To 2100
If isLongYear(y) Then Print y,
Next
End
Function p(y As Integer) As Integer
Return (y + (y \ 4) - (y \ 100) + (y \ 400)) Mod 7
End Function
Function isLongYear(y As Integer) As Boolean
If p(y) = 4 Then Return True
If p(y - 1) = 3 Then Return True
Return False
End Function</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|GW-BASIC}}===
<
20 X = Y
30 GOSUB 100
Line 565 ⟶ 651:
100 P = X + INT(X/4) - INT(X/100) + INT(X/400)
110 P = P MOD 7
120 RETURN</
==={{header|IS-BASIC}}===
<
110 DEF RD(Y)=Y*365+INT(Y/4)-INT(Y/100)+INT(Y/400)
120 DEF LONGYEAR(Y)=(4=MOD(RD(Y),7)) OR(4=MOD((RD(Y-1)+1),7))
Line 576 ⟶ 662:
160 IF LONGYEAR(Y) THEN PRINT Y,
170 NEXT
180 PRINT</
==={{header|MSX Basic}}===
{{trans| Commodore BASIC}}
<syntaxhighlight lang="qbasic">10 CLS
20 REM WD(Y) = WEEKDAY OF Y-12-31, 0 = SUNDAY
30 DEF FNWD(Y) = (Y + INT(Y/4) - INT(Y/100) + INT(Y/400)) MOD 7
40 REM LY(Y) = NONZERO IF Y IS LONG
50 DEF FNLY(Y) = (4 = FNWD(Y)) OR (3 = FNWD(Y-1))
60 PRINT "*** LIST OF ISO LONG YEARS ***"
70 INPUT "START YEAR ";S
80 INPUT " END YEAR ";E
90 PRINT
100 FOR Y = S TO E
110 IF FNLY(Y) THEN PRINT Y,
120 NEXT Y
130 PRINT
140 END</syntaxhighlight>
{{out}}
<pre>Similar as Commodore BASIC entry.</pre>
==={{header|Nascom BASIC}}===
{{trans|Commodore BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<
10 REM Long year
20 REM FNM7(N)=MOD(N,7)
Line 602 ⟶ 707:
190 PRINT
200 END
</syntaxhighlight>
{{out}}
<pre>
Line 610 ⟶ 715:
1998 2004 2009 2015 2020 2026 2032 2037 2043
</pre>
==={{header|Palo Alto Tiny BASIC}}===
{{trans|Commodore BASIC}}
<syntaxhighlight lang="basic">
10 REM LONG YEAR
20 PRINT "*** LIST OF ISO LONG YEARS ***"
30 INPUT "START YEAR"B
40 INPUT "END YEAR"E
50 FOR Y=B TO E
60 GOSUB 200
70 IF L#0 PRINT Y," ",
80 NEXT Y
90 PRINT
100 STOP
190 REM L NONZERO IF Y IS LONG
200 LET L=0,J=Y
210 GOSUB 400
220 IF W=4 LET L=1
230 LET J=Y-1
240 GOSUB 400
250 IF W=3 LET L=1
260 RETURN
370 REM CALCULATE DAY OF WEEK W GIVEN
380 REM OF J-12-31, GIVEN YEAR J
390 REM SUNDAY = 0, SATURDAY = 6
400 LET W=J+J/4-J/100+J/400
410 LET W=W-(W/7)*7
420 RETURN
</syntaxhighlight>
{{out}}
<pre>
*** LIST OF ISO LONG YEARS ***
START YEAR:1995
END YEAR:2045
1998 2004 2009 2015 2020 2026 2032 2037 2043
</pre>
==={{header|PureBasic}}===
{{trans|BASIC256}}
<
ProcedureReturn (y + Int(y/4) - Int(y/100) + Int(y/400)) % 7
EndProcedure
Line 631 ⟶ 772:
Print(""): Input()
CloseConsole()
EndIf</
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|
{{Works with|QB|4.x}}
{{Works with|PDS|7.x}}
Line 643 ⟶ 782:
{{Works with|VB-DOS|1.0}}
Translated from Delphi
<syntaxhighlight lang="vb">
DEFINT A-Z
Line 675 ⟶ 814:
p% = (Yr + INT(Yr / 4) - INT(Yr / 100) + INT(Yr / 400)) MOD 7
END FUNCTION
</syntaxhighlight>
{{Out}}
<pre>
Line 688 ⟶ 827:
</pre>
==={{header|
{{trans|BASIC256}}
<syntaxhighlight lang="vb">function p(y)
p = (y + int(y/4) - int(y/100) + int(y/400)) mod 7
end function
function isLongYear(y)
isLongYear = (p(y) = 4) or (p(y -1) = 3)
end function
for y = 2000 to 2100
if isLongYear(y) then print y
next y
end</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|S-BASIC}}===
<syntaxhighlight lang="basic">
$lines
Line 717 ⟶ 873:
comment
The simplest of several possible tests is that
any
Thursday is "long", i.e.,
end
function islongyear(yr = integer) = integer
Line 738 ⟶ 894:
next year
end</
{{output}}
<pre>
Line 747 ⟶ 903:
==={{header|Tiny BASIC}}===
<
INPUT Y
LET X = Y
Line 762 ⟶ 918:
LET P = P - 7
GOTO 110
</syntaxhighlight>
{{out}}
<pre>
Line 776 ⟶ 932:
==={{header|True BASIC}}===
{{trans|BASIC256}}
<
FUNCTION isLongYear(y)
Line 791 ⟶ 947:
IF isLongYear(y) > 0 THEN PRINT y
NEXT y
END</
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|Visual Basic}}===
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
{{works with|VBA|Access 97}}
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<syntaxhighlight lang="vb">Option Explicit
Function IsLongYear(ByVal Year As Integer) As Boolean
Select Case vbThursday
Case VBA.DatePart("w", VBA.DateSerial(Year, 1, 1)), _
VBA.DatePart("w", VBA.DateSerial(Year, 12, 31))
IsLongYear = True
End Select
End Function
Sub Main()
'test
Dim l As Long
For l = 1990 To 2021
Select Case l
Case 1992, 1998, 2004, 2009, 2015, 2020
Debug.Assert IsLongYear(l)
Case Else
Debug.Assert Not IsLongYear(l)
End Select
Next l
End Sub
</syntaxhighlight>
===={{header|Visual Basic for DOS}}====
{{trans|Delphi}}
<syntaxhighlight lang="vb">OPTION EXPLICIT
DECLARE FUNCTION p (Yr AS INTEGER) AS INTEGER
DECLARE FUNCTION LongYear (Yr AS INTEGER) AS INTEGER
DIM iYi AS INTEGER, iYf AS INTEGER, i AS INTEGER
CLS
PRINT "This program calculates which are 53-week years in a range."
PRINT
INPUT "Initial year"; iYi
INPUT "Final year (could be the same)"; iYf
IF iYf >= iYi THEN
FOR i = iYi TO iYf
IF LongYear(i) THEN
PRINT i; " ";
END IF
NEXT i
END IF
PRINT
PRINT
PRINT "End of program."
END
FUNCTION p (Yr AS INTEGER) AS INTEGER
p = (Yr + INT(Yr / 4) - INT(Yr / 100) + INT(Yr / 400)) MOD 7
END FUNCTION
FUNCTION LongYear (Yr AS INTEGER) AS INTEGER
LongYear = (p(Yr) = 4) OR (p(Yr - 1) = 3)
END FUNCTION
</syntaxhighlight>
==={{header|XBasic}}===
{{works with|Windows XBasic}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">PROGRAM "LongYear"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
DECLARE FUNCTION p (y)
DECLARE FUNCTION isLongYear (y)
FUNCTION Entry ()
FOR y = 2000 TO 2100
IF isLongYear(y) THEN PRINT y,
NEXT y
END FUNCTION
FUNCTION p (y)
RETURN (y + INT(y/4) - INT(y/100) + INT(y/400)) MOD 7
END FUNCTION
FUNCTION isLongYear (y)
RETURN (p(y) = 4) OR (p(y - 1) = 3)
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
==={{header|Yabasic}}===
{{trans|BASIC256}}
<
return mod((y + int(y/4) - int(y/100) + int(y/400)), 7)
end sub
Line 808 ⟶ 1,054:
for y = 2000 to 2100
if isLongYear(y)
next y
end</
{{out}}
<pre>Same as BASIC256 entry.</pre>
=={{header|BCPL}}==
<
let p(y) = (y + y/4 - y/100 + y/400) rem 7
Line 824 ⟶ 1,068:
let start() be
for y = 2000 to 2100
if longyear(y) do writef("%N*N", y)</
{{out}}
<pre>2004
Line 846 ⟶ 1,090:
=={{header|C sharp}}==
<
using System.Collections.Generic;
using System.Linq;
Line 863 ⟶ 1,107:
}
}</
{{out}}
<pre>
Line 870 ⟶ 1,114:
=={{header|C}}==
<
#include <math.h>
Line 897 ⟶ 1,141:
printf("\n");
return 0;
}</
{{out}}
Line 908 ⟶ 1,152:
=={{header|C++}}==
<
// https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year
Line 937 ⟶ 1,181:
std::cout << '\n';
return 0;
}</
{{out}}
Line 951 ⟶ 1,195:
=={{header|Clojure}}==
<
(-> (java.time.LocalDate/of year 12 28)
(.get (.weekOfYear (java.time.temporal.WeekFields/ISO)))
(= 53)))
(filter long-year? (range 2000 2100))</
{{out}}
<pre>
Line 963 ⟶ 1,207:
=={{header|CLU}}==
<
% we can hide the helper `p' in a cluster
Line 986 ⟶ 1,230:
end
end
end start_up</
{{out}}
<pre>2004
Line 1,009 ⟶ 1,253:
=={{header|Common Lisp}}==
<
(mod (+ year (floor year 4) (- (floor year 100)) (floor year 400)) 7))
Line 1,016 ⟶ 1,260:
(format t "Long years between 1800 and 2100:~&~a~%"
(loop for y from 1800 to 2100 if (iso-long-year-p y) collect y))</
{{Out}}
Line 1,024 ⟶ 1,268:
1970 1976 1981 1987 1992 1998 2004 2009 2015 2020 2026 2032 2037 2043 2048
2054 2060 2065 2071 2076 2082 2088 2093 2099)</pre>
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
sub longyear(year: uint16): (r: uint8) is
sub p(y: uint16): (d: uint8) is
d := ((y + y/4 - y/100 + y/400) % 7) as uint8;
end sub;
r := 0;
if p(year) == 4 or p(year-1) == 3 then
r := 1;
end if;
end sub;
var year: uint16 := 2000;
while year <= 2100 loop
if longyear(year) != 0 then
print_i16(year);
print_nl();
end if;
year := year + 1;
end loop;</syntaxhighlight>
{{out}}
<pre>2004
2009
2015
2020
2026
2032
2037
2043
2048
2054
2060
2065
2071
2076
2082
2088
2093
2099</pre>
=={{header|Dc}}==
{{trans|Tcl}}
{{works with|GNU dc|1.3.95}}
<
[1q]s1
[1r- r 1r- * 1r-]sO # O = logical OR
Line 1,082 ⟶ 1,368:
[Long years between 1800 and 2100:]P AP
1800 2100 lDx
AP</
{{out}}
Line 1,098 ⟶ 1,384:
Note: The Library '''System.DateUtils''' implement a '''WeeksInYear''',but not working, return 52 always.
{{Trans|C++}}
<syntaxhighlight lang="delphi">
program Long_year;
Line 1,141 ⟶ 1,427:
Readln;
end.
</syntaxhighlight>
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc p(word y) word:
(y + y/4 - y/100 + y/400) % 7
corp
proc longyear(word y) bool:
p(y) = 4 or p(y-1) = 3
corp
proc main() void:
word y;
for y from 2000 upto 2100 do
if longyear(y) then writeln(y) fi
od
corp</syntaxhighlight>
{{out}}
<pre>2004
2009
2015
2020
2026
2032
2037
2043
2048
2054
2060
2065
2071
2076
2082
2088
2093
2099</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
func p y .
return (y + y div 4 - y div 100 + y div 400) mod 7
.
func longyear y .
return if p y = 4 or p (y - 1) = 3
.
for y = 2000 to 2100
if longyear y = 1
write y & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099
</pre>
=={{header|Elixir}}==
<
def long_year?(y) do
{:ok, jan1} = Date.new(y,1,1)
Line 1,152 ⟶ 1,493:
end
IO.inspect(Enum.filter(1990..2050, &ISO.long_year?/1))</
{{Out}}
Line 1,159 ⟶ 1,500:
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
<
: long-year? ( n -- ? ) 12 28 <date> week-number 53 = ;
"Year Long?\n-----------" print 1990 2021 [a,b]
[ dup long-year? "yes" "no" ? "%d %s\n" printf ] each</
{{out}}
<pre style="height:45ex">
Line 1,204 ⟶ 1,545:
=={{header|Forth}}==
<
: long? ( year -- flag ) dup dec31wd 4 = if drop 1 else 1 - dec31wd 3 = if 1 else 0 then then ;
: demo ( startyear endyear -- ) cr swap do i long? if i . then loop cr ;</
{{Out}}
Line 1,214 ⟶ 1,555:
=={{header|Fortran}}==
<
program longyear
use iso_fortran_env, only: output_unit, input_unit
Line 1,252 ⟶ 1,593:
end function is_long_year
end program longyear
</syntaxhighlight>
{{out}}
Line 1,269 ⟶ 1,610:
=={{header|Go}}==
<
import (
Line 1,290 ⟶ 1,631:
fmt.Println(longYears)
}
}</
{{out}}
Line 1,305 ⟶ 1,646:
=={{header|Haskell}}==
<
import Data.Time.Calendar.WeekDate (toWeekDate)
Line 1,314 ⟶ 1,655:
main :: IO ()
main = mapM_ print $ filter longYear [2000 .. 2100]</
{{Out}}
<pre>2004
Line 1,337 ⟶ 1,678:
=={{header|J}}==
{{trans|C}}
<
ily =: (4=p) +. 3=p@:<:
ply =: (#~ ily)@:([ + 1+i.@:-~)</
{{out}}
<pre>
Line 1,347 ⟶ 1,688:
=={{header|Java}}==
<
import java.time.LocalDate;
import java.time.temporal.WeekFields;
Line 1,367 ⟶ 1,708:
}
</syntaxhighlight>
{{out}}
Line 1,376 ⟶ 1,717:
=={{header|JavaScript}}==
{{trans|TypeScript}}
<
const jan1 = new Date(year, 0, 1);
const dec31 = new Date(year, 11, 31);
Line 1,386 ⟶ 1,727:
console.log(y)
}
}</
{{Out}}
Line 1,404 ⟶ 1,745:
===Using Zeller's congruence===
<syntaxhighlight lang="jq">
# Use Zeller's Congruence to determine the day of the week, given
# year, month and day as integers in the conventional way.
Line 1,429 ⟶ 1,770:
"Long years from 1900 to 2100 inclusive:",
([range(1900;2101) | select(has53weeks)] | nwise(10) | join(", "))</
{{out}}
<pre>
Line 1,440 ⟶ 1,781:
===Using mktime and gmtime===
<
# with 0 for Sunday, 1 for Monday, etc
# $year $month $day are conventional
Line 1,455 ⟶ 1,796:
"Long years from 1900 to 2100 inclusive:",
([range(1900;2101) | select(has53weeks)] | nwise(10) | join(", "))</
{{out}}
As above.
=={{header|Julia}}==
<
has53weeks(year) = week(Date(year, 12, 28)) == 53
Line 1,468 ⟶ 1,809:
println(year, " ", has53weeks(year) ? "Yes" : "No")
end
</
<pre>
Year 53 weeks?
Line 1,507 ⟶ 1,848:
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">
fun main() {
val has53Weeks = { year: Int -> LocalDate.of(year, 12, 28).get(WeekFields.ISO.weekOfYear()) == 53 }
Line 1,514 ⟶ 1,855:
.forEach { year -> print("$year ")}
}
</syntaxhighlight>
{{out}}
<pre style="height35ex">
Line 1,525 ⟶ 1,866:
=={{header|Logo}}==
{{works with|UCB Logo}}
<
output int quotient :x :y
end
Line 1,537 ⟶ 1,878:
end
for [y 1995 2045 1] [if iso_long_year? :y [print :y]]</
{{Out}}
Line 1,549 ⟶ 1,890:
2037
2043</pre>
=={{header|Lua}}==
<syntaxhighlight lang="lua">function isLongYear (y)
local function p (y)
local f = math.floor
return (y + f(y/4) - f(y/100) + f(y/400)) % 7
end
return p(y) == 4 or p(y - 1) == 3
end
print("Long years in the 21st century:")
for year = 2001, 2100 do
if isLongYear(year) then io.write(year .. " ") end
end</syntaxhighlight>
{{out}}
<pre>Long years in the 21st century:
2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
lastyear = 2099;
years = Range[firstyear, lastyear];
Line 1,558 ⟶ 1,917:
If[firstday[[n]] == Thursday || lastday[[n]] == Thursday,
Style[years[[n]] " long year \n", Bold, Red] ,
years[[n]] " short \n"], "error \n"], {n, Length[years]}]</
{{out}}
<pre style="height:35ex">{2000 short
Line 1,663 ⟶ 2,022:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 1,684 ⟶ 2,043:
END;
END;
END LongYear.</
{{out}}
<pre>2004
Line 1,706 ⟶ 2,065:
=={{header|Nim}}==
<
proc has53weeks(year: Positive): bool =
Line 1,716 ⟶ 2,075:
for year in 2000..2100:
if year.has53weeks:
echo year</
{{out}}
Line 1,740 ⟶ 2,099:
=={{header|МК-61/52}}==
<
/ [x] - ИП0 4 ВП 2 / [x] +
^ ^ 7 / [x] 7 * - П1 4
- x#0 40 ИП1 3 - x#0 40 0 С/П
1 С/П</
{{out}}
<pre>
Line 1,750 ⟶ 2,109:
</pre>
=={{header|Pascal}}==
<
var
y: integer;
Line 1,784 ⟶ 2,143:
if long_year(y) then
writeln(y)
end.</
{{Out}}
<pre>1993
Line 1,799 ⟶ 2,158:
==={{header|Free Pascal}}===
Using DateUtils and WeeksInYear to not reinvent this.
<
program Long_year;
Line 1,832 ⟶ 2,191:
Readln;
{$ENDIF}
end.</
{{out}}
<pre>
Line 1,845 ⟶ 2,204:
=={{header|Perl}}==
<
use warnings;
use DateTime;
Line 1,854 ⟶ 2,213:
}
print "\n";
}</
{{out}}
<pre>1903 1908 1914 1920 1925 1931 1936 1942 1948 1953 1959 1964 1970 1976 1981 1987 1992 1998
Line 1,861 ⟶ 2,220:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">week_number</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
Line 1,880 ⟶ 2,239:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Long years in the %d%s century:%v\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span><span style="color: #000000;">long_years</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,889 ⟶ 2,248:
=={{header|PHP}}==
<
return (53 == strftime('%V', gmmktime(0,0,0,12,28,$year)));
}
Line 1,897 ⟶ 2,256:
printf("%s\n", $y);
}
}</
{{Out}}
<pre>1998
Line 1,908 ⟶ 2,267:
2037
2043</pre>
=={{header|PL/0}}==
{{trans|Commodore BASIC}}
The program waits for two numbers: <code>startyear</code> and <code>endyear</code>. Then displays long years from <code>startyear</code> to <code>endyear</code>.
<syntaxhighlight lang="pascal">
var startyear, endyear, year, longyear, ayear, weekday;
procedure calcweekday;
begin
weekday := ayear + ayear / 4 - ayear / 100 + ayear / 400;
weekday := weekday - (weekday / 7) * 7
end;
procedure calclongyear;
begin
longyear := 0; ayear := year;
call calcweekday;
if weekday = 4 then longyear := 1;
ayear := year - 1;
call calcweekday;
if weekday = 3 then longyear := 1
end;
begin
? startyear;
? endyear;
year := startyear;
while year <= endyear do
begin
call calclongyear;
if longyear <> 0 then ! year;
year := year + 1
end
end.
</syntaxhighlight>
{{in}}
<pre>1995
2045</pre>
{{out}}
<pre>
1998
2004
2009
2015
2020
2026
2032
2037
2043
</pre>
=={{header|PowerShell}}==
<
param([Int]$year)
53 -eq (Get-Date -Year $year -Month 12 -Day 28 -UFormat %V)
Line 1,919 ⟶ 2,328:
Write-Host $y
}
}</
{{Out}}
Line 1,935 ⟶ 2,344:
{{trans|C++}}
{{works with|SWI Prolog}}
<
p(Year, P):-
Line 1,972 ⟶ 2,381:
main:-
print_long_years(1800, 2100).</
{{out}}
Line 1,987 ⟶ 2,396:
=={{header|Python}}==
{{Works with|Python|3.7}}
<
from datetime import date
Line 2,011 ⟶ 2,420:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>2004
Line 2,036 ⟶ 2,445:
<code>dayofweek</code> is defined at [[Day of the week#Quackery]]
<
[ 1 1 rot dayofweek 4 = ]
31 12 rot dayofweek 4 = or ] is longyear ( n --> b )
Line 2,044 ⟶ 2,453:
100 times
[ 2000 i^ + longyear if
[ 2000 i^ + echo sp ] ]</
{{out}}
Line 2,057 ⟶ 2,466:
{{works with|Rakudo|2019.11}}
December 28 is always in the last week of the year. (By ISO8601)
<syntaxhighlight lang="raku"
# Testing
say "Long years in the 20th century:\n", (1900..^2000).grep: &is-long;
say "\nLong years in the 21st century:\n", (2000..^2100).grep: &is-long;
say "\nLong years in the 22nd century:\n", (2100..^2200).grep: &is-long;</
{{out}}
<pre>Long years in the 20th century:
Line 2,074 ⟶ 2,483:
=={{header|REXX}}==
<
Parse Arg lo hi . /* obtain optional args. */
current=left(date('S'),4)
If lo=='' | lo=="," | lo=='*' Then lo=current /*Not given? Use default.*/
If hi=='' | hi=="," Then hi=lo /* " " " " */
If hi=='*' Then hi=current /*an asterisk: current yr*/
Do
right(word('short long',is_long(yr)+1),5) ' year'
End
Exit
/*----------------------------------------------------------------------*/
wd_1231:
/*************************************************************************
* returns the day of the week of 31 December year
*************************************************************************/
Parse Arg year
Return (year+year%4-year%100+year%400)//7
is_long:
Parse Arg year
Return wd_1231(year)==4 |, /* year ends in a Thursday */
wd_1231(year-1)==3 /* or previous year ends in a Wednesday */</syntaxhighlight>
{{out|output|text= when using the inputs of: <tt> 1990 2030 </tt>}}
Line 2,135 ⟶ 2,556:
=={{header|Ring}}==
<
see "long years 2000-2099: "
for year = 2000 to 2100
Line 2,145 ⟶ 2,566:
ok
next
</syntaxhighlight>
{{out}}
<pre>
long years 2000-2099: 2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099
</pre>
=={{header|RPL}}==
This is a direct transcription from [https://en.wikipedia.org/wiki/ISO%20week%20date Wikipedia's formula].
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪
DUP 1 -
1 2 '''START'''
→ y ≪ y DUP 4 / IP + y 100 / IP - y 400 / IP + 7 MOD ≫
SWAP
'''NEXT'''
3 == SWAP 4 == OR
≫ 'LONG?' STO
|
( year -- weekday )
Calling 2 times...
... p() as a nested function, to get p(y-1)...
...then p(y)
|}
The following line of code delivers what is required:
≪ {} 2023 2100 '''FOR''' y '''IF''' y LONG? '''THEN''' y + '''END NEXT''' ≫ EVAL
{{out}}
<pre>
1: { 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099 }
</pre>
=={{header|Ruby}}==
<
require 'date'
Line 2,160 ⟶ 2,614:
(2020..2030).each{|year| puts "#{year} is long? #{ long_year?(year) }." }
</syntaxhighlight>
{{out}}
<pre>2020 is long? true.
Line 2,177 ⟶ 2,631:
=={{header|Rust}}==
<
use time::Date;
Line 2,190 ⟶ 2,644:
Date::try_from_ymd(year, 12, 28).map_or(false, |date| date.week() == 53)
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,216 ⟶ 2,670:
{{Out}}Best seen running in your browser by [https://scastie.scala-lang.org/LDKMJ29SSrehyVbCmiMH1Q Scastie (remote JVM)].
<
import java.time.temporal.{ChronoField, IsoFields}
import java.time.{DayOfWeek, LocalDate, Month}
Line 2,262 ⟶ 2,716:
results.zipWithIndex.foreach(solution => println(s"Solution ${solution._2}: ${solution._1.mkString(" ")}"))
}</
=={{header|Scheme}}==
Line 2,271 ⟶ 2,725:
The demo code uses <tt>iota</tt> as defined in SRFI-1, so won't work in Schemes lacking that function. Racket requires the addition of <tt>(require srfi/1)</tt>.
<
(remainder (apply + (map (lambda (d) (quotient year d)) '(1 4 -100 400))) 7))
Line 2,277 ⟶ 2,731:
(display "Long years between 1800 and 2100:") (newline)
(display (filter long? (iota 300 1800)))</
{{Out}}
Line 2,284 ⟶ 2,738:
=={{header|Sidef}}==
<
Date.parse("#{year}-12-28", "%Y-%m-%d").week == 53
}
Line 2,290 ⟶ 2,744:
say ( "Long years in the 20th century:\n", (1900..^2000).grep(is_long_year))
say ("\nLong years in the 21st century:\n", (2000..^2100).grep(is_long_year))
say ("\nLong years in the 22nd century:\n", (2100..^2200).grep(is_long_year))</
{{out}}
<pre>
Line 2,304 ⟶ 2,758:
=={{header|Snobol}}==
<
DEC31WD DEC31WD = REMDR(Year + (Year / 4) - (Year / 100) + (Year / 400), 7) :(RETURN)
END_DEC31WD
Line 2,322 ⟶ 2,776:
ISODEMO(1995, 2045)
END</
{{Out}}
Line 2,337 ⟶ 2,791:
=={{header|Swift}}==
<
let year1 = year - 1
let p = (year + (year / 4) - (year / 100) + (year / 400)) % 7
Line 2,347 ⟶ 2,801:
for range in [1900...1999, 2000...2099, 2100...2199] {
print("\(range): \(range.filter(isLongYear))")
}</
{{out}}
Line 2,358 ⟶ 2,812:
{{trans|C++}}
<syntaxhighlight lang="tcl">
## Reference: https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year
Line 2,383 ⟶ 2,837:
puts "Long years between 1800 and 2100:"
print_long_years 1800 2100
puts ""</
{{out}}
<pre>
Line 2,399 ⟶ 2,853:
Contents of main module:
<
source = "./iso-long-years"
start_year = 1995
Line 2,407 ⟶ 2,861:
output "long-years" {
value = module.iso-long-years.long-years
}</
Contents of <tt>iso-long-years</tt> module:
<
type = number
}
Line 2,432 ⟶ 2,886:
value = compact([for y in [for n in local.year_list: tostring(n)]:
module.iso-long-year[y].isLong ? y : ""])
}</
Contents of <tt>iso-long-year</tt> module:
<
type = string
default = ""
Line 2,451 ⟶ 2,905:
output isLong {
value = (local.dec31 % 7 == 4 || local.jan1 % 7 == 4)
}</
{{Out}}
Line 2,471 ⟶ 2,925:
]</pre>
=={{header|TypeScript}}==
<
const jan1: Date = new Date(year, 0, 1);
const dec31: Date = new Date(year, 11, 31);
Line 2,481 ⟶ 2,935:
console.log(y)
}
}</
{{Out}}
Line 2,497 ⟶ 2,951:
=== Thursdays check using cal(1) and grep(1) ===
<
cal 1 $1 | grep -q ' 3 *$' && return 0
cal 12 $1 | grep -q ' 26 *$'
}</
=== Straightforward check using GNU date(1) ===
December 28th is always in the last week of the year, so just check whether or not that's week 53:
<syntaxhighlight lang="sh">long_year() {
expr $(date -d "$1-12-28" +%V) = 53 >/dev/null
}</
=== Direct computation with built-in arithmetic in newer shells ===
Line 2,511 ⟶ 2,967:
{{works with|Korn Shell}}
{{works with|Zsh}}
<
# return weekday (time_t tm_wday, 0=Sunday) of December 31st of the given year
typeset -i y=$1
echo $(( (y + y / 4 - y / 100 + y / 400) % 7 ))
}
# the year is long if the year starts or ends on a Thursday (starts on a
# Thursday = the previous year ends on a Wednesday)
long_year() {
typeset -i y=$1
(( 4 == $(dec31wd $y) || 3 == $(dec31wd $(( y - 1 ))) ))
}</
Demo code for any of the above:
<
if long_year $y; then
echo $y
fi
done | column
</syntaxhighlight>
{{Out}}
<pre>1998 2004 2009 2015 2020 2026 2032 2037 2043</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-date}}
<
var centuries = ["20th", "21st", "22nd"]
Line 2,615 ⟶ 3,009:
}
System.print(longYears)
}</
{{out}}
Line 2,632 ⟶ 3,026:
{{trans|Commodore BASIC}}
{{works with|EXPL-32}}
<
\Long year
code Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12, Clear=40;
Line 2,657 ⟶ 3,051:
CrLf(0);
end
</syntaxhighlight>
{{out}}
<pre>
Line 2,668 ⟶ 3,062:
=={{header|zkl}}==
<
foreach nm,y in (T(T("20th",1900), T("21st",2000), T("22nd",2100))){
println("\nLong years in the %s century:\n%s".fmt(nm,
[y..y+99].filter(isLongYear).concat(" ")));
}</
{{out}}
<pre>
|