Reputation: 177
I have a mathematical range [a, b] and I am trying to break the range into 'n' equal parts. The numbers, a and b can be anything but for an example let's assume the range is [0, 1].
Basically what I am trying to do is let's say that if n = 4
and the range is [0, 1]
then I want breakpoints for that range to be calculated as [.25, .5, .75]
because these divide [0, 1]
into 4 equal points. Similarly, if the range is [0, 1]
and n = 2
, I want the breakpoints to be [.5].
What is the fastest way of computing these breakpoints in java? Is there a built-in function for doing this sort of thing?
Upvotes: 2
Views: 11413
Reputation: 618
If the range is [a,b]
and it has to be divided in n
partitions, then one approach can be:-
public class Range
{
public static void main(String[] args)
{
double lowLimit = a;
double highLimit = b;
double difference = (b-a);
double partition = difference/n; // n is the number partitions of the range [a,b]
System.out.println("The partitions are:");
for(int i = 1;i<=n; i++)
{
lowLimit += partition;
System.out.print(lowLimit+"\t");
}
System.out.println();
}
}
Upvotes: 2
Reputation: 14276
Say you have numbers [x,y]
and you want n
parts. Then you could do calculate that difference:
diff = (y-x)/n
Now create a variable d
which equals to x
and loop from 1
to n
. As you loop, add diff
to d
and print d
. Some pseudo code:
diff = (y-x)/n;
d = x;
for(1 to n)
d = d + diff
print(d)
NOTE: Use double
.
Upvotes: 8
Reputation: 5586
Code just to give you an idea where to start:
int a = 0;
int b = 1;
int n = 4;
double fraction = (b - a) / (double) n;
for (int i = 0; i < n; ++i) {
System.out.println("Lower bound: " + i * fraction + "; Upper bound: " + (i + 1) * fraction);
}
Upvotes: 0
Reputation: 3023
Here's an approach with while loop:
public class Breakpoint
{
public Breakpoint()
{
double start = 0;
double end = 1;
double n = 4;
double difference = (end - start) / n;
double value = start;
while (value < end)
{
value += difference;
System.out.println(value);
}
}
public static void main(String[] args)
{
new Breakpoint();
}
}
Upvotes: 0
Reputation: 489
double dx = ((upper_bound - lower_bound)/n)
This will calculate the space between each point in the range.
If you wanted to place these points in an array, you would do as follows:
double value = lower_bound;
for (int i = 0; i < n; i++) {
double value += dx;
double_array[i] = value;
}
You haven't specified what sort of data structure you are working with, so beyond this simple array implementation I cannot help you without further explanation.
Upvotes: 0
Reputation: 11143
Not sure if this is what you need (to print breaks, or System break on those breakPoints), anyway, both are over here
double division += (maxRange - minRange)/n;
for(int i=0; i<=n; i++){
double minRange = 0;
double maxRange = 1;
double breaks[i] = division;
//System.out.println(breaks[i]); ----------------------USE THIS ONE IF PRINTING BREAKPOINT IS NEEDED
//new java.util.Scanner(System.in).nextLine(); --------THIS LINE STOPS THE PROGRAM UNTIL YOU PRESS A KEY
}
Upvotes: 0