It would be straightforward to multiply Java (or C#) integers (int) together to compute n! except that an int For the 20th term, k = 20, we already need to compute 80! These numbers rapidly get very big! The highest my pocket calculator can This looks simple enough, until you realise the problem caused by the factorials (! is factorial or 'shriek'). We may wish to add tens or even hundreds of these terms together. This series is a good approximation for computation, since it converges very rapidly on the true The more decimal places of accuracy we require, the more terms we must Note that this is a series approximation - we calculate a series of terms for k = 0, k = 1, k = 2. Private static BigDecimal Sqrt_2(int terms)īigDecimal buffer = new BigDecimal īigDecimal initial = new BigDecimal("1.4") īuffer = buffer.divide(BigDecimal.valueOf(2), terms*5,īigDecimal.ROUND_HALF_UP).add(BigDecimal.valueOf(1).divide(buffer, terms*5, Throw new IllegalArgumentException("Negative factorial not defined") īigInteger factorial = new BigInteger("1") Private static BigInteger LargeFactorial(int n) Calculates factorials of large values using BigInteger Pi = unity.divide(inverse_pi, decimalPlaces, BigDecimal.ROUND_HALF_UP) Inverse_pi = coefficient.multiply(sigma) Final algebra multiply sum by coefficient and invert ("Enter a positive integer or 0 to exit:") * Calculates pi to an arbitrary number od decimal places
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |