Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update and rename Exercise_06/Exercise_06_22/Exercise_06_22.java to E…#60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
yusufErpak wants to merge1 commit intojsquared21:master
base:master
Choose a base branch
Loading
fromyusufErpak:patch-2
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@
Babylonian method. It approximates the square root of a number, n, by repeatedly
performing a calculation using the following formula:


nextGuess = (lastGuess + n / lastGuess) / 2

When nextGuess and lastGuess are almost identical, nextGuess is the
Expand All@@ -19,30 +20,27 @@ public static double sqrt(long n)

public class Exercise_06_22 {
/** Main Method */
public class Bolum6_22_YaklasikKarakök {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a Scanner

// Prompt the user to enter an integer
System.out.print("Enter a number: ");
long number = input.nextLong();

// Display the square root
System.out.println(
"The approximated square root of " + number + " is: " + sqrt(number));
Scanner input = new Scanner(System.in);
System.out.println("Enter a number.");
long n = input.nextLong();
sgrt(n);
}

/** Method squrt approximates the square root of n */
public static double sqrt(long n) {
long lastGuess = 1;// Initial guess to positive value
long nextGuess = (lastGuess + n / lastGuess) / 2;

// If the difference between nextGuess and lastGuess is less than 0.0001,
// return nextGuess as the approximated square root of n.
while (nextGuess - lastGuess > 0.0001) {
public static double sgrt(long n) {
double lastGuess = 1;
double nextGuess = (lastGuess + n / lastGuess) / 2;
double m = n;
while (n == m) {
lastGuess = nextGuess;
nextGuess = (lastGuess + n / lastGuess) / 2;
if (nextGuess - lastGuess == 0.0) {
System.out.println(n + " is result sgrt: " + nextGuess);
m++;
}

}
lastGuess = nextGuess;
return nextGuess = (lastGuess + n / lastGuess) / 2;
return n;
}
}

[8]ページ先頭

©2009-2025 Movatter.jp