title: How to find backward primes within a range of integers?
body: <p>I'm trying to solve a backward prime question.</p>
  
  <p>Following is the question:</p>
  
  <blockquote>
    <p>Find all Backwards Read Primes between two positive given numbers (both inclusive), the second one being greater than the first one. The resulting array or the resulting string will be ordered following the natural order of the prime numbers.</p>
  </blockquote>
  
  <p>Example</p>
  
  <pre><code>backwardsPrime(2, 100) =&gt; [13, 17, 31, 37, 71, 73, 79, 97] 
  backwardsPrime(9900, 10000) =&gt; [9923, 9931, 9941, 9967]
  </code></pre>
  
  <p>I tried doing something like this:</p>
  
  <pre><code>public function backwardPrime()
  {
      $start = 7000;
      $stop = 7100;
  
      $ans = [];
  
      while($start &lt;= $stop)
      {
          if($start &gt; 10)
          {
              if($start !== $this-&gt;reverse($start))
              {
                  if($this-&gt;isPrime($start) &amp;&amp; $this-&gt;isPrime($this-&gt;reverse($start)))
                  {
                      array_push($ans, $start);
                  }
              }
          }
          $start++;
      }
      return $ans;
  }
  
  public function reverse($num)
  {
      $reverse = 0;
      while($num &gt; 0)
      {
          $reverse = $reverse * 10;
          $reverse = $reverse + $num%10;
          $num = (int)($num/10);
      }
      return $reverse;
  }
  
  public function isPrime($num)
  {
      if($num == 1 || $num == 2 || $num == 3)
          return true;
      elseif ($num%2 == 0 || $num%3 == 0)
          return false;
      else
      {
          $i=5;
          while($i&lt;=$num/2)
          {
              if($num%$i===0)
              {
                  return false;
              }
              $i++;
          }
      }
      return true;
  }
  </code></pre>
  
  <p>I'm able to get the appropriate answer but while doing the same in single function I'm not able to get it:</p>
  
  <pre><code>public function backwardPrimes()
  {
      $start = 7000;
      $stop = 7100;
      $ans = [];
      while($start &lt;= $stop)
      {
          $isStartPrime = true;
          $isReversePrime = true;
          if($start &gt; 10)
          {
              $reverse = 0;
              $num = $start;
              while($num &gt; 0)
              {
                  $reverse = $reverse * 10;
                  $reverse = $reverse + $num%10;
                  $num = (int)($num/10);
              }
              if($start !== $reverse)
              {
                  if($start%2 != 0 &amp;&amp; $start%3 != 0)
                  {
                      $i =5;
                      while($i&lt;=$start/2)
                      {
                          if($start%$i === 0)
                          {
                              $isStartPrime = false;
                              break;
                          }
                          $i++;
                      }
                  }
                  if($reverse%2 != 0 &amp;&amp; $reverse%3 != 0)
                  {
                      $i =5;
                      while($i&lt;=$reverse/2)
                      {
                          if($reverse%$i === 0)
                          {
                              $isReversePrime = false;
                              break;
                          }
                          $i++;
                      }
                  }
                  if($isStartPrime &amp;&amp; $isReversePrime)
                  {
                      array_push($ans, $start);
                  }
              }
          }
          $start++;
      }
      return $ans;
  }
  </code></pre>
  
  <p>I don't know where I'm having mistake, guide me.</p>
  
  <p>Thanks.</p>