mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
big: Add factorial, have tests use hex strings.
This commit is contained in:
@@ -749,6 +749,30 @@ int_sqrmod :: proc(remainder, number, modulus: ^Int) -> (err: Error) {
|
||||
}
|
||||
sqrmod :: proc { int_sqrmod, };
|
||||
|
||||
|
||||
int_factorial :: proc(res: ^Int, n: DIGIT) -> (err: Error) {
|
||||
if n < 0 { return .Invalid_Argument; }
|
||||
|
||||
i := DIGIT(len(_factorial_table));
|
||||
if n < i {
|
||||
return set(res, _factorial_table[n]);
|
||||
}
|
||||
|
||||
a := &Int{};
|
||||
defer destroy(a);
|
||||
|
||||
if err = set( a, i - 1); err != .None { return err; }
|
||||
if err = set(res, _factorial_table[i - 1]); err != .None { return err; }
|
||||
|
||||
for {
|
||||
if err = mul(res, res, DIGIT(i)); err != .None || i == n { return err; }
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return .None;
|
||||
}
|
||||
factorial :: proc { int_factorial, };
|
||||
|
||||
/*
|
||||
==========================
|
||||
Low-level routines
|
||||
@@ -1237,4 +1261,69 @@ _int_div_digit :: proc(quotient, numerator: ^Int, denominator: DIGIT) -> (remain
|
||||
}
|
||||
destroy(q);
|
||||
return remainder, .None;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
when size_of(rawptr) == 8 {
|
||||
_factorial_table := [35]_WORD{
|
||||
/* f(00): */ 1,
|
||||
/* f(01): */ 1,
|
||||
/* f(02): */ 2,
|
||||
/* f(03): */ 6,
|
||||
/* f(04): */ 24,
|
||||
/* f(05): */ 120,
|
||||
/* f(06): */ 720,
|
||||
/* f(07): */ 5_040,
|
||||
/* f(08): */ 40_320,
|
||||
/* f(09): */ 362_880,
|
||||
/* f(10): */ 3_628_800,
|
||||
/* f(11): */ 39_916_800,
|
||||
/* f(12): */ 479_001_600,
|
||||
/* f(13): */ 6_227_020_800,
|
||||
/* f(14): */ 87_178_291_200,
|
||||
/* f(15): */ 1_307_674_368_000,
|
||||
/* f(16): */ 20_922_789_888_000,
|
||||
/* f(17): */ 355_687_428_096_000,
|
||||
/* f(18): */ 6_402_373_705_728_000,
|
||||
/* f(19): */ 121_645_100_408_832_000,
|
||||
/* f(20): */ 2_432_902_008_176_640_000,
|
||||
/* f(21): */ 51_090_942_171_709_440_000,
|
||||
/* f(22): */ 1_124_000_727_777_607_680_000,
|
||||
/* f(23): */ 25_852_016_738_884_976_640_000,
|
||||
/* f(24): */ 620_448_401_733_239_439_360_000,
|
||||
/* f(25): */ 15_511_210_043_330_985_984_000_000,
|
||||
/* f(26): */ 403_291_461_126_605_635_584_000_000,
|
||||
/* f(27): */ 10_888_869_450_418_352_160_768_000_000,
|
||||
/* f(28): */ 304_888_344_611_713_860_501_504_000_000,
|
||||
/* f(29): */ 8_841_761_993_739_701_954_543_616_000_000,
|
||||
/* f(30): */ 265_252_859_812_191_058_636_308_480_000_000,
|
||||
/* f(31): */ 8_222_838_654_177_922_817_725_562_880_000_000,
|
||||
/* f(32): */ 263_130_836_933_693_530_167_218_012_160_000_000,
|
||||
/* f(33): */ 8_683_317_618_811_886_495_518_194_401_280_000_000,
|
||||
/* f(34): */ 295_232_799_039_604_140_847_618_609_643_520_000_000,
|
||||
};
|
||||
} else {
|
||||
_factorial_table := [21]_WORD{
|
||||
/* f(00): */ 1,
|
||||
/* f(01): */ 1,
|
||||
/* f(02): */ 2,
|
||||
/* f(03): */ 6,
|
||||
/* f(04): */ 24,
|
||||
/* f(05): */ 120,
|
||||
/* f(06): */ 720,
|
||||
/* f(07): */ 5_040,
|
||||
/* f(08): */ 40_320,
|
||||
/* f(09): */ 362_880,
|
||||
/* f(10): */ 3_628_800,
|
||||
/* f(11): */ 39_916_800,
|
||||
/* f(12): */ 479_001_600,
|
||||
/* f(13): */ 6_227_020_800,
|
||||
/* f(14): */ 87_178_291_200,
|
||||
/* f(15): */ 1_307_674_368_000,
|
||||
/* f(16): */ 20_922_789_888_000,
|
||||
/* f(17): */ 355_687_428_096_000,
|
||||
/* f(18): */ 6_402_373_705_728_000,
|
||||
/* f(19): */ 121_645_100_408_832_000,
|
||||
/* f(20): */ 2_432_902_008_176_640_000,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user