title: Friends Pairing Problem 2
signature: long long friendsPairingProblem2(int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(long long actual, long long expected){
      return actual == expected;
  }
  
  string driver(int n, long long expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver(24, 17492190577600); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver(1, 1); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver(29, 101990226254706560); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver(3, 4); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver(14, 2390480); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def friends_pairing_problem_2(n):
      (a, b, c) = (1, 2, 0)
      if (n <= 2):
          return n
      for i in range(3, (n + 1)):
          c = (b + ((i - 1) * a))
          a = b
          b = c
      return c
source_cpp: int friends_pairing_problem_2(int n) {
    int a = 1, b = 2, c = 0;
    if ( n <= 2 ) {
      return n;
    }
    for ( int i = 3;
    i <= n;
    i ++ ) {
      c = b + ( i - 1 ) * a;
      a = b;
      b = c;
    }
    return c;
  }