Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix factorial implementation #17

Merged
merged 5 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use num_bigint::*;
fn naive_factorial(n: &BigUint) -> BigUint {
let mut acc = BigUint::from(1_usize);
let mut i = BigUint::from(2_usize);
while &i <= &n {
while &i <= n {
acc *= i.clone();
i += BigUint::from(1_usize);
}
Expand Down
51 changes: 42 additions & 9 deletions examples/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,49 @@ fn checked_naive_factorial(n: u128) -> Option<u128> {
Some(acc)
}

fn prime_range(
sieve: &Sieve,
lower_bound: u128,
upper_boud: u128,
) -> impl Iterator<Item = u128> + '_ {
sieve
.primes_from(lower_bound as usize)
.map(|m| m as u128)
.take_while(move |m| *m <= upper_boud)
}

const SMALL_SWING: [u128; 33] = [
1, 1, 1, 3, 3, 15, 5, 35, 35, 315, 63, 693, 231, 3003, 429, 6435, 6435, 109395, 12155, 230945,
46189, 969969, 88179, 2028117, 676039, 16900975, 1300075, 35102025, 5014575, 145422675,
9694845, 300540195, 300540195,
];

fn prime_swing(n: u128, sieve: &Sieve) -> Option<u128> {
let mut product = 1u128;
for prime in sieve.primes_from(2).take_while(|x| *x < n as usize) {
let mut p = 1u128;
if n < 33 {
return Some(SMALL_SWING[n as usize]);
}
let sqrt = ((n as f64).sqrt().floor()) as u128;
let mut product: u128 = 1;
for prime in prime_range(sieve, n / 2 + 1, n) {
product = product.checked_mul(prime)?;
}

for prime in prime_range(sieve, sqrt + 1, n / 3) {
if (n / prime) & 1 == 1 {
product = product.checked_mul(prime)?;
}
}

for prime in prime_range(sieve, 3, sqrt) {
let mut p = 1;
let mut q = n;
while q != 0 {
q /= prime as u128;
// q%2 == 1 if q is odd
if q % 2u128 == 1 {
p = p.checked_mul(prime as u128)?;
loop {
q /= prime;
if q == 0 {
break;
}
if q & 1 == 1 {
p *= prime;
}
}
if p > 1 {
Expand Down Expand Up @@ -48,7 +81,7 @@ fn main() -> std::io::Result<()> {
));

let sieve = Sieve::new(1_000);
file_content.push_str("pub const SMALL_PRIME_SWING: ");
file_content.push_str("pub const SMALL_ODD_SWING: ");
let mut n = 0u128;
let mut prime_swings = vec![];
while let Some(swing) = prime_swing(n, &sieve) {
Expand Down
254 changes: 127 additions & 127 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,134 +35,134 @@ pub const SMALL_FACTORIAL: [u128; 35] = [
8683317618811886495518194401280000000,
295232799039604140847618609643520000000,
];
pub const SMALL_PRIME_SWING: [u128; 129] = [
pub const SMALL_ODD_SWING: [u128; 129] = [
1,
1,
1,
2,
6,
6,
20,
20,
70,
630,
252,
252,
924,
924,
3432,
51480,
12870,
12870,
48620,
48620,
184756,
3879876,
705432,
705432,
2704156,
67603900,
10400600,
280816200,
40116600,
40116600,
155117520,
155117520,
601080390,
19835652870,
2333606220,
81676217700,
9075135300,
9075135300,
35345263800,
1378465288200,
137846528820,
137846528820,
538257874440,
538257874440,
2104098963720,
94684453367400,
8233430727600,
8233430727600,
32247603683100,
1580132580471900,
126410606437752,
6446940928325352,
495918532948104,
495918532948104,
1946939425648112,
107081668410646160,
7648690600760440,
435975364243345080,
30067266499541040,
30067266499541040,
118264581564861424,
118264581564861424,
465428353255261088,
29321986255081448544,
1832624140942590534,
119120569161268384710,
7219428434016265740,
7219428434016265740,
28453041475240576740,
1963259861791599795060,
112186277816662845432,
112186277816662845432,
442512540276836779204,
442512540276836779204,
1746130564335626209832,
130959792325171965737400,
6892620648693261354600,
530731789949381124304200,
27217014869199032015600,
27217014869199032015600,
107507208733336176461620,
8708083907400230293391220,
424784580848791721628840,
424784580848791721628840,
1678910486211891090247320,
142707391328010742671022200,
6637553085023755473070800,
577467118397066726157159600,
26248505381684851188961800,
26248505381684851188961800,
103827421287553411369671120,
9448295337167360434640071920,
410795449442059149332177040,
38203976798111500887892464720,
1625701140345170250548615520,
154441608332791173802118474400,
6435067013866298908421603100,
6435067013866298908421603100,
25477612258980856902730428600,
2522283613639104833370312431400,
100891344545564193334812497256,
100891344545564193334812497256,
399608854866744452032002440112,
399608854866744452032002440112,
1583065848125949175357548128136,
166221914053224663412542553454280,
6272525058612251449529907677520,
6272525058612251449529907677520,
24857784491537440929618523018320,
24857784491537440929618523018320,
98527218530093856775578873054432,
10936521256840418102089254909041952,
390590044887157789360330532465784,
390590044887157789360330532465784,
1548655265692941410446222812934512,
178095355554688262201315623487468880,
6141219157058215937976400809912720,
718522641375811264743238894759788240,
24356699707654619143838606602026720,
2898447265210899678116794185641179680,
96614908840363322603893139521372656,
11690403969683962035071069882086091376,
383291933432261050330199012527412832,
47144907812168109190614478540871778336,
1520803477811874490019821888415218656,
190100434726484311252477736051902332000,
6034934435761406706427864636568328000,
6034934435761406706427864636568328000,
23951146041928082866135587776380551750,
3,
3,
15,
5,
35,
35,
315,
63,
693,
231,
3003,
429,
6435,
6435,
109395,
12155,
230945,
46189,
969969,
88179,
2028117,
676039,
16900975,
1300075,
35102025,
5014575,
145422675,
9694845,
300540195,
300540195,
9917826435,
583401555,
20419054425,
2268783825,
83945001525,
4418157975,
172308161025,
34461632205,
1412926920405,
67282234305,
2893136075115,
263012370465,
11835556670925,
514589420475,
24185702762325,
8061900920775,
395033145117975,
15801325804719,
805867616040669,
61989816618513,
3285460280781189,
121683714103007,
6692604275665385,
956086325095055,
54496920530418135,
1879204156221315,
110873045217057585,
7391536347803839,
450883717216034179,
14544636039226909,
916312070471295267,
916312070471295267,
59560284580634192355,
1804857108504066435,
120925426269772451145,
7113260368810144185,
490814965447899948765,
14023284727082855679,
995653215622882753209,
110628135069209194801,
8075853860052271220473,
218266320541953276229,
16369974040646495717175,
861577581086657669325,
66341473743672640538025,
1701063429324939500975,
134384010916670220577025,
26876802183334044115405,
2177020976850057573347805,
53098072606098965203605,
4407140026306214111899215,
209863810776486386280915,
17838423916001342833877775,
414847067813984717066925,
36091694899816670384822475,
3281063172710606398620225,
292014622371243969477200025,
6489213830472088210604445,
590518458572960027165004495,
25674715590128696833261065,
2387748549881968805493279045,
50803160635786570329644235,
4826300260399724181316202325,
1608766753466574727105400775,
156050375086257748529223875175,
3184701532372607112841303575,
315285451704888104171289053925,
12611418068195524166851562157,
1273753224887747940852007777857,
24975553429171528252000152507,
2572482003204667409956015708221,
197883231015743646919693516017,
20777739256653082926567819181785,
392032816163265715595619229845,
41947511329469431568731257593415,
1553611530721090058101157688645,
169343656848598816333026188062305,
3078975579065433024236839782951,
341766289276263065690289215907561,
48823755610894723670041316558223,
5517084384031103774714668771079199,
96790954105808838152888925808407,
11130959722168016387582226467966805,
383826197316138496123525050619545,
44907665085988204046452430922486765,
761146865864206848244956456313335,
90576477037840614941149818301286865,
6038431802522707662743321220085791,
730650248105247627191941867630380711,
11977872919758157822818719141481651,
1473278369130253412206702454402243073,
47525108681621077813119434012975583,
5940638585202634726639929251621947875,
94295850558771979787935384946380125,
11975573020964041433067793888190275875,
11975573020964041433067793888190275875,
];
Loading