Skip to content

Instantly share code, notes, and snippets.

@KoStard
Created April 20, 2018 14:24
Show Gist options
  • Save KoStard/5c88cbce89cd2586e0679bdc0d8b4423 to your computer and use it in GitHub Desktop.
Save KoStard/5c88cbce89cd2586e0679bdc0d8b4423 to your computer and use it in GitHub Desktop.
Task 87 by UniLecs Telegram
function task87(N){
let mem = [];
let sum = 0;
for (j = 0; j < N; j++ ){
sum += 1;
if (j>1){
sum+=mem[j-2];
}
mem.push(sum);
}
return mem[mem.length-1];
} // The solution ends here.
function test(num){ // for test give any number, and this will print all results for [1:num]
if (!num) {
num = 10;
}
for (i=1;i<=num;i++){
console.log((i) + ": " + (task87(i)));
/*
we are adding one by one to the row, calculating the sum (how many variants can be with that number),
and saving it to the mem array.
So, how will we calculate the sum?
1 - First of all, every person can be chosen separately,
so when we are adding one person, the sum gets bigger by one.
2 - After that, if there is more than 2 persons, each person can
be chosen in the pair with other persons, separated by one.
*.*, but as we know, we already saved the sum value for the
[j-2] element, which contains the 'information' about all
j-2 element, which whom the new element can create contacts.
But what about the [j-1] element? It can't make connections with
the new element. So what happens here. [j-1] element can make
connection with the [j-3] element (and others, but this is the one,
with whom the [j-2] couldn't make connection). But here is the point,
that our variable sum, is cumulative, so the new connections of [j-3]
are saved in the sum and used in the calculations of [j-2], the same
with [j-1], whose new connections are added to the sum, so they
affect the value of [j].
So, every time we add new element, the sum will increase with the
number of new combinations. If the new element connect to element,
that creates 10 combinations (10 combinations are started with that
element), then the sum will increase by 10, instead of 1. But here
is the case, that the number of combinations created by any element,
is involved in the creation of sum for newer element.
*/
}
}
/*
1: 1
2: 2
3: 4
4: 7
5: 12
6: 20
7: 33
8: 54
9: 88
10: 143
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment