From: Josef 'Jupp' Schugt Date: 2007-09-02T01:53:43+09:00 Subject: Re: Help with leap year programing -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 * HB: > On chapter 6, I got same assignment. I guess that is a classic one. So here is a nonclassic solution to this classic problem :) If s % 4 differs from 0 we start with a year that cannot be a leap year. If this occurs add 4 - (s % 4) to skip to the next year that is a leap year unless century rules happen to apply. Now only every fourth year needs to be considered. A year that can be divided by four is a leap year under the condition that it can be divided by 400 or cannot be divided by 100. puts 'starting year:' s = gets.chomp.to_i puts 'ending year:' e = gets.chomp.to_i if s > e puts 'ending year should be bigger than staring year' else puts "leap year between #{s} and #{e} as below:" end s += 4 - (s % 4) if s % 4 != 0 while s <= e puts s if (s % 400 == 0 or s % 100 != 0) s += 4 end puts 'all done' Josef 'Jupp' Schugt - -- Blog available at http://www.mynetcologne.de/~nc-schugtjo/blog/ PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org iD8DBQFG2ZkSrhv7B2zGV08RAmuVAJ4xCwywVTVlXg2S3kUvtibQcynnmgCg2hHD Mpa+a5kp4DdFSqLqMa+FDQQ= =T/yP -----END PGP SIGNATURE-----