Post Reply 
Top three calculators ?
08-18-2018, 04:41 PM
Post: #83
RE: Top three calculators ?
(04-05-2018 10:16 AM)Pekis Wrote:  But I think no one can deny that the mind can't figure instantly what all these stack operations are doing in RPL (and RPN), and you'll end up documenting with pseudo-code tasting like ... BASIC:


Day of week:


RPL
<< ROT 2 -
IF DUP 0 < THEN
12 +
SWAP 1 - SWAP
END
3 ROLLD
100 / DUP FP 100 * SWAP IP
DUP 4 / IP SWAP 2 * - SWAP DUP 4 / IP
+ + + SWAP 2.6 * .2 - IP + 7 MOD
{ "Sunday"
"Monday"
"Tuesday"
"Wednesday"
"Thrusday"
"Friday"
"Saturday" }
SWAP 1 + GET
>>

This formula is from Zeller's congruence:

\(h=\left(q+\left\lfloor {\frac {13(m+1)}{5}}\right\rfloor +K+\left\lfloor {\frac {K}{4}}\right\rfloor +\left\lfloor {\frac {J}{4}}\right\rfloor -2J\right){\bmod {7}}\)

where
  • h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday)
  • q is the day of the month
  • m is the month (3 = March, 4 = April, 5 = May, ..., 14 = February)
  • K the year of the century (\( year \bmod 100 \)).
  • J is the zero-based century (\( \lfloor year/100\rfloor \))

NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year.

Here's a possible implementation for the HP-48G:

( mm dd yyyy -- dow )
Code:
« ROT
  IF DUP 3 <
  THEN 12 + SWAP 1 -
  ELSE SWAP
  END
  DUP 100 MOD
  SWAP 100 / IP
  → q m K J
  « { "Saturday"
      "Sunday"
      "Monday"
      "Tuesday"
      "Wednesday"
      "Thursday"
      "Friday" } 
    q
    m 1 + 2.6 * IP +
    K + K 4 / IP +
    J 4 / IP + J 2 * -
    7 MOD
    1 + GET
  »
»


Using local variables helps to avoid stack acrobatics.
It makes code maintainable without the need of pseudo-code in comments.
My rule of thumb is not juggling with more than three objects on the stack.

Valentin is able to write BASIC-code for the HP-71B that I often don't understand at a first glimpse. But that's okay when he's playing code golf.

Thus it's not so much about the language but about your attitude when writing code.

I agree with you though in terms of RPN due to its lack of local variables and means to structure code and data.
There it helps to have a mapping of variable names to registers, stack-diagrams or the implementation of the algorithm in a language like Python.

Kind regards
Thomas


I had a hard time understanding your code but that was more related to the question of why you used a different formula for handling m?
Just so you can start the list of weekdays with "Sunday" instead of "Saturday"?
Or is it based on a different source for the algorithm?

Or then why don't you reuse c in the calculation of d?
Code:
c = int(yr/100)
d = yr - 100*int(yr/100)
It's been defined in the line before.

BTW: There's a typo in your RPL code: "Thrusday" → "Thursday"
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Top three calculators ? - Sylvain Cote - 04-02-2018, 04:37 PM
RE: Top three calculators ? - CY-CL - 04-02-2018, 04:42 PM
RE: Top three calculators ? - emece67 - 04-02-2018, 04:48 PM
RE: Top three calculators ? - rprosperi - 04-02-2018, 06:09 PM
RE: Top three calculators ? - polbit - 04-02-2018, 06:22 PM
RE: Top three calculators ? - hth - 04-02-2018, 07:43 PM
RE: Top three calculators ? - Jlouis - 04-02-2018, 08:53 PM
RE: Top three calculators ? - John Keith - 04-02-2018, 09:01 PM
RE: Top three calculators ? - pier4r - 04-02-2018, 09:43 PM
RE: Top three calculators ? - DavidM - 04-02-2018, 09:54 PM
RE: Top three calculators ? - ttw - 04-02-2018, 11:05 PM
My top three calculators - striegel - 04-02-2018, 11:42 PM
RE: Top three calculators ? - Carsen - 04-03-2018, 05:02 AM
RE: Top three calculators ? - peacecalc - 04-03-2018, 06:58 AM
RE: Top three calculators ? - larthurl - 04-03-2018, 08:12 PM
RE: Top three calculators ? - pier4r - 04-03-2018, 10:19 AM
RE: Top three calculators ? - ChanTran - 05-05-2021, 06:44 PM
RE: Top three calculators ? - Gamo - 04-03-2018, 11:59 AM
RE: Top three calculators ? - pier4r - 04-03-2018, 12:47 PM
RE: Top three calculators ? - JimP - 04-03-2018, 12:32 PM
RE: Top three calculators ? - klesl - 04-03-2018, 01:45 PM
RE: Top three calculators ? - wtp - 04-03-2018, 05:57 PM
RE: Top three calculators ? - xmehq - 04-03-2018, 08:42 PM
RE: Top three calculators ? - peacecalc - 04-03-2018, 08:50 PM
RE: Top three calculators ? - toml_12953 - 04-03-2018, 08:55 PM
RE: Top three calculators ? - Harald - 04-03-2018, 09:13 PM
RE: Top three calculators ? - hp41cx - 04-03-2018, 11:48 PM
RE: Top three calculators ? - Waon Shinyoe - 04-04-2018, 09:11 PM
RE: Top three calculators ? - Voldemar - 04-04-2018, 11:25 PM
RE: Top three calculators ? - cjmcc - 04-05-2018, 03:10 AM
RE: Top three calculators ? - Graan - 04-05-2018, 07:14 AM
RE: Top three calculators ? - hp41cx - 04-05-2018, 12:02 PM
RE: Top three calculators ? - Harald - 04-05-2018, 06:30 PM
RE: Top three calculators ? - Logan - 04-05-2018, 06:44 PM
RE: Top three calculators ? - rprosperi - 04-05-2018, 07:34 PM
RE: Top three calculators ? - Harald - 04-05-2018, 08:41 PM
RE: Top three calculators ? - Harald - 04-05-2018, 08:33 PM
RE: Top three calculators ? - pier4r - 04-06-2018, 10:51 AM
RE: Top three calculators ? - lrdheat - 04-06-2018, 02:18 AM
RE: Top three calculators ? - pier4r - 04-06-2018, 12:04 PM
RE: Top three calculators ? - pier4r - 04-06-2018, 12:31 PM
RE: Top three calculators ? - Harald - 04-06-2018, 04:20 PM
RE: Top three calculators ? - rprosperi - 04-06-2018, 06:25 PM
RE: Top three calculators ? - Harald - 04-06-2018, 06:52 PM
RE: Top three calculators ? - toml_12953 - 04-06-2018, 08:11 PM
RE: Top three calculators ? - DA74254 - 04-06-2018, 08:30 PM
RE: Top three calculators ? - rprosperi - 04-06-2018, 09:51 PM
RE: Top three calculators ? - Harald - 04-06-2018, 10:15 PM
RE: Top three calculators ? - ttw - 04-06-2018, 02:12 PM
RE: Top three calculators ? - brickviking - 04-07-2018, 04:48 AM
RE: Top three calculators ? - Archilog - 04-08-2018, 12:55 AM
RE: Top three calculators ? - damaltor - 04-09-2018, 06:48 AM
RE: Top three calculators ? - aurelio - 04-09-2018, 07:23 PM
RE: Top three calculators ? - Thomas Okken - 04-09-2018, 07:51 PM
RE: Top three calculators ? - aurelio - 04-10-2018, 06:35 PM
RE: Top three calculators ? - Luigi Vampa - 04-09-2018, 09:39 PM
RE: Top three calculators ? - Helix - 04-09-2018, 09:57 PM
RE: Top three calculators ? - Craig Bladow - 04-11-2018, 06:33 PM
RE: Top three calculators ? - John Cadick - 04-12-2018, 07:05 AM
RE: Top three calculators ? - Thomas Klemm - 08-18-2018 04:41 PM
RE: Top three calculators ? - aurelio - 08-25-2018, 08:57 AM
RE: Top three calculators ? - DaveBr - 08-30-2018, 03:04 PM
RE: Top three calculators ? - Geoff - 08-30-2018, 10:14 PM
RE: Top three calculators ? - megarat - 09-02-2018, 04:24 AM
RE: Top three calculators ? - Paul Dale - 09-02-2018, 05:04 AM
RE: Top three calculators ? - toml_12953 - 09-02-2018, 04:02 PM
RE: Top three calculators ? - Jlouis - 09-02-2018, 05:08 PM
RE: Top three calculators ? - Archilog - 09-02-2018, 08:04 PM
RE: Top three calculators ? - Pjwum - 09-02-2018, 08:13 PM
RE: Top three calculators ? - Siegfried - 09-07-2018, 09:57 PM
RE: Top three calculators ? - Namir - 09-08-2018, 12:19 AM
RE: Top three calculators ? - Marcel Samek - 09-08-2018, 12:55 AM
RE: Top three calculators ? - Marcel Samek - 09-08-2018, 12:48 AM
RE: Top three calculators ? - DGM - 09-08-2018, 02:53 AM
RE: Top three calculators ? - edryer - 09-08-2018, 11:28 PM
RE: Top three calculators ? - tuxfish - 11-18-2018, 11:49 PM
RE: Top three calculators ? - badaze - 11-19-2018, 06:26 PM
RE: Top three calculators ? - StephenG1CMZ - 03-03-2019, 07:31 PM
RE: Top three calculators ? - KeithB - 11-21-2018, 02:22 PM
RE: Top three calculators ? - Craig Bladow - 11-21-2018, 07:35 PM
RE: Top three calculators ? - badaze - 11-19-2018, 06:28 PM
RE: Top three calculators ? - edryer - 11-20-2018, 08:15 AM
RE: Top three calculators ? - Claudio L. - 11-20-2018, 10:45 PM
RE: Top three calculators ? - xmehq - 11-23-2018, 11:49 PM
RE: Top three calculators ? - KeithB - 11-30-2018, 04:37 PM
RE: Top three calculators ? - Craig Bladow - 02-27-2019, 07:32 PM
RE: Top three calculators ? - GVHOVE - 02-27-2019, 08:33 PM
RE: Top three calculators ? - Thomas Okken - 03-03-2019, 02:17 PM
RE: Top three calculators ? - GVHOVE - 03-04-2019, 06:02 PM
RE: Top three calculators ? - JSS - 03-03-2019, 09:42 AM
RE: Top three calculators ? - cdmackay - 03-03-2019, 04:14 PM
RE: Top three calculators ? - JSS - 03-03-2019, 05:36 PM
RE: Top three calculators ? - Erwin - 03-03-2019, 11:41 AM
RE: Top three calculators ? - StephenG1CMZ - 03-04-2019, 09:04 AM
RE: Top three calculators ? - Bill Duncan - 03-04-2019, 11:02 PM
RE: Top three calculators ? - Craig Bladow - 03-09-2019, 04:22 AM
RE: Top three calculators ? - freelanzr - 04-10-2019, 02:10 AM
RE: Top three calculators ? - freelanzr - 06-19-2019, 08:54 AM
RE: Top three calculators ? - Marco Polo - 06-19-2019, 12:21 PM
RE: Top three calculators ? - BartDB - 07-04-2019, 12:43 PM
RE: Top three calculators ? - John Keith - 07-04-2019, 07:49 PM
RE: Top three calculators ? - Archilog - 09-08-2019, 03:18 AM
RE: Top three calculators ? - toml_12953 - 09-14-2019, 03:51 PM
RE: Top three calculators ? - Marco Polo - 09-14-2019, 08:07 PM
RE: Top three calculators ? - Domino - 06-20-2019, 10:34 AM
RE: Top three calculators ? - jlind - 09-08-2019, 08:43 AM
RE: Top three calculators ? - edryer - 09-08-2019, 08:19 PM
RE: Top three calculators ? - Luigi Vampa - 10-29-2020, 09:53 PM
RE: Top three calculators ? - HP67 - 09-12-2019, 08:32 PM
RE: Top three calculators ? - badaze - 09-14-2019, 01:23 PM
RE: Top three calculators ? - BusyCalc - 09-13-2019, 06:33 AM
RE: Top three calculators ? - HP67 - 09-14-2019, 08:46 PM
RE: Top three calculators ? - Marco Polo - 09-15-2019, 08:35 AM
RE: Top three calculators ? - toml_12953 - 09-15-2019, 02:17 PM
RE: Top three calculators ? - groskwik - 09-15-2019, 09:16 PM
RE: Top three calculators ? - tom234 - 10-29-2020, 05:45 PM
RE: Top three calculators ? - Mike T. - 10-29-2020, 10:40 PM
RE: Top three calculators ? - grsbanks - 10-30-2020, 09:46 AM
RE: Top three calculators ? - CMarangon - 10-31-2020, 06:39 PM
RE: Top three calculators ? - WesPerdue - 11-03-2020, 09:28 AM
RE: Top three calculators ? - vaklaff - 11-03-2020, 01:16 PM
RE: Top three calculators ? - WesPerdue - 11-03-2020, 05:34 PM
RE: Top three calculators ? - Davidy - 11-14-2020, 02:48 AM
RE: Top three calculators ? - ttw - 11-14-2020, 03:20 AM
RE: Top three calculators ? - EdS2 - 11-15-2020, 11:08 AM
RE: Top three calculators ? - Mike T. - 04-26-2021, 10:01 PM
RE: Top three calculators ? - OtakuLab - 11-17-2020, 12:24 PM
RE: Top three calculators ? - Peet - 01-08-2021, 08:32 AM
RE: Top three calculators ? - lmmt61 - 01-08-2021, 09:06 AM
RE: Top three calculators ? - Coco - 01-08-2021, 05:10 PM
RE: Top three calculators ? - MNH - 01-09-2021, 05:22 AM
RE: Top three calculators ? - MNH - 01-09-2021, 01:34 PM
RE: Top three calculators ? - Chr Yoko - 01-09-2021, 01:48 PM
RE: Top three calculators ? - Gilles - 01-09-2021, 08:30 PM
RE: Top three calculators ? - Uli - 01-09-2021, 10:41 PM
RE: Top three calculators ? - Sukiari - 01-10-2021, 07:17 AM
RE: Top three calculators ? - OlidaBel - 04-27-2021, 07:54 AM
RE: Top three calculators ? - isanchez - 05-03-2021, 06:46 AM
RE: Top three calculators ? - Dave Britten - 05-04-2021, 02:28 PM
RE: Top three calculators ? - HP-71C - 08-26-2021, 09:33 PM
RE: Top three calculators ? - BusyCalc - 08-27-2021, 10:55 AM
RE: Top three calculators ? - Ren - 08-27-2021, 01:20 PM
RE: Top three calculators ? - Ren - 08-30-2021, 03:49 PM
RE: Top three calculators ? - polbit - 09-04-2021, 12:47 PM
RE: Top three calculators ? - Nihotte(lma) - 09-05-2021, 01:11 PM
RE: Top three calculators ? - Helix - 09-06-2021, 11:23 PM
RE: Top three calculators ? - Ren - 10-24-2021, 02:03 AM
RE: Top three calculators ? - Liamtoh Resu - 10-22-2021, 04:49 AM
RE: Top three calculators ? - Alevin - 10-23-2021, 06:52 PM
RE: Top three calculators ? - tangdfx - 03-27-2022, 06:25 AM



User(s) browsing this thread: 1 Guest(s)