C# :: Aufgabe #184
4 Lösungen

Wurzel ziehen mit Intervallschachtelung
Anfänger - C#
von Felix
- 11.07.2017 um 21:30 Uhr
Schreibe eine Methode die aus einer Zahl die Wurzel zieht, benutze dafür die Intervallschachtelung.
Lösungen:

using System; public class Program { public static void Main() { double number = 99; int intervalIterations = 100; double root = number/2; double lowerIntervalLimit = 0; double upperIntervalLimit = number; for (int i = 0; i <= intervalIterations; i++) { if (Math.Pow(root,2) < number) { lowerIntervalLimit = root; root = (root+upperIntervalLimit)/2; } else if (Math.Pow(root,2) > number) { upperIntervalLimit = root; root = (root+lowerIntervalLimit)/2; } else { break; } } Console.WriteLine("Die Wurzel aus {0} lautet {1}" , number, root); } }

using System; using static System.Console; namespace wurzel_184 { public static class Program { static void Main() { double zahl = 70.12345678; ("wurzel(" + zahl.ToString() + ") = " + zahl.wurzel(20).ToString()).EndMessage(); zahl = 0.12345678; ("wurzel(" + zahl.ToString() + ") = " + zahl.wurzel(20).ToString()).EndMessage(); zahl = 4.0; ("wurzel(" + zahl.ToString() + ") = " + zahl.wurzel(20).ToString()).EndMessage(); zahl = -4.0; ("wurzel(" + zahl.ToString() + ") = " + zahl.wurzel(20).ToString()).EndMessage(); } static double wurzel(this double zahl, int nachkommastellen) { if (zahl < 0.0) return double.NaN; double delta = 1.0, start = 0.0, merker = start, grenze = 1.0 / Math.Pow(10.0, nachkommastellen); do { while(start * start < zahl) { merker = start; start += delta; }; start = merker; delta /= 10.0; } while ((grenze < delta) && (delta > 1.0E-15)); return start; } static void EndMessage(this string s) { WriteLine(s); ReadKey(true); } } }

using System; namespace ConsoleIntervallschachtelung { class Program { static void Main(string[] args) { Console.WriteLine("----- Wurzel ziehen mit Intervallschachtelung -----"); Console.Write("Geben Sie eine positve Zahl ein: "); decimal d; if (decimal.TryParse(Console.ReadLine(), out d) && d >= 0) { Console.Write("Anzahl der Nachkommastellen (0-14): "); int decimals; if (int.TryParse(Console.ReadLine(), out decimals) && decimals >= 0 && decimals <= 14) { decimal sqrt = Sqrt(d, decimals); Console.WriteLine($"Die Wurzel von {d} ist ungefähr: {sqrt}"); } else { Console.WriteLine("Fehlerhafte Eingabe"); } } else { Console.WriteLine("Fehlerhafte Eingabe"); } Console.ReadLine(); } static decimal Sqrt(decimal d, int decimals, int currentDecimals = 0, decimal start = 0) { if (d < 0) { return decimal.MinusOne; } decimal increment = (decimal)Math.Pow(10, -currentDecimals); for (decimal i = start; i < int.MaxValue; i += increment) { decimal a = Math.Round((decimal)Math.Pow((double)i, 2), currentDecimals * 2); decimal b = Math.Round((decimal)Math.Pow((double)(i + increment), 2), currentDecimals * 2); if (a == d) { return i; } else if (b == d) { return i + increment; } else if (a < d && d < b) { if (currentDecimals >= decimals) { return Math.Round(i, decimals); } return Math.Round(Sqrt(d, decimals, currentDecimals + 1, i), decimals); } } return decimal.MinusOne; } } }

Console.WriteLine("From which number you wanna extract the root?"); double number = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Which kind of root you wanna extract?"); double root = Convert.ToDouble(Console.ReadLine()); double exponent = 1 / root; double result; if (number >= 0) { result = Math.Pow(number, exponent); Console.WriteLine("Result:" + result.ToString()); } else Console.WriteLine("You can not extract a root with a negativ number ");