title: Distributing a loop to different machines of a cluster in Spark
body: <p>Here is a for loop that I'm running in my code:</p>
  
  <pre><code> for(x&lt;-0 to vertexArray.length-1)
    {
      for(y&lt;-0 to vertexArray.length-1)
        {
          breakable {
  
            if (x.equals(y)) {
              break
            }
            else {
              var d1 = vertexArray(x)._2._2
              var d2 = vertexArray(y)._2._2
              val ps = new Period(d1, d2)
  
              if (ps.getMonths() == 0 &amp;&amp; ps.getYears() == 0 &amp;&amp; Math.abs(ps.toStandardHours().getHours()) &lt;= 5) {
                edgeArray += Edge(vertexArray(x)._1, vertexArray(y)._1, Math.abs(ps.toStandardHours().getHours()))
              }
            }
          }
        }
    }
  </code></pre>
  
  <p>I want to speed up the running time of this code by distributing it across multiple machines in a cluster. I'm using Scala on intelliJ-idea with Spark. How would I implement this type of code to work on multiple machines? </p>