File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different way of thinking about Leap

 
John Peters
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason I couldn't wrap my mind around how the leap years should be tested. After using three sheets of paper to write out how the logic would make sense, and using Wikipedia, I think I've come up with something that makes more sense in my mind. I hope it helps someone else:

In simple terms:


Where the program should terminate kept hanging me up.

Good help from Wikipedia:

...most years whose division by 4 equals an integer are leap years.
Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Peters:
If the year is divisible by 400, than it's true and program should exit.
If the year is not divisible by 100 but IS (and) divisible by 4, than it's true and should exit.
Otherwise it's false and the program should exit.

I think I would change it just a little bit ...
If the year is divisible by 400, than it's true -- final answer
If the year is divisible by 4 but (and) is not divisible by 100, than it's true -- final answer
Otherwise it's false -- final answer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic