@@ -242,6 +242,60 @@ class Test_Basic_Math: XCTestCase {
242242 }
243243 }
244244
245+ func test_Multiplication_large_roundtrip( ) {
246+ // (a*b)/a == b for large operands
247+ let a = BIntMath . randomBInt ( bits: 10_000 )
248+ let b = BIntMath . randomBInt ( bits: 10_000 )
249+ let product = a * b
250+ XCTAssertEqual ( product / a, b)
251+ XCTAssertEqual ( product / b, a)
252+ }
253+
254+ func test_Multiplication_commutative_large( ) {
255+ let a = BIntMath . randomBInt ( bits: 50_000 )
256+ let b = BIntMath . randomBInt ( bits: 50_000 )
257+ XCTAssertEqual ( a * b, b * a)
258+ }
259+
260+ func test_Multiplication_distributive( ) {
261+ let a = BIntMath . randomBInt ( bits: 5_000 )
262+ let b = BIntMath . randomBInt ( bits: 5_000 )
263+ let c = BIntMath . randomBInt ( bits: 5_000 )
264+ XCTAssertEqual ( a * ( b + c) , a * b + a * c)
265+ }
266+
267+ func test_Multiplication_known_square( ) {
268+ let base = BInt ( 10 ) ** 1000
269+ XCTAssertEqual ( base * base, BInt ( 10 ) ** 2000 )
270+ }
271+
272+ func test_Factorial_consistency( ) {
273+ // n! == n * (n-1)!
274+ XCTAssertEqual ( BInt ( 1000 ) . factorial ( ) , BInt ( 1000 ) * BInt( 999 ) . factorial ( ) )
275+ XCTAssertEqual ( BInt ( 10000 ) . factorial ( ) , BInt ( 10000 ) * BInt( 9999 ) . factorial ( ) )
276+ }
277+
278+ func test_Factorial_trailing_zeros( ) {
279+ // Trailing zeros in n! = sum of floor(n/5^k)
280+ func expectedTrailingZeros( _ n: Int ) -> Int {
281+ var count = 0 ; var p = 5
282+ while p <= n { count += n / p; p *= 5 }
283+ return count
284+ }
285+ for n in [ 100 , 1000 , 5000 , 10000 ] {
286+ let str = BInt ( n) . factorial ( ) . description
287+ let zeros = str. reversed ( ) . prefix ( while: { $0 == " 0 " } ) . count
288+ XCTAssertEqual ( zeros, expectedTrailingZeros ( n) , " \( n) ! trailing zeros " )
289+ }
290+ }
291+
292+ func test_Exponentiation_10_pow_large( ) {
293+ let str = ( BInt ( 10 ) ** 10_000 ) . description
294+ XCTAssertEqual ( str. count, 10_001 )
295+ XCTAssertEqual ( str. first, " 1 " )
296+ XCTAssert ( str. dropFirst ( ) . allSatisfy ( { $0 == " 0 " } ) )
297+ }
298+
245299 func test_Power( ) {
246300 // Reference issue #41
247301 let TWO : BInt = 2
0 commit comments