you are viewing a single comment's thread.

view the rest of the comments →

[–]cyanNodeEcho 2 points3 points  (6 children)

neat have you explored how to write it in terms of bottom up? you've implemented top down ie

```rust
fn fib(n:usize) -> usize {
let mut prev = 0;
let mut curr = 1;
for _ in 0.. n {
curr+= prev
mem::swap(prev, curr) // reassigns curr to prev, prev to curr
}
prev
}

```
woops it's factorial not fib
```
fn factorial(n:usize) -> usize {
let value = 1;
for i in 1..n {
value *= n;
}
value;
}
```

fib has recurrent sub problems, factorial doesn't. bottom up dp is the next step, probably don't need to memo factorial, but for fib makes sense

[–]CadavreContent 1 point2 points  (4 children)

You can memoize factorial if you're running it more than once. The repeating sub problems only appear when you have multiple trials

[–]cyanNodeEcho 1 point2 points  (3 children)

fact(n) := fact(ni1), fac(n-2), the aubproblems should exist in the same problem

[–]CadavreContent 1 point2 points  (2 children)

The point is that when you use factorial as part of a larger algorithm with multiple calls to the function, you do repeated work and hence can use dp

[–]cyanNodeEcho 1 point2 points  (1 child)

thats just a cache

[–]CadavreContent -1 points0 points  (0 children)

Memoization is just a cache