views:

114

answers:

2
function mystery($y, $m, $d) {

  $a = 0;
  $b = 0;
  $c = 0;

  if($m < 3) {
    $a = $m + 10;
    $b = ($y-1) % 100;
    $c = ($y-1) / 100;
  }
  else {
    $a = $m - 2;
    $b = $y % 100;
    $c = $y / 100;
  }

  $w = (700 + (((26*$a)-2)/10)+$d+$b+$b/4+$c/4-(2*$c))%7;
  echo $w;

}

One of my tutorial questions asks what the function calculates. I can go through and explain every calculation, but I'm sure that's not what we're expected to do. Is there any obvious use I'm not seeing?

It looks to me like it could be a checksum algorithm, because it always seems to generate a digit between 0 and 6.

ps, it was originally written in Java but I ported it to PHP for simplicity when I typed it into my computer to test. I can re-type the Java version in if anyone would prefer it.

+7  A: 

What does %7 suggest?

Marcelo Cantos
haha, it's so obvious now. I'm ashamed I didn't notice it straight away..
Matt
Hindsight has 20-20 vision. I didn't spot it immediately either, even though I noticed the `%7` straight away.
Marcelo Cantos
also the `d` `m` `y` variable names might have given a clue!
theomega
+1  A: 

Think about dates and what Marcelo posted. Here is compilable java. Try running the program with various inputs and see what you come up with.

class mys {
    public static void main(String[] args) {
          int y= Integer.parseInt(args[0]);
          int m= Integer.parseInt(args[1]);
          int d = Integer.parseInt(args[2]);
          int a = 0;
          int b = 0;
          int c = 0;

          if(m < 3) {
            a = m + 10;
            b = (y-1) % 100;
            c = (y-1) / 100;
          }
          else {
            a = m - 2;
            b = y % 100;
            c = y / 100;
          }

          int w = (700 + (((26*a)-2)/10)+d+b+b/4+c/4-(2*c))%7;
          System.out.println(w);
    }
}
David Watson