How to resolve the algorithm Find the last Sunday of each month step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the last Sunday of each month step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Write a program or a script that returns the last Sundays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc). Example of an expected output:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the last Sunday of each month step by step in the PARI/GP programming language

Source code in the pari/gp programming language

\\ Normalized Julian Day Number from date
njd(D) =
{
  my (m = D[2], y = D[1]);
  if (D[2] > 2, m++, y--; m += 13);
  (1461 * y) \ 4 + (306001 * m) \ 10000 + D[3] - 694024 + 2 - y \ 100 + y \ 400
}

\\ Date from Normalized Julian Day Number
njdate(J) =
{
  my (a = J + 2415019, b = (4 * a - 7468865) \ 146097, c, d, m, y);

  a += 1 + b - b \ 4 + 1524;
  b = (20 * a - 2442) \ 7305;
  c = (1461 * b) \ 4;
  d = ((a - c) * 10000) \ 306001;
  m = d - 1 - 12 * (d > 13);
  y = b - 4715 - (m > 2);
  d = a - c - (306001 * d) \ 10000;

  [y, m, d]
}

for (m=1, 12, a=njd([2013,m+1,0]); print(njdate(a-(a+6)%7)))

  

You may also check:How to resolve the algorithm Disarium numbers step by step in the bc programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Objeck programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Phix programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the Scala programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the uBasic/4tH programming language