Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. package bugfix6;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Bugfix6 {
  6.  
  7. public static void main(String[] args) {
  8. Scanner input = new Scanner(System.in); // Create a Scanner
  9.  
  10. System.out.print("Enter a number: ");
  11. long number = input.nextLong();
  12.  
  13.  
  14. System.out.println(
  15. "The approximated square root of " + number + " is: " + sqrt(number));
  16. }
  17.  
  18. public static double sqrt(long n) {
  19. long lastGuess = 0;
  20. long nextGuess = (lastGuess + n / lastGuess) / 2;
  21.  
  22. while (nextGuess - lastGuess > 0.0001) {
  23. lastGuess = nextGuess;
  24. nextGuess = (lastGuess + n / lastGuess) / 2;
  25. }
  26. lastGuess = nextGuess;
  27. return nextGuess = (lastGuess + n / lastGuess) / 2;
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement