Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
function countFriendsPairings ( n ) { let a = 1 , b = 2 , c = 0 ; if ( n <= 2 ) { return n ; } for ( let i = 3 ; i <= n ; i ++ ) { c = b + ( i - 1 ) * a ; a = b ; b = c ; } return c ; } let n = 4 ; document . write ( countFriendsPairings ( n ) ) ;