fn partition_memoize(mut pc: &mut PartitionCache, n: usize) -> usize {
    // If the value is cached, return immediately
    if n < pc.capacity() && pc[n] != 0 {
        pc[n]
    } else {
        let mut sum = 0_i64;
        for k in 1..n {
            let sign = if k % 2 == 1 { 1 } else { -1 };

            println!("{} {}", n, k);
            let x = partition_memoize(&mut pc, n - k*(3*k - 1)/2);
            let y = partition_memoize(&mut pc, n - k*(3*k + 1)/2);

            println!("{}: {} {}", n, x, y);
            sum += sign * (x + y) as i64;
        }

        assert!(sum >= 0);

        // Add value to cache
        if n < pc.capacity() {
            pc[n] = sum as usize;
        }

        sum as usize
    }
}
/// Compute the partition function of a specified number using the
/// given cache of values.
Compute the partition function of a specified number using the
given cache of values.
Compute the partition function of a specified number using the given cache of values .
fn partition_memoize(mut pc: &mut PartitionCache, n: usize) -> usize {
    // If the value is cached, return immediately
    if n < pc.capacity() && pc[n] != 0 {
        pc[n]
    } else {
        let mut sum = 0_i64;
        for k in 1..n {
            let sign = if k % 2 == 1 { 1 } else { -1 };
            println!("{} {}", n, k);
            let x = partition_memoize(&mut pc, n - k*(3*k - 1)/2);
            let y = partition_memoize(&mut pc, n - k*(3*k + 1)/2);
            println!("{}: {} {}", n, x, y);
            sum += sign * (x + y) as i64;
        }
        assert!(sum >= 0);
        // Add value to cache
        if n < pc.capacity() {
            pc[n] = sum as usize;
        }
        sum as usize
    }
}
fn partition_memoize ( mut pc : & mut PartitionCache , n : usize ) -> usize { // If the value is cached, return immediately if n < pc . capacity ( ) && pc [ n ] != 0 { pc [ n ] } else { let mut sum = 0_i64 ; for k in 1 .. n { let sign = if k % 2 == 1 { 1 } else { - 1 } ; println ! ( " " n k ) ; let x = partition_memoize ( & mut pc , n - k * ( 3 * k - 1 ) / 2 ) ; let y = partition_memoize ( & mut pc , n - k * ( 3 * k + 1 ) / 2 ) ; println ! ( " " n x y ) ; sum += sign * ( x + y ) as i64 ; } assert ! ( sum 0 ) ; // Add value to cache if n < pc . capacity ( ) { pc [ n ] = sum as usize ; } sum as usize } }
Compute the partition function of a specified number using the
given cache of values.
Compute the partition function of a specified number using the given cache of values .
pc
&mut PartitionCache
n
usize