1- namespace MattSourceGenHelpers . Examples ;
1+ using System . Globalization ;
2+ using ExtendedNumerics ;
23
3- public class SlowMath
4- {
5- // Pi digits string (0-indexed: position 0 = '3', position 1 = '1', position 2 = '4', ...)
6- private static readonly string PiDigits =
7- "31415926535897932384626433832795028841971693993751" +
8- "05820974944592307816406286208998628034825342117067" +
9- "98214808651328230664709384460955058223172535940812" +
10- "84811174502841027019385211055596446229489549303819" +
11- "64428810975665933446128475648233786783165271201909" +
12- "14564856692346034861045432664821339360726024914127" +
13- "37245870066063155881748815209209628292540917153643" +
14- "67892590360011330530548820466521384146951941511609" +
15- "43305727036575959195309218611738193261179310511854" +
16- "80744623799627495673518857527248912279381830119491" +
17- "29833673362440656643086021394946395224737190702179" +
18- "86094370277053921717629317675238467481846766940513" +
19- "20005681271452635608277857713427577896091736371787" +
20- "21468440901224953430146549585371050792279689258923" +
21- "54201995611212902196086403441815981362977477130996" +
22- "05187072113499999983729780499510597317328160963185" +
23- "95024459455346908302642522308253344685035261931188" +
24- "17101000313783875288658753320838142061717766914730" +
25- "35982534904287554687311595628638823537875937519577" +
26- "81857780532171226806613001927876611195909216420198" ;
4+ namespace MattSourceGenHelpers . Examples ;
275
6+ public static class SlowMath
7+ {
288 public static int CalculatePiDecimal ( int decimalNumber )
299 {
30- if ( decimalNumber >= 0 && decimalNumber < PiDigits . Length )
31- return PiDigits [ decimalNumber ] - '0' ;
32- return 0 ;
10+ if ( decimalNumber < 0 ) throw new ArgumentOutOfRangeException ( nameof ( decimalNumber ) , "Decimal number must be non-negative." ) ;
11+ if ( decimalNumber == 0 ) return 3 ;
12+
13+ BigDecimal pi = BigDecimal . ApproximatePi ( decimalNumber + 1 ) ;
14+
15+ if ( pi . DecimalPlaces < decimalNumber ) throw new ArgumentException ( $ "Failed to calculate pi to { decimalNumber } decimal places.") ;
16+
17+ string piString = pi . ToString ( CultureInfo . InvariantCulture ) ;
18+ return int . Parse ( piString . Substring ( decimalNumber + 1 , 1 ) ) ;
3319 }
3420}
0 commit comments