HP Forums

Full Version: Easter Algorithm Bug
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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;
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;
Reference URL's