Concatenating Primes Challenge: 

You are given a string containing only digits. Your task is to output the minimum number of primes which must be concatenated to form the string. If this is impossible, output 0.

Test Cases:

Input -> Output:

252 -&gt; 3
235 -&gt; 2
92 -&gt; 0
31149 -&gt; 2
JavaScript (ES6), 123 121 120 bytes

f=(s,v)=&gt;(p=n=&gt;--n-1?s%n&amp;&amp;p(n):1)(s)||[...s].map((_,i)=&gt;!(n=i&amp;&amp;(a=f(s.slice(0,i)))&amp;&amp;(b=f(s.slice(i)))&amp;&amp;a+b)|n&gt;v?0:v=n)|v


Saved a byte thanks to @Neil!

Explanation

Takes a single string as input. Due to the prime checking method (recursive trial division), the greatest number that can be safely checked is 13840. Some numbers above this will fail due to the maximum call stack size being exceeded. It does, however, finish instantly for every case it can handle.



f=(s,v)=&gt;
  (p=n=&gt;--n-1?s%n&amp;&amp;p(n):1)(s) // if s is prime, return 1
  ||[...s].map((_,i)=&gt;        // else for each index in s, split s into 2
    !(n=i&amp;&amp;                   // v = return value (initialised to undefined)
      (a=f(s.slice(0,i)))&amp;&amp;
      (b=f(s.slice(i)))&amp;&amp;
      a+b
    )|n&gt;v?0:v=n               // if i, a, b and n are all &gt; 0 and !(n &gt; v), v = n
  )|v                         // cast v to an integer and return it

// Test
var testCases = [ "252", "235", "92", "3149", "24747" ];
document.write("&lt;pre&gt;" + testCases.map(c =&gt; c + ": " + f(c)).join("\n"));