I'm performing a statistical analysis to determine the possibility of whether a larger transaction has been hidden by breaking it into smaller transactions within a certain time frame. What I'm doing is breaking the larger data set into smaller subsets (arrays of 12, at the moment) and then running a series of loops over each subset to determine whether any combination of the elements add up to within a target range.
Here's my current code:
amounts_matrix = [1380.54,9583.33,37993.04,3240.96...]
matrix_amounts = amounts_matrix.length
total_permutations = 0;
total_hits = 0;
target_range = 1
target = 130000
low_threshold = target - target_range
high_threshold = target + target_range
entries = []
range = 12
for(x = 0; x< matrix_amounts-(range-1); x++){
amounts = amounts_matrix.slice(x, x+range)
total_amounts = range
for(i = 0; i< total_amounts; i++){
entries.push(amounts[i])
totalcheck(entries)
entries = []
}
for(i = 0; i< total_amounts; i++){
for(j = i+1; j< total_amounts; j++){
entries.push(amounts[i])
entries.push(amounts[j])
totalcheck(entries)
entries = []
}
}
...
for(i = 0; i< total_amounts; i++){
for(j = i+1; j< total_amounts; j++){
for(k = j+1; k< total_amounts; k++){
for(l = k+1; l< total_amounts; l++){
for(m = l+1; m< total_amounts; m++){
for(n = m+1; n< total_amounts; n++){
for(o = n+1; o< total_amounts; o++){
for(p = o+1; p< total_amounts;p++){
for(q = p+1; q< total_amounts;q++){
for(r = q+1; r< total_amounts;r++){
for(s = r+1; s< total_amounts;s++){
for(t = s+1; t< total_amounts;t++){
entries.push(amounts[i])
entries.push(amounts[j])
entries.push(amounts[k])
entries.push(amounts[l])
entries.push(amounts[m])
entries.push(amounts[n])
entries.push(amounts[o])
entries.push(amounts[p])
entries.push(amounts[q])
entries.push(amounts[r])
entries.push(amounts[s])
entries.push(amounts[t])
totalcheck(entries)
entries = []
}
}
}
}
}
}
}
}
}
}
}
}
}
function totalcheck(array){
total_permutations += 1;
sum_amount = 0
for(z = 0; z < array.length; z++){
sum_amount += array[z]
}
if(sum_amount > low_threshold && sum_amount < high_threshold){
total_hits += 1
console.log(array)
console.log(sum_amount.toFixed(2))
console.log("---------------")
}
}
console.log("overall total hits = " + total_hits)
console.log("overall total permutations = " + total_permutations)
I'm pretty embarrassed by how extensive those for loops get, and I'd like to generalize it with a function where I can just tell it to run X loops rather than having to build them out like this. The permutation functions I've found aren't really viable for me because they all build arrays full of the total possibilities; in mine I want to check against the target as I go to avoid having gigantic arrays and running into memory issues. How do I build a recursive loop that will do this?