Matt
Matt

Reputation: 11327

What does this code do?

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.

Upvotes: 1

Views: 243

Answers (1)

David Watson
David Watson

Reputation: 2039

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);
    }
}

Upvotes: 1

Related Questions