Ejemplo n.º 1
0
		public static BigInt Gcd(BigInt x, BigInt y) {
			
			cnt++;
			
			if( y == 0 ) return x;
			if( y == 1 ) return y;
			
			return Gcd( y, (BigInt)(x % y) );
			
		}
Ejemplo n.º 2
0
        public BigRational(String num, int nBase)
        {
            // the algo to convert from a float into a rational isn't that complicated:

            // 1. Get the non-integer part of the number
            // 2. Assuming it's base 10, then:
            // 2.1 ...the numerator is the non-integer part converted to an integer, and the
            // 2.2 ...the denominator is an integer equal to 10^(length of non-integer part)
            // 2.3 ...then just normalise it
            // 3. Then you need to add the integer part back

            // HACK: String processing to get numbers: baaaaad

            int radixIdx = num.IndexOf('.');

            if( radixIdx == -1 ) {

                _num = new BigInt( num );
                _den = new BigInt(1);
                return;

            }

            String intPart = num.Substring(0, radixIdx);
            String fltPart = num.Substring( radixIdx );

            BigInt tempNumerator   = new BigInt( fltPart );
            BigInt tempDenominator = new BigInt( (int)Math.Pow( nBase, fltPart.Length ) );

            Normalise( tempNumerator, tempDenominator, out tempNumerator, out tempDenominator );

            // then add back the integer part

            BigRational intPortion = new BigRational( intPart );
            BigRational fltPortion = new BigRational( tempNumerator, tempDenominator );

            BigRational result = (BigRational)(intPortion + fltPortion);

            this._num = result._num;
            this._den = result._den;
        }
Ejemplo n.º 3
0
		public static BigInt Factorial(BigInt num) {
			// HACK: Is there a more efficient implementation?
			// I know there is a way to cache and use earlier results, but not much more
			
			// also, note this function fails if num is non-integer. This should be the Gamma function instead
			
			BigInt one = (BigInt)BigIntFactory.Instance.Unity;
			
			if(num.IsZero) return one;
			if(num < num.Factory.Zero) throw new ArgumentException("Argument must be greater than or equal to zero", "num");
			
			return (BigInt)( num * Factorial( (BigInt)(num - one) ) );
		}
Ejemplo n.º 4
0
 private BigInt(BigInt copyThis)
 {
     _v = new IntX( copyThis._v );
     _v.Normalize();
 }
Ejemplo n.º 5
0
 private BigRational(BigInt num, BigInt den)
 {
     _num = num;
     _den = den;
 }
Ejemplo n.º 6
0
        private static void Normalise(BigInt oldNum, BigInt oldDen, out BigInt newNum, out BigInt newDen)
        {
            // TODO: Maybe also ensure that the denominator is positive, only the numerator can be negative

            // find the GCD of oldNum and oldDen, then apply it to find and return newNum and newDen

            BigInt gcd = (BigInt)BigMath.Gcd( oldNum, oldDen );

            newNum = (BigInt)(oldNum / gcd);
            newDen = (BigInt)(oldDen / gcd);
        }
Ejemplo n.º 7
0
 public BigRational(BigInt num)
 {
     // an integer is represented by a fraction integer/1
     _num = num;
     _den = new BigInt(1);
 }