June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,11 @@
10 ' Leap year
20 DEF FN ISLEAPYEAR(Y%) = ((Y% MOD 4 = 0) AND (Y% MOD 100 <> 0)) OR (Y% MOD 400 = 0)
95 ' *** Test ***
100 FOR I% = 1 TO 5
110 READ YEAR%
120 PRINT YEAR%; "is ";
130 IF FN ISLEAPYEAR(YEAR%) = 0 THEN PRINT "not "; ELSE PRINT "";
140 PRINT "a leap year."
150 NEXT I%
160 END
200 DATA 1900, 1994, 1996, 1997, 2000

View file

@ -0,0 +1,8 @@
import java.time.Year;
public class IsLeap {
public static void main(String[] args) {
System.out.println(Year.isLeap(2004));
}
}

View file

@ -1,5 +1,4 @@
leap(y) = y%4==0 && (y<1582 || y%400==0 || y%100!=0)
isleap(yr::Integer) = yr % 4 == 0 && (yr < 1582 || yr % 400 == 0 || yr % 100 != 0)
# some tests
@assert all([leap(yr) for yr in [2400, 2012, 2000, 1600, 1500, 1400]])
@assert all([~leap(yr) for yr in [2100, 2014, 1900, 1800, 1700, 1499]])
@assert all(isleap, [2400, 2012, 2000, 1600, 1500, 1400])
@assert !any(isleap, [2100, 2014, 1900, 1800, 1700, 1499])

View file

@ -0,0 +1 @@
x = ~mod(YEAR, 4) & (mod(YEAR, 100) | ~mod(YEAR, 400))

View file

@ -0,0 +1,30 @@
MODULE LeapYear;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
PROCEDURE IsLeapYear(year : INTEGER) : BOOLEAN;
BEGIN
IF year MOD 100 = 0 THEN
RETURN year MOD 400 = 0;
END;
RETURN year MOD 4 = 0
END IsLeapYear;
PROCEDURE Print(year : INTEGER);
VAR
buf : ARRAY[0..63] OF CHAR;
leap : BOOLEAN;
BEGIN
leap := IsLeapYear(year);
FormatString("Is %i a leap year? %b\n", buf, year, leap);
WriteString(buf)
END Print;
BEGIN
Print(1900);
Print(1994);
Print(1996);
Print(1997);
Print(2000);
ReadChar
END LeapYear.

View file

@ -3,5 +3,5 @@ isLeapYear <- function(year) {
}
for (y in c(1900, 1994, 1996, 1997, 2000)) {
print(paste(y, " is ", ifelse(isLeapYear(y), "", "not "), "a leap year.", sep=""))
cat(y, ifelse(isLeapYear(y), "is", "isn't"), "a leap year.\n")
}

View file

@ -1,5 +1,4 @@
fn is_leap(year: i32) -> bool {
let factor = |x| year % x == 0;
factor(4) && (!factor(100) || factor(400))
}
}

View file

@ -0,0 +1,9 @@
Public Function IsLeapYear1(ByVal theYear As Integer) As Boolean
'this function utilizes documented behaviour of the built-in DateSerial function
IsLeapYear1 = (VBA.Day(VBA.DateSerial(theYear, 2, 29)) = 29)
End Function
Public Function IsLeapYear2(ByVal theYear As Integer) As Boolean
'this function uses the well-known formula
IsLeapYear2 = IIf(theYear Mod 100 = 0, theYear Mod 400 = 0, theYear Mod 4 = 0)
End Function

View file

@ -0,0 +1,7 @@
Sub Main()
'testing the above functions
Dim i As Integer
For i = 1750 To 2150
Debug.Assert IsLeapYear1(i) Eqv IsLeapYear2(i)
Next i
End Sub