# narrow down a range algorithm

Joe Robles
Greenhorn
Posts: 11
Hi all,

I have a range of two date values. I need to write logic that keeps breaking the range further into lesser ranges till no more ranges are avaiable.

Is there any algorithm that I can use to do this in java.

Any help is highly appreciated.

Example:

start date - 01-01-1981
end date - 01-01-1988

The program must add the above and divide by 2 repeatedly so I get the following ranges.

1981-1988
1988-1994
1994-2000
2000-2006

Henry Wong
author
Marshal
Posts: 21711
85
The program must add the above and divide by 2 repeatedly so I get the following ranges.

1981-1988
1988-1994
1994-2000
2000-2006

Can you elaborate your example a bit? I don't see any adding and divide by 2 going on.

Henry

Joe Robles
Greenhorn
Posts: 11
Hi

New example:

Date range that needs to be broken: 1960 - 2000

I chose to add the above values and divide by two; and need to repeat the same for each of the resultant ranges; then the resultant ranges need to end up like this:

1960 - 1965
1965 - 1970
1970 - 1975
1975 - 1980
1980 - 1985
1985 - 1990
1990 - 1995
1995 - 2000

Hope this helps...

Thanks.

Campbell Ritchie
Sheriff
Posts: 50646
83
Those aren't divided ranges. They have an overlap. What you actually want is
1961 to 2000

1961 to 1980, 1981 to 2000

1961 to 1970, 1971 to 1980, 1981 to 1990, 1991 to 2000, etc.