Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,78 @@
|
|||
namespace RosettaCode.ArithmeticGeometricMean
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static double ArithmeticGeometricMean(double number,
|
||||
double otherNumber,
|
||||
IEqualityComparer<double>
|
||||
comparer)
|
||||
{
|
||||
return comparer.Equals(number, otherNumber)
|
||||
? number
|
||||
: ArithmeticGeometricMean(
|
||||
ArithmeticMean(number, otherNumber),
|
||||
GeometricMean(number, otherNumber), comparer);
|
||||
}
|
||||
|
||||
private static double ArithmeticMean(double number, double otherNumber)
|
||||
{
|
||||
return 0.5 * (number + otherNumber);
|
||||
}
|
||||
|
||||
private static double GeometricMean(double number, double otherNumber)
|
||||
{
|
||||
return Math.Sqrt(number * otherNumber);
|
||||
}
|
||||
|
||||
private static void Main()
|
||||
{
|
||||
Console.WriteLine(
|
||||
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
|
||||
new RelativeDifferenceComparer(1e-5)).
|
||||
ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
private class RelativeDifferenceComparer : IEqualityComparer<double>
|
||||
{
|
||||
private readonly double _maximumRelativeDifference;
|
||||
|
||||
internal RelativeDifferenceComparer(double maximumRelativeDifference)
|
||||
{
|
||||
_maximumRelativeDifference = maximumRelativeDifference;
|
||||
}
|
||||
|
||||
public bool Equals(double number, double otherNumber)
|
||||
{
|
||||
return RelativeDifference(number, otherNumber) <=
|
||||
_maximumRelativeDifference;
|
||||
}
|
||||
|
||||
public int GetHashCode(double number)
|
||||
{
|
||||
return number.GetHashCode();
|
||||
}
|
||||
|
||||
private static double RelativeDifference(double number,
|
||||
double otherNumber)
|
||||
{
|
||||
return AbsoluteDifference(number, otherNumber) /
|
||||
Norm(number, otherNumber);
|
||||
}
|
||||
|
||||
private static double AbsoluteDifference(double number,
|
||||
double otherNumber)
|
||||
{
|
||||
return Math.Abs(number - otherNumber);
|
||||
}
|
||||
|
||||
private static double Norm(double number, double otherNumber)
|
||||
{
|
||||
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
|
||||
class Program {
|
||||
|
||||
static Decimal DecSqRoot(Decimal v) {
|
||||
Decimal r = (Decimal)Math.Sqrt((double)v), t = 0, d = 0, ld = 1;
|
||||
while (ld != d) { t = v / r; r = (r + t) / 2;
|
||||
ld = d; d = t - r; } return t; }
|
||||
|
||||
static Decimal CalcAGM(Decimal a, Decimal b) {
|
||||
Decimal c, d = 0, ld = 1; while (ld != d) { ld = d; c = a;
|
||||
d = (a = (a + b) / 2) - (b = DecSqRoot(c * b)); } return b; }
|
||||
|
||||
static void Main(string[] args) {
|
||||
Console.WriteLine(CalcAGM(1M, DecSqRoot(0.5M)));
|
||||
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
using static System.Math;
|
||||
using static System.Console;
|
||||
using BI = System.Numerics.BigInteger;
|
||||
|
||||
class Program {
|
||||
|
||||
static BI BIP(char leadDig, int numDigs) { // makes big constant
|
||||
return BI.Parse(leadDig + new string('0', numDigs)); }
|
||||
|
||||
static BI IntSqRoot(BI v, BI res) { // res is the initial guess
|
||||
BI term = 0, d = 0, dl = 1; while (dl != d) { term = v / res; res = (res + term) >> 1;
|
||||
dl = d; d = term - res; } return term; }
|
||||
|
||||
static BI CalcByAGM(int digs) { // note: a and b are hard-coded for this RC task
|
||||
BI a = BIP('1', digs), // value of 1, extended to required number of digits
|
||||
b = BI.Parse(string.Format("{0:0.00000000000000000}", Sqrt(0.5)).Substring(2) +
|
||||
new string('0', digs - 17)), // initial guess for square root of 0.5
|
||||
c, // temporary variable to swap a and b
|
||||
diff = 0, ldiff = 1; // difference of a and b, last difference
|
||||
b = IntSqRoot(BIP('5', (digs << 1) - 1), b); // value of square root of 0.5
|
||||
while (ldiff != diff) { ldiff = diff; c = a; a = (a + b) >> 1;
|
||||
diff = a - (b = IntSqRoot(c * b, a)); } return b; }
|
||||
|
||||
static void Main(string[] args) {
|
||||
int digits = 25000; if (args.Length > 0) {
|
||||
int.TryParse(args[0], out digits);
|
||||
if (digits < 1 || digits > 999999) digits = 25000; }
|
||||
WriteLine("0.{0}", CalcByAGM(digits));
|
||||
if (System.Diagnostics.Debugger.IsAttached) ReadKey(); }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue