HP Forums
Easter Algorithm Bug - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Easter Algorithm Bug (/thread-8336.html)



Easter Algorithm Bug - codycbradio - 05-11-2017 06:53 PM

The following code is supposed to output the a date of Easter for a given year. For some reason it doesn't output the right date and doesn't add the space after the month. If anyone can figure out what the bug in my code is, I would be greatly appreciated.

Code:

EXPORT easter(T)
BEGIN
  // Year (1583-9999)
  LOCAL A,B,C,D,E,K,M,N;
  LOCAL P,Q;
  IF T<1583 OR T>9999 THEN
    RETURN "Error - Year is invalid.";
  ELSE
    K:=IP(T/100);
    A:=T MOD 19;
    B:=T MOD 4;
    C:=T MOD 7;
    Q:=IP(K/4);
    P:=IP((8*K+13)/25);
    M:=15-P+K-Q MOD 30;
    D:=19*A+M MOD 30;
    N:=4+K-Q MOD 7;
    E:=2*B+4*C+6*D+N MOD 7;
    IF D+E<=9 THEN
      D:=22+D+E;
      M:=3;
    ELSE
      IF D==19 AND E==6 THEN
        D:=19;
        M:=4;
      ELSE
        IF D==28 AND E==6 AND A>10 THEN
          D:=18;
          M:=4;
        ELSE
          D:=D+E-9;
          M:=4;
        END;
      END;
    END;
    IF M==3 THEN
      RETURN cat("March ",D);
    ELSE
      RETURN cat("April ",D);
    END;
  END;
END;



RE: Easter Algorithm Bug - Didier Lachieze - 05-11-2017 08:33 PM

Hello, with MOD you need parentheses.
This version of your code seems to work well :
Code:
EXPORT easter(T)
BEGIN
  // Year (1583-9999)
  LOCAL A,B,C,D,E,K,M,N;
  LOCAL P,Q;
  IF T<1583 OR T>9999 THEN
    RETURN "Error - Year is invalid.";
  ELSE
    K:=IP(T/100);
    A:=T MOD 19;
    B:=T MOD 4;
    C:=T MOD 7;
    Q:=IP(K/4);
    P:=IP((8*K+13)/25);
    M:=(15-P+K-Q) MOD 30;
    D:=(19*A+M) MOD 30;
    N:=(4+K-Q) MOD 7;
    E:=(2*B+4*C+6*D+N) MOD 7;
    IF D+E<=9 THEN
      D:=22+D+E;
      M:=3;
    ELSE
      IF D==19 AND E==6 THEN
        D:=19;
        M:=4;
      ELSE
        IF D==28 AND E==6 AND A>10 THEN
          D:=18;
          M:=4;
        ELSE
          D:=D+E-9;
          M:=4;
        END;
      END;
    END;
    IF M==3 THEN
      RETURN "March "+D;
    ELSE
      RETURN "April "+D;
    END;
  END;
END;