# Giving infinite as result when using matrix chain multiplication to find the efficient cost

I am trying to understand Matrix Chain Multiplication. Particularly the problem `Given a sequence of matrices, find the most efficient way to multiply these matrices together.`

I tried the following but it prints some infinite value for the result. What is it that I am doing incorrectly?

``````const p = [1, 2, 3, 4, 3];

function mcm(m, i, j) {
if (i >= j) return 0;
else {
let ans = Number.MAX_VALUE;
let temp;
for (let k = i; k < j - 1; k++) {
temp = mcm(m, i, k) + mcm(m, k + 1, j) + m[i - 1] * m[k] * m[j];
if (ans > temp) ans = temp;
}
return ans;
}
}

console.log(mcm(p, 1, p.length - 1));``````

### 133 thoughts on “Giving infinite as result when using matrix chain multiplication to find the efficient cost”

1. I have been browsing online more than 4 hours today, yet I never found
any interesting article like yours. It is pretty worth enough for me.

In my view, if all website owners and bloggers made good content as you did, the
web will be a lot more useful than ever before.

2. It’s perfect time to make some plans for the future and it’s
time to be happy. I’ve read this post and if I could
I wish to suggest you few interesting things or advice.

3. Good answers in return of this question with firm arguments and describing all regarding that.

4. I have been surfing online more than three hours today,
yet I never found any interesting article like
yours. It’s pretty worth enough for me. In my opinion, if all
web owners and bloggers made good content as you did, the net will be
much more useful than ever before.

5. Link exchange is nothing else but it is simply placing the other person’s web site link on your page at appropriate place and other person will also do same in favor of you.

6. generic cialis reviews is cialis warnings for tadalafil

7. Hi! I could have sworn I’ve visited your blog before but after
looking at some of the articles I realized it’s new to
me. Anyways, I’m definitely pleased I found it and I’ll be bookmarking it and checking back regularly!

8. I’ve been browsing online more than 4 hours today, yet I never found any interesting article like yours.
It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content
as you did, the internet will be much more useful than ever before.

Do you’ve any? Kindly let me recognise so that I could subscribe.
Thanks.

10. Thanks for your article. It is extremely unfortunate that over the last several years, the travel industry has already been able to to fight terrorism, SARS, tsunamis, bird flu virus, swine flu, plus the first ever entire global economic collapse. Through everthing the industry has really proven to be solid, resilient and also dynamic, obtaining new strategies to deal with trouble. There are generally fresh complications and chance to which the field must once more adapt and reply.

11. I have observed that of all sorts of insurance, health care insurance is the most controversial because of the clash between the insurance plan company’s necessity to remain afloat and the buyer’s need to have insurance coverage. Insurance companies’ revenue on overall health plans are incredibly low, consequently some firms struggle to make money. Thanks for the tips you write about through this blog.

12. Hi there would you mind letting me know which webhost you’re utilizing?
say this blog loads a lot faster then most. Can you suggest a good hosting provider at a honest price?
Many thanks, I appreciate it!

13. It’s appropriate time to make some plans for the future and it’s time to
be happy. I’ve read this post and if I could I want to suggest
you few interesting things or advice. Maybe you can write next articles referring to
it!

14. Now I am going away to do my breakfast, once having my breakfast coming yet again to read further news.