title: Algorithm to find the sum of all contiguous substrings of a set
body: <p>Im trying to write a time efficient algorithm that can find that sum of all of the possible <strong>contiguous</strong> substring of an array (preserving order and combinations can be any length)</p>
  
  <p>For example: </p>
  
  <pre><code>[1,2,3,4] -&gt; 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234 = 1670
  </code></pre>
  
  <p>It is also important to note that the array can repeat itself multiple times</p>
  
  <p>My best attempt so far is probably this: (n is the set of numbers)</p>
  
  <pre><code>k = 3 // number of times the array repeats
  length = len(n)
  total = 0
  
  for i in range(0, length*k):
      for exp in range(0, length*k-i): 
      //iterate though all of the possible powers of ten a certain number could be in
      // ie. all the different places that number could be in for all combinations 
  
          total += ((n[i % length] * 10**exp) * (i + 1))
          // ^ turns number from standard from into int. The i + 1 account for
          // the fact the number could be in the same position in more than one combination
  
  return total
  </code></pre>
  
  <p>However, this algorithm has to run for an array with over 10^20 numbers in it, so I am looking for a faster algorithm.</p>
  
  <p>Note that all numbers are single digits and the digits can repeat</p>