How to resolve the algorithm Long year step by step in the Pascal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Long year step by step in the Pascal programming language

Table of Contents

Problem Statement

Most years have 52 weeks, some have 53, according to ISO8601.

Write a function which determines if a given year is long (53 weeks) or not, and demonstrate it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long year step by step in the Pascal programming language

Source code in the pascal programming language

program long_year(input);
  var
    y: integer;

  function rd_dec31(year: integer): integer;
  begin
    { Rata Die of Dec 31, year }
    rd_dec31 := year * 365 + year div 4 - year div 100 + year div 400
  end;

  function rd_jan1(year: integer): integer;
  begin
    rd_jan1 := rd_dec31(year - 1) + 1
  end;

  function weekday(rd: integer): integer;
  begin
    weekday := rd mod 7;
  end;

  function long_year(year: integer): boolean;
  var
    jan1: integer;
    dec31: integer;
  begin
    jan1 := rd_jan1(year);
    dec31 := rd_dec31(year);
    long_year := (weekday(jan1) = 4) or (weekday(dec31) = 4)
  end;

  begin
    for y := 1990 to 2050 do
      if long_year(y) then
        writeln(y)
  end.


program Long_year;

uses
  SysUtils,
  DateUtils;

  procedure PrintLongYears(StartYear, EndYear: Uint32);
  var
    Year, Count: Uint32;
    DateSep: char;
  begin
    DateSep := FormatSettings.DateSeparator;
    Writeln('Long years between ', StartYear, ' and ', EndYear);
    Count := 0;
    for Year := StartYear to EndYear do
      if WeeksInYear(StrToDate('01' + DateSep + '01' + DateSep + IntToStr(Year))) = 53 then
      begin
        if Count mod 10 = 0 then
          Writeln;
        Write(Year, ' ');
        Inc(Count);
      end;
    if Count mod 10 <> 0 then
      Writeln;
    writeln('Found ', Count, ' long years between ', StartYear, ' and ', EndYear);
  end;

begin
  PrintLongYears(1800, 2100);
  {$IFDEF WINDOWS}
  Readln;
  {$ENDIF}
end.


  

You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the Perl programming language
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the Tcl programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the C++ programming language