C# :: Aufgabe #183
4 Lösungen
Funktion um Pi zu berechnen.
Anfänger - C#
von Felix
- 09.07.2017 um 21:59 Uhr
Schreibe eine Methode um Pi zu berechnen. Versuche Pi auf so viele Stellen wie möglich zu berechnen.
Lösungen:
using System;
using static System.Console;
using System.Numerics; //muss zu Verweisen hinzugefügt werden
using System.Diagnostics;
namespace pi_183{
class Program {
static string NL = Environment.NewLine, LZ = " ";
static void Main() {
Stopwatch sw = new Stopwatch();
sw.Start();
int genauigkeit = 1001;
WriteLine("Garantierte Genauigkeit: " + (genauigkeit - 1).ToString("n0") + " Nachkommastellen" + NL);
int n = 10;
BigInteger pi1 = 0, pi2 = 0;
bool bo_ok = false;
while (!bo_ok) {
pi1 = pi2;
rational L2 = new rational();
L2 = Lambert(1, n + 1);
BigInteger p2 = BigInteger.Pow(10, L2.zaehler.ToString().Length);
pi2 = p2 * 4 * L2.nenner / L2.zaehler;
int identisch = pi2.identisch_bis(pi1);
bo_ok = genauigkeit <= identisch;
n++;
}
WriteLine(pi2.ToString().MitCharAnzahl(genauigkeit));
WriteLine(NL + Math.PI * Math.Pow(10, 14));
sw.Stop();
TimeSpan ts = sw.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
WriteLine(NL + "Rechenzeit " + elapsedTime);
ReadKey();
}
#region biginteger region
public struct rational
{
public BigInteger zaehler;
public BigInteger nenner;
}
public static rational div(rational z1, rational z2) {
rational erg;
erg.zaehler = z1.zaehler * z2.nenner;
erg.nenner = z1.nenner * z2.zaehler;
return erg;
}
public static rational add(rational z1, rational z2) {
rational erg;
erg.nenner = z1.nenner * z2.nenner;
erg.zaehler = z1.zaehler * z2.nenner + z2.zaehler * z1.nenner;
return erg;
}
public static rational Lambert(int k, int n) { // nach Lamberts Kettenbruch
// lambert(k) = 2 * k - 1 + k * k / lambert(k + 1)
rational erg;
if (k == n)
{
erg.zaehler = 1;
erg.nenner = 1;
return erg;
}
rational a, b;
a.zaehler = 2 * k - 1; a.nenner = 1;
b.zaehler = k * k; b.nenner = 1;
erg = add(a, div(b, Lambert(k + 1, n)));
return erg;
}
#endregion
}
public static class MyExtensions {
public static string MitCharAnzahl(this string s, int chars) {
string result = string.Empty;
for (var i = 0; i < s.Length; i++)
if (i < chars)
result += s[i].ToString();
else
break;
return result;
}
public static int identisch_bis(this BigInteger z1, BigInteger z2)
{
int result = 0;
char[] c1 = z1.ToString().ToCharArray();
char[] c2 = z2.ToString().ToCharArray();
for (var i = 0; i < Math.Min(c1.Length, c2.Length); i++)
if (c1[i] == c2[i])
result++;
else
break;
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PI
{
class Program
{
static void Main(string[] args)
{
double pi = 4/1;
bool b = false;
double i = 1;
while(i < Double.MaxValue)
{
i += 2;
pi -= (4 / i);
i += 2;
pi += (4 / i);
Console.WriteLine("" + pi);
}
Console.WriteLine("" + pi);
Console.ReadLine();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace pi
{
class Program
{
static void Main(string[] args)
{
Random zufall = new Random();
double innerhlab = 0;
int tropfen = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i <= tropfen;i++)
{
double x = zufall.NextDouble();
double y = zufall.NextDouble();
if(x*x+y*y < 1.0)
{
innerhlab++;
}
}
double pi = 4*innerhlab / tropfen;
Console.WriteLine(pi);
Console.Read();
}
}
}using System;
namespace CS_Aufgabe_183_Pi
{
class Program
{
static void Main(string[] args)
{
double n = 1e8;
int counter = 0;
Random rnd = new Random();
for (int i = 0; i < n; i++)
{
double x = rnd.NextDouble();
double y = rnd.NextDouble();
if (Math.Sqrt(Math.Pow(x, 2e0) + Math.Pow(y, 2e0)) < 1e0) {
counter++;
}
}
Console.WriteLine($"pi = {4e0 * counter / n}");
Console.ReadKey();
}
}
}
