Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
46
Task/Partition-function-P/Rust/partition-function-p.rs
Normal file
46
Task/Partition-function-P/Rust/partition-function-p.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// [dependencies]
|
||||
// rug = "1.11"
|
||||
|
||||
use rug::Integer;
|
||||
|
||||
fn partitions(n: usize) -> Integer {
|
||||
let mut p = Vec::with_capacity(n + 1);
|
||||
p.push(Integer::from(1));
|
||||
for i in 1..=n {
|
||||
let mut num = Integer::from(0);
|
||||
let mut k = 1;
|
||||
loop {
|
||||
let mut j = (k * (3 * k - 1)) / 2;
|
||||
if j > i {
|
||||
break;
|
||||
}
|
||||
if (k & 1) == 1 {
|
||||
num += &p[i - j];
|
||||
} else {
|
||||
num -= &p[i - j];
|
||||
}
|
||||
j += k;
|
||||
if j > i {
|
||||
break;
|
||||
}
|
||||
if (k & 1) == 1 {
|
||||
num += &p[i - j];
|
||||
} else {
|
||||
num -= &p[i - j];
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
p.push(num);
|
||||
}
|
||||
p[n].clone()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
use std::time::Instant;
|
||||
let n = 6666;
|
||||
let now = Instant::now();
|
||||
let result = partitions(n);
|
||||
let time = now.elapsed();
|
||||
println!("P({}) = {}", n, result);
|
||||
println!("elapsed time: {} microseconds", time.as_micros());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue