Viral,
<script>
function fn(b, d) {
for (var a = [1,1,1,1,1,1,1,1,1].slice(b), c = []; a[0] != b;) a.reduce(function(a, b) {
return +a + +b
}, 0) < b ? a.push(1) : (a.every(function(a) {
return +a <= d
}) && c.push(a.slice()), a.pop(), a[a.length - 1]++);
c.push([b])
return c.sort(function(a, b) {
return +a.join('') - +b.join('')
})
};
document.write(fn(5,5).join('<br>')+ "<br>Всего: "+ fn(5,5).length);
</script>