Skip to content

Commit a913874

Browse files
authored
Add Computus in Javascript and Typescript (#807)
* Add Computus in Javascript and Typescript * Changed Computus in JavaScript, Typescript and added Computus in Dart * Linked computus.md to JavaScript, Typescript and Dart * Added Dart to book.json and updated contributors * Readded Sobel_filters.png
1 parent 139c4a1 commit a913874

File tree

6 files changed

+259
-3
lines changed

6 files changed

+259
-3
lines changed

CONTRIBUTORS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
5757
- Delphi1024
5858
- ntindle
5959
- Mahdi Sarikhani
60-
- Ridham177
60+
- Ridham177
61+
- Hugo Salou

book.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@
193193
{
194194
"lang": "ps1",
195195
"name": "PowerShell"
196-
},
197-
{
196+
},
197+
{
198198
"lang": "v",
199199
"name": "Vlang"
200200
},
@@ -217,6 +217,10 @@
217217
{
218218
"lang": "coco",
219219
"name": "Coconut"
220+
},
221+
{
222+
"lang": "dart",
223+
"name": "Dart"
220224
}
221225
]
222226
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
String computus(int year, {bool servois = false}) {
2+
// Year's position in metonic cycle
3+
final a = year % 19;
4+
5+
// Century index
6+
final k = (year / 100).floor();
7+
8+
// Shift of metonic cycle, add a day offset every 300 years
9+
final p = ((13 + 8 * k) / 25).floor();
10+
11+
// Correction for non-observed leap days
12+
final q = (k / 4).floor();
13+
14+
// Correction to starting point of calculation each century
15+
final M = (15 - p + k - q) % 30;
16+
17+
// Number of days from March 21st until the full moon
18+
final d = (19 * a + M) % 30;
19+
20+
// Returning if user wants value for Servois' table
21+
if (servois) {
22+
return ((21 + d) % 31).toString();
23+
}
24+
25+
// Finding the next Sunday
26+
// Century-based offset in weekly calculation
27+
final N = (4 + k - q) % 7;
28+
29+
// Correction for leap days
30+
final b = year % 4;
31+
final c = year % 7;
32+
33+
// Days from d to next Sunday
34+
var e = (2 * b + 4 * c + 6 * d + N) % 7;
35+
36+
// Historical corrections for April 26 and 25
37+
if (e == 6) {
38+
if (d == 29 || (d == 28 && a > 10)) {
39+
e = -1;
40+
}
41+
}
42+
43+
// Determination of the correct month for Easter
44+
if (22 + d + e > 31) {
45+
return 'April ${d + e - 9}';
46+
} else {
47+
return 'March ${22 + d + e}';
48+
}
49+
}
50+
51+
void main() {
52+
print("The following are the dates of the Paschal full moon (using Servois " +
53+
"notation) and the date of Easter for 2020-2030 AD:");
54+
55+
print("Year\tServois number\tEaster");
56+
57+
for (var year = 2020; year <= 2030; year++) {
58+
final servoisNumber = computus(year, servois: true);
59+
final easterDate = computus(year);
60+
61+
print('$year\t$servoisNumber\t\t$easterDate');
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* In this code, the modulus operator is used.
3+
* However, this operator in javascript/typescript doesn't support negative numbers.
4+
* So, where there may be negative numbers, the function mod is used.
5+
* This function gives the modulo of any relative number a
6+
*/
7+
8+
/**
9+
* @param {number} a
10+
* @param {number} b
11+
* @returns {number}
12+
*/
13+
function mod(a, b) {
14+
if (a < 0) return mod(a + b, b);
15+
else return a % b;
16+
}
17+
18+
/**
19+
* @param {number} year
20+
* @param {boolean} [servois=false]
21+
* @returns {string}
22+
*/
23+
function computus(year, servois = false) {
24+
// Year's position in metonic cycle
25+
const a = year % 19;
26+
27+
// Century index
28+
const k = Math.floor(year / 100);
29+
30+
// Shift of metonic cycle, add a day offset every 300 years
31+
const p = Math.floor((13 + 8 * k) / 25);
32+
33+
// Correction for non-observed leap days
34+
const q = Math.floor(k / 4);
35+
36+
// Correction to starting point of calculation each century
37+
const M = mod(15 - p + k - q, 30);
38+
39+
// Number of days from March 21st until the full moon
40+
const d = (19 * a + M) % 30;
41+
42+
// Returning if user wants value for Servois' table
43+
if (servois) {
44+
return ((21 + d) % 31).toString();
45+
}
46+
47+
// Finding the next Sunday
48+
// Century-based offset in weekly calculation
49+
const N = mod(4 + k - q, 7);
50+
51+
// Correction for leap days
52+
const b = year % 4;
53+
const c = year % 7;
54+
55+
// Days from d to next Sunday
56+
let e = (2 * b + 4 * c + 6 * d + N) % 7;
57+
58+
// Historical corrections for April 26 and 25
59+
if (e === 6) {
60+
if (d === 29 || (d === 28 && a > 10)) {
61+
e = -1;
62+
}
63+
}
64+
65+
// Determination of the correct month for Easter
66+
if (22 + d + e > 31) {
67+
return `April ${d + e - 9}`;
68+
} else {
69+
return `March ${22 + d + e}`;
70+
}
71+
}
72+
73+
console.log(
74+
"The following are the dates of the Paschal full moon (using Servois " +
75+
"notation) and the date of Easter for 2020-2030 AD:"
76+
);
77+
78+
const values = [];
79+
80+
for (let year = 2020; year <= 2030; year++) {
81+
const servoisNumber = computus(year, true);
82+
const easterDate = computus(year);
83+
84+
// Creation of an object to be displayed as a line in the output table
85+
values[year] = {
86+
"servois number": +servoisNumber,
87+
easter: easterDate,
88+
};
89+
}
90+
91+
console.table(values);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* In this code, the modulus operator is used.
3+
* However, this operator in javascript/typescript doesn't support negative numbers.
4+
* So, where there may be negative numbers, the function mod is used.
5+
* This function gives the modulo of any relative number a
6+
*/
7+
8+
function mod(a: number, b: number): number {
9+
if (a < 0) {
10+
return mod(a + b, b);
11+
} else {
12+
return a % b;
13+
}
14+
}
15+
function computus(year: number, servois: boolean = false): string {
16+
// Year's position in metonic cycle
17+
const a: number = year % 19;
18+
19+
// Century index
20+
const k: number = Math.floor(year / 100);
21+
22+
// Shift of metonic cycle, add a day offset every 300 years
23+
const p: number = Math.floor((13 + 8 * k) / 25);
24+
25+
// Correction for non-observed leap days
26+
const q: number = Math.floor(k / 4);
27+
28+
// Correction to starting point of calculation each century
29+
const M: number = mod(15 - p + k - q, 30);
30+
31+
// Number of days from March 21st until the full moon
32+
const d: number = (19 * a + M) % 30;
33+
34+
// Returning if user wants value for Servois' table
35+
if (servois) {
36+
return ((21 + d) % 31).toString();
37+
}
38+
39+
// Finding the next Sunday
40+
// Century-based offset in weekly calculation
41+
const N: number = mod(4 + k - q, 7);
42+
43+
// Correction for leap days
44+
const b: number = year % 4;
45+
const c: number = year % 7;
46+
47+
// Days from d to next Sunday
48+
let e: number = (2 * b + 4 * c + 6 * d + N) % 7;
49+
50+
// Historical corrections for April 26 and 25
51+
if (e === 6) {
52+
if (d === 29 || (d === 28 && a > 10)) {
53+
e = -1;
54+
}
55+
}
56+
57+
// Determination of the correct month for Easter
58+
if (22 + d + e > 31) {
59+
return `April ${d + e - 9}`;
60+
} else {
61+
return `March ${22 + d + e}`;
62+
}
63+
}
64+
65+
console.log(
66+
"The following are the dates of the Paschal full moon (using Servois " +
67+
"notation) and the date of Easter for 2020-2030 AD:"
68+
);
69+
70+
// Type of a line in the output table
71+
interface IOutputLine {
72+
"servois number": number;
73+
easter: string;
74+
}
75+
76+
const values: IOutputLine[] = [];
77+
78+
for (let year = 2020; year <= 2030; year++) {
79+
const servoisNumber: string = computus(year, true);
80+
const easterDate: string = computus(year);
81+
82+
// Creation of an object to be displayed as a line in the output table
83+
const line: IOutputLine = {
84+
"servois number": +servoisNumber,
85+
easter: easterDate,
86+
};
87+
88+
values[year] = line;
89+
}
90+
91+
console.table(values);

contents/computus/computus.md

+6
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us
322322
[import, lang:"nim"](code/nim/gauss_easter.nim)
323323
{% sample lang="scala" %}
324324
[import, lang:"scala"](code/scala/gauss_easter.scala)
325+
{% sample lang="dart" %}
326+
[import, lang:"dart"](code/dart/gauss_easter.dart)
327+
{% sample lang="javascript" %}
328+
[import, lang:"javascript"](code/javascript/gauss_easter.js)
329+
{% sample lang="typescript" %}
330+
[import, lang:"typescript"](code/typescript/gauss_easter.ts)
325331
{% endmethod %}
326332

327333

0 commit comments

Comments
 (0)