Page 152 - CSharp/C#
P. 152

Chapter 18: BigInteger




        Remarks



        When To Use



        BigInteger objects are by their very nature very heavy on RAM. Consequently, they should only be
        used when absolutely necessary, ie for numbers on a truly astronomical scale.

        Further to this, all arithmetic operations on these objects are an order of magnitude slower than
        their primitive counterparts, this problem gets further compounded as the number grows as they
        are not of a fixed size. It is therefore feasibly possible for a rogue BigInteger to cause a crash by
        consuming all of the available RAM.


        Alternatives



        If speed is imperative to your solution it may be more efficient to implement this functionality
        yourself using a class wrapping a Byte[] and overloading the necessary operators yourself.
        However, this does require a significant amount of extra effort.


        Examples



        Calculate the First 1,000-Digit Fibonacci Number


        Include using System.Numerics and add a reference to System.Numerics to the project.


         using System;
         using System.Numerics;

         namespace Euler_25
         {
             class Program
             {
                 static void Main(string[] args)
                 {
                     BigInteger l1 = 1;
                     BigInteger l2 = 1;
                     BigInteger current = l1 + l2;
                     while (current.ToString().Length < 1000)
                     {
                         l2 = l1;
                         l1 = current;
                         current = l1 + l2;
                     }
                     Console.WriteLine(current);
                 }
             }
         }





        https://riptutorial.com/                                                                               98
   147   148   149   150   151   152   153   154   155   156   157