Skip to content

Add Computus in Javascript and Typescript #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Delphi1024
- ntindle
- Mahdi Sarikhani
- Ridham177
- Ridham177
- Hugo Salou
8 changes: 6 additions & 2 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@
{
"lang": "ps1",
"name": "PowerShell"
},
{
},
{
"lang": "v",
"name": "Vlang"
},
Expand All @@ -217,6 +217,10 @@
{
"lang": "coco",
"name": "Coconut"
},
{
"lang": "dart",
"name": "Dart"
}
]
}
Expand Down
63 changes: 63 additions & 0 deletions contents/computus/code/dart/gauss_easter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
String computus(int year, {bool servois = false}) {
// Year's position in metonic cycle
final a = year % 19;

// Century index
final k = (year / 100).floor();

// Shift of metonic cycle, add a day offset every 300 years
final p = ((13 + 8 * k) / 25).floor();

// Correction for non-observed leap days
final q = (k / 4).floor();

// Correction to starting point of calculation each century
final M = (15 - p + k - q) % 30;

// Number of days from March 21st until the full moon
final d = (19 * a + M) % 30;

// Returning if user wants value for Servois' table
if (servois) {
return ((21 + d) % 31).toString();
}

// Finding the next Sunday
// Century-based offset in weekly calculation
final N = (4 + k - q) % 7;

// Correction for leap days
final b = year % 4;
final c = year % 7;

// Days from d to next Sunday
var e = (2 * b + 4 * c + 6 * d + N) % 7;

// Historical corrections for April 26 and 25
if (e == 6) {
if (d == 29 || (d == 28 && a > 10)) {
e = -1;
}
}

// Determination of the correct month for Easter
if (22 + d + e > 31) {
return 'April ${d + e - 9}';
} else {
return 'March ${22 + d + e}';
}
}

void main() {
print("The following are the dates of the Paschal full moon (using Servois " +
"notation) and the date of Easter for 2020-2030 AD:");

print("Year\tServois number\tEaster");

for (var year = 2020; year <= 2030; year++) {
final servoisNumber = computus(year, servois: true);
final easterDate = computus(year);

print('$year\t$servoisNumber\t\t$easterDate');
}
}
91 changes: 91 additions & 0 deletions contents/computus/code/javascript/gauss_easter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* In this code, the modulus operator is used.
* However, this operator in javascript/typescript doesn't support negative numbers.
* So, where there may be negative numbers, the function mod is used.
* This function gives the modulo of any relative number a
*/

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
function mod(a, b) {
if (a < 0) return mod(a + b, b);
else return a % b;
}

/**
* @param {number} year
* @param {boolean} [servois=false]
* @returns {string}
*/
function computus(year, servois = false) {
// Year's position in metonic cycle
const a = year % 19;

// Century index
const k = Math.floor(year / 100);

// Shift of metonic cycle, add a day offset every 300 years
const p = Math.floor((13 + 8 * k) / 25);

// Correction for non-observed leap days
const q = Math.floor(k / 4);

// Correction to starting point of calculation each century
const M = mod(15 - p + k - q, 30);

// Number of days from March 21st until the full moon
const d = (19 * a + M) % 30;

// Returning if user wants value for Servois' table
if (servois) {
return ((21 + d) % 31).toString();
}

// Finding the next Sunday
// Century-based offset in weekly calculation
const N = mod(4 + k - q, 7);

// Correction for leap days
const b = year % 4;
const c = year % 7;

// Days from d to next Sunday
let e = (2 * b + 4 * c + 6 * d + N) % 7;

// Historical corrections for April 26 and 25
if (e === 6) {
if (d === 29 || (d === 28 && a > 10)) {
e = -1;
}
}

// Determination of the correct month for Easter
if (22 + d + e > 31) {
return `April ${d + e - 9}`;
} else {
return `March ${22 + d + e}`;
}
}

console.log(
"The following are the dates of the Paschal full moon (using Servois " +
"notation) and the date of Easter for 2020-2030 AD:"
);

const values = [];

for (let year = 2020; year <= 2030; year++) {
const servoisNumber = computus(year, true);
const easterDate = computus(year);

// Creation of an object to be displayed as a line in the output table
values[year] = {
"servois number": +servoisNumber,
easter: easterDate,
};
}

console.table(values);
91 changes: 91 additions & 0 deletions contents/computus/code/typescript/gauss_easter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* In this code, the modulus operator is used.
* However, this operator in javascript/typescript doesn't support negative numbers.
* So, where there may be negative numbers, the function mod is used.
* This function gives the modulo of any relative number a
*/

function mod(a: number, b: number): number {
if (a < 0) {
return mod(a + b, b);
} else {
return a % b;
}
}
function computus(year: number, servois: boolean = false): string {
// Year's position in metonic cycle
const a: number = year % 19;

// Century index
const k: number = Math.floor(year / 100);

// Shift of metonic cycle, add a day offset every 300 years
const p: number = Math.floor((13 + 8 * k) / 25);

// Correction for non-observed leap days
const q: number = Math.floor(k / 4);

// Correction to starting point of calculation each century
const M: number = mod(15 - p + k - q, 30);

// Number of days from March 21st until the full moon
const d: number = (19 * a + M) % 30;

// Returning if user wants value for Servois' table
if (servois) {
return ((21 + d) % 31).toString();
}

// Finding the next Sunday
// Century-based offset in weekly calculation
const N: number = mod(4 + k - q, 7);

// Correction for leap days
const b: number = year % 4;
const c: number = year % 7;

// Days from d to next Sunday
let e: number = (2 * b + 4 * c + 6 * d + N) % 7;

// Historical corrections for April 26 and 25
if (e === 6) {
if (d === 29 || (d === 28 && a > 10)) {
e = -1;
}
}

// Determination of the correct month for Easter
if (22 + d + e > 31) {
return `April ${d + e - 9}`;
} else {
return `March ${22 + d + e}`;
}
}

console.log(
"The following are the dates of the Paschal full moon (using Servois " +
"notation) and the date of Easter for 2020-2030 AD:"
);

// Type of a line in the output table
interface IOutputLine {
"servois number": number;
easter: string;
}

const values: IOutputLine[] = [];

for (let year = 2020; year <= 2030; year++) {
const servoisNumber: string = computus(year, true);
const easterDate: string = computus(year);

// Creation of an object to be displayed as a line in the output table
const line: IOutputLine = {
"servois number": +servoisNumber,
easter: easterDate,
};

values[year] = line;
}

console.table(values);
6 changes: 6 additions & 0 deletions contents/computus/computus.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ For now, we have the code outputting a tuple of $$d$$ and $$e$$, so users can us
[import, lang:"nim"](code/nim/gauss_easter.nim)
{% sample lang="scala" %}
[import, lang:"scala"](code/scala/gauss_easter.scala)
{% sample lang="dart" %}
[import, lang:"dart"](code/dart/gauss_easter.dart)
{% sample lang="javascript" %}
[import, lang:"javascript"](code/javascript/gauss_easter.js)
{% sample lang="typescript" %}
[import, lang:"typescript"](code/typescript/gauss_easter.ts)
{% endmethod %}


Expand Down