Skip to content

Instantly share code, notes, and snippets.

@Beraliv
Last active February 9, 2018 06:00
Show Gist options
  • Save Beraliv/7573d9240b8a9c77cb61783f786cc3ea to your computer and use it in GitHub Desktop.
Save Beraliv/7573d9240b8a9c77cb61783f786cc3ea to your computer and use it in GitHub Desktop.
Task 68. UniLecs. Count of n digit numbers using only 4 and 7, where cannot be 3 same digits in a row.
/*
* It's not technically correct as there are possible numbers
* that have more than one subsequence of exact digits: 7774777
* but here it's considered only one possible subsequence of exact digits
*/
const answer = n => {
const allPossibleCount = n === 0 ? 0 : Math.pow(2, n);
const threeOrMoreSameDigitInARowCount = Array(Math.max(n - (3 + index) + 1, 0))
.fill(0)
.map((_, index) => (n - 3 + index) * 2)
.reduce((a, b) => a + b, 0);
return allPossibleCount - threeOrMoreSameDigitInARowCount;
};
for (let i = 0; i <= 30; i++) {
console.log(answer(i));
}
// 0
// 2
// 4
// 6
// 10
// 20
// 44
// 98
// 214
// 456
// 952
// 1958
// 3986
// 8060
// 16228
// 32586
// 65326
// 130832
// 261872
// 523982
// 1048234
// 2096772
// 4193884
// 8388146
// 16776710
// 33553880
// 67108264
// 134217078
// 268434754
// 536870156
// 1073741012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment