I have no idea what I'm doing wrong here. My math is off but by the smallest ammount.
I tried changing the DecimalFormat but I know its correct at where it's set now. I looked at using long but I don't see why that would make a difference I'm not going past the size of double.
I made sure to check that I was working with all doubles. I'm not even sure exactly how to troubleshoot this.
Input data:
12
657 7
9079 13
9551 8
4633 7
8622 12
98 2
254 2
12040 11
14711 10
46254 8
8978
62424 2
Expected result:
25.6343560524 95.2837866586 98.7716099678 71.3069977502 92.8547252433 25.7398989899 64.7460784314 109.726933795 121.288922541 258.904619449 566.427585376 15607.249984
My result:
25.6343560524 95.2837866586 98.7716099678 71.3069977502 92.8547252433 25.7398989899 64.7460784314 109.7269337948 121.2889225409 258.9046194492 566.4275853759 15607.2499839808
My code:
package com.secryption.CA18SquareRoot;
import java.text.DecimalFormat;
import java.util.Scanner;
public class SquareRoot {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter Data: ");
int numberOfTestCases = myScanner.nextInt();
double r;
DecimalFormat df = new DecimalFormat("##.##########");
for (int i = 0; i < numberOfTestCases; i++) {
r = 1.0;
int calculateSquareRootFor = myScanner.nextInt();
int numberOfSteps = myScanner.nextInt();
double d;
if (numberOfSteps > 0) {
for (int w = 0; w < numberOfSteps; w++) {
d = (double) calculateSquareRootFor / r;
r = (r + d) / 2.0;
}
}
System.out.print((df.format(r) + " "));
}
}
}
EDIT: After talking to the site owner, double is correct. I ended up trying a few different data sets and it finally saw it as correct.