# Programming Kotlin: dynamic programming example speed up (Kindle Loc 9737)

**URL:** <https://forum.devtalk.com/t/programming-kotlin-dynamic-programming-example-speed-up-kindle-loc-9737/62069>\
**Category:** PragProg Customers\
**Tags:** book-programming-kotlin, suggestion\
**Created:** [1 July 2022 16:25 UTC](https://forum.devtalk.com/t/programming-kotlin-dynamic-programming-example-speed-up-kindle-loc-9737/62069 "2022-07-01T16:25:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrzysztofB](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@KrzysztofB](https://forum.devtalk.com/u/KrzysztofB)\
**Post date:** [1 July 2022 16:25 UTC](https://forum.devtalk.com/t/programming-kotlin-dynamic-programming-example-speed-up-kindle-loc-9737/62069/1 "2022-07-01T16:25:28Z")

</div>

### @venkats

There is an example in **Applying Memoization to Dynamic Programming** chapter.  
recursion/cutrod.kts, fragment below

```auto
    (1 until length).fold(priceAtLength){
        max, cutLength ->
        val cutPrice = maxPrice(cutLength) + maxPrice(length - cutLength)
        Math.max(cutPrice, max)
    }

```

cutPrice calculation effort is doubled here because of addition symmetry:  
maxPrice(1) + maxPrice(6) is the same as maxPrice(6) + maxPrice(1)

The same result can be achieved by reducing computations to

```auto
    (1 until length/2).fold(priceAtLength){ ...

```

Regards  
Krzysztof Bardzinski
