<commit_before>// iterative
// function factorialIterative(num) {
//   var factorial = 1;
//   for (var i = num; i > 0; i--) {
//     factorial *= i;
//   }
//   return factorial;
// }

function factorialIterative(num) {
  var factorial = 1;
  while (num > 0) {
    factorial *= num;
    num --;
  }
  return factorial;
}


// recursive
function factorialRecursive(num) {
  // base cases?
  if (num === 1 || num === 0) {
    return 1;
  }
  // console.log(num + " " + factorialRecursive(num - 1));
  // recursive factorial
  return num * factorialRecursive(num - 1);
}
<commit_msg>Add memoization to factorial<commit_after>/* 
  given integer n, return factorial for n
  5 -> 120
  4 -> 24
  3 -> 6
  2 -> 2
  1 -> 1
  0 -> 0
*/

// function factorialIterative(num) {
//   var factorial = 1;
//   for (var i = num; i > 0; i--) {
//     factorial *= i;
//   }
//   return factorial;
// }

function factorialIterative(num) {
  var factorial = 1;
  while (num > 0) {
    factorial *= num;
    num --;
  }
  return factorial;
}

function factorialRecursive(num) {
  // base case
  if (num <= 2) {
    return num;
  }
  // console.log(num + " " + factorialRecursive(num - 1));
  return num * factorialRecursive(num - 1);
}

let memo = [];

function factorialMemoized(num) {
    if (num <= 2) {
      return num;
    }
    
    if (!(num in memo)) {
      memo[num] = factorialMemoized(num-1) * num;
    }

    // console.log(memo);
    return memo[num];
}

{'fraction_non_alphanumeric': 0.16609235010337697, 'fraction_numerical': 0.022742935906271536, 'mean_word_length': 2.1842105263157894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6497020', 'n_tokens_mistral': 571, 'n_tokens_neox': 541, 'n_words': 139}