I am supposed to write a Java program to sum the following sequence: 1.0/1.0 + 1.0/2.0 + 1.0/3.0.... + 1.0/15,000,000.0 in increasing order and then again in decreasing order starting from 1.0/15,000,000.0 + 1.0/14,999,999 all the way down to 1.0/1.0 using 32 bit floating point. I'm struggling to figure out how to do this, but here is what I have so far (no idea if it will work):
EDIT: Sorry to open this thing back up, but I'm getting a 1.0 for both answers and I'm pretty sure that's incorrect. Does anyone know what I did wrong?
public class FloatSum {
public static float increasingSum (float numbers1){
float sum1 = 0;
for (int i = 1; i <= 15000000; i++){
sum1 = sum1 + 1/i;
}
return sum1;
}
public static float decreasingSum (float numbers2){
float sum2 = 0;
for (int i = 15000000; i >= 1; i--){
sum2 = sum2 + 1/i;
}
return sum2;
}
public static void main(String[] args) {
float sum1 = 0;
float sum2 = 0;
System.out.println(increasingSum(sum1));
System.out.println(increasingSum(sum2));
}
}