Skip to content

Instantly share code, notes, and snippets.

@ffredyk
Last active March 26, 2022 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffredyk/bfbee1e0c9e682349c6b4a119b2dea55 to your computer and use it in GitHub Desktop.
Save ffredyk/bfbee1e0c9e682349c6b4a119b2dea55 to your computer and use it in GitHub Desktop.
Pi Calculation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
namespace PiCalc
{
public static class PiCalc
{
static BigInteger FOUR = new BigInteger(4);
static BigInteger SEVEN = new BigInteger(7);
static BigInteger TEN = new BigInteger(10);
static BigInteger THREE = new BigInteger(3);
static BigInteger TWO = new BigInteger(2);
static BigInteger k = BigInteger.One;
static BigInteger l = new BigInteger(3);
static BigInteger n = new BigInteger(3);
static BigInteger q = BigInteger.One;
static BigInteger r = BigInteger.Zero;
static BigInteger t = BigInteger.One;
public static async Task Main()
{
//Token pro zrušení početního cyklu
var cancel = new CancellationTokenSource();
//Async vlákno pro odchycení user inputu
var taskCancel = Task.Run(() =>
{
Console.WriteLine("Press ENTER to stop execution and print result");
Console.ReadLine();
cancel.Cancel(); //Aktivuje token pro zrušení cyklu
});
//Uchovat info o pozici kurzoru v konzoli (pro následné přepsání dalšími hodnotami)
var cursor = Console.GetCursorPosition();
Console.SetCursorPosition(cursor.Left, cursor.Top);
//LOGIKA: Výpočet Pi
//https://rosettacode.org/wiki/Pi#C.23
var elapsed = Stopwatch.StartNew(); //Měření celkového času
BigInteger nn, nr;
bool first = true;
string piresult = ""; //Proměnná pro úschovu výpočtu (nešťastný string)
while (!cancel.IsCancellationRequested) //Cyklus s tokenem pro výstup
{
var watch = Stopwatch.StartNew(); //Měření času iterace
if ((FOUR * q + r - t).CompareTo(n * t) == -1)
{
piresult = piresult.Append(n); //Nacpání číslice do outputu
if (first)
{
piresult = piresult.Append("."); //Nacpání tečky při první iteraci (za číslici 3)
first = false;
}
nr = TEN * (r - (n * t));
n = TEN * (THREE * q + r) / t - (TEN * n);
q *= TEN;
r = nr;
}
else //??
{
nr = (TWO * q + r) * l;
nn = (q * (SEVEN * k) + TWO + r * l) / (t * l);
q *= k;
t *= l;
l += TWO;
k += BigInteger.One;
n = nn;
r = nr;
}
watch.Stop(); //Konec měření času iterace
Console.WriteLine($"Calculating iteration {l}..".PadRight(Console.WindowWidth, ' '));
Console.WriteLine($"Iteration time: {watch.ElapsedMilliseconds}ms".PadRight(Console.WindowWidth, ' '));
Console.WriteLine($"Iteration ticks: {watch.ElapsedTicks}".PadRight(Console.WindowWidth, ' '));
Console.WriteLine($"Total time elapsed: {elapsed.Elapsed.TotalSeconds}s".PadRight(Console.WindowWidth, ' '));
Console.WriteLine($"Number of digits: {piresult.Length}x".PadRight(Console.WindowWidth, ' '));
Console.SetCursorPosition(cursor.Left, cursor.Top); //Návrat kurzoru console zpět na výchozí bod
}
//Oddíl po ukončení cyklu tokenem
Console.Clear();
Console.WriteLine("Results are:");
Console.Write(piresult);
Console.ReadLine();
}
public static string Append(this string str, object append)
{
return string.Format("{0}{1}", str, append);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment