RosettaCodeData/Task/Arithmetic-geometric-mean/C-sharp/arithmetic-geometric-mean-2.cs
2023-07-01 13:44:08 -04:00

18 lines
622 B
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}