The goal is as follows:

In the Gregorian calendar, the year is a leap year in two cases: either it is a multiple of 4, but not a multiple of 100, or a multiple of 400.

Implement a method that calculates the number of leap years from the beginning of our era (first year) to a given year inclusive. In fact, the Gregorian calendar was introduced much later, but here, for simplicity, we extend its effect throughout our era.

Method Format:

public static int leapYearCount(int year) { return year; } 

Nothing comes to mind ... In principle, the task would be simple. But I need to solve it without using if and for . Only arithmetic and bit operations are allowed. Can someone tell me how to decide or direct the right path?

    1 answer 1

    Well, the easiest option - just count. Leap year every 4 - i.e. year/4 . But every 100 is not a leap year then subtract year/100 . Every 400 is nonetheless a leap year. Total:

     return year/4 - year/100 + year/400; 

    I assume that 0 year was not.

    In general (not for educational purposes), I would use the methods of the desired class from the Calendar interface.