Home > Software engineering >  Simple algorithm to alternate days
Simple algorithm to alternate days

Time:05-30

I need to alternate between 2 tasks every day, and I need a simple algorithm to know which task I need to do.

I need to be able to run this algorithm by head, using simple general knowledge (like day of week, day of month, etc), and it must not rely of which task has been done the previous day (because I have a crappy memory).

I have tried checking for parity in a combination of day of week / day of month / # of month, etc, but couldn't find a suitable system: day of week have 2 consecutive odd numbers, same goes for day of month every so often.

CodePudding user response:

I am afraid that this is impossible: if you can't remember what you did the day before, any other procedure will require more mnemonic effort.

  • remember what you did on January first (or another date),

  • remember the parities of the cumulated months: oeoeoeooeoe or ooeoeoeeoeo for a leap year,

  • add the cumulated parity of the month before* to the parity of the day,

  • add that to the parity of the first task.

E.g. if A on January 1st 2022, then on March 17, 2022: e o = o gives B.


*In January, use even.

You can also state the month parity rule as: until August inclusive, use the co-parity of the month number; then use the parity. But for a leap year, change that parity after February (excluded).

CodePudding user response:

<?php
     $day = Date('j');

     if($day%2==0)
          echo "Task 1";
     }else{
          echo "Task 2";
     }
   ?>

CodePudding user response:

I need to be able to run this algorithm by head

So, you don't need to take help of Computer science. You can use cognitive human ability to map a thing to another thing.

Note: This need not make sense to everybody though, if you are thinking out of the box.

  • Map task 1 as God's day.
  • Map task 2 as Devil's day in your brain.
  • This should be simple just like day and night.
  • Now, remember that devil's evil karma is always burnt by God the next day and that devil never learns his lesson. So this way, alternating would be easy.

Friends Episode snippet on Youtube

CodePudding user response:

Just count the number of days in between your date and a given "zero" one...then use parity.

Take number of seconds (or milli, or whatever) since EPOCH (common zero for date and time), divide (integer division) by 60x60x24 (or 1000x60x60x24, or what is appropriate), you then get the number of days since EPOCH.

----EDIT----

Example: Got 1653910695 seconds since EPOCH (at the time of my experience). Dividing it by 60x60x24 give 19142 days. To morrow it will give 19143, etc.

  • Related