# Determining which range (bucket) a given number belongs to

I have a sequence of ranges (i.e., buckets or pigeon-holes), starting at 0, all the same size (for example, 32):

[0…31], [32…63], [64…95], …

Given a non-negative integer, how can I determine (in a bash or other shell script) which bucket it belongs to? For example:

1. if a given number=15 then response = [0…31] (i.e., min=0 and max=31)
2. if a given number=35 then response = [32…63] (i.e., 32 && 63)
• This could be clarified a lot. How exactly are the min and max values formed? What's the meaning of the last value of 10 000? Aug 27, 2016 at 13:31
• @ikkachu, MIN starts from zero and the increment is 32 while the MAX is the highest number in a particular range as shown in my example. 10000 can be used as the limits but it's not important. Aug 27, 2016 at 14:32
• This is actually a math problem. Once you figure out the equation to use, you can always come back to get help implementing it in a shell script if you want. Aug 27, 2016 at 16:10

How about something like this?

``````range() {
# increment
incr=\$1
# input
input=\$2

# mathematically, min = floor( input/incr )*incr
# then max = min+incr-1
# but `floor' isn't necessary in bash because it uses integer division

min=\$(( input/incr*incr ))
max=\$(( min+incr-1 ))

echo \$min \$max
}
``````

Then:

``````\$ range 32 15
0 31
\$ range 32 35
32 63
``````