CodePen

HTML

            
              <html>
 <body>
   Possible Solution for this <a href="http://www.quora.com/Can-you-help-me-solve-this-math-problem">Quora Question</a>
   
    <div id="output"></div>
  </body>
</html>
            
          
!

↑ Insert the most common viewport meta tag

CSS

            
              
            
          
!
? ?
? ?
Must be a valid URL.
+ add another resource
via CSS Lint

JS

            
              function getResults( testCase )
{
  var out = document.getElementById("output");
  var count = getCombinations( testCase, 0, 1, 0 );
  out.innerHTML += testCase + " has " + count+ " ugly solution"+(count!=1 ? "s" : "")+"<br/>";
}

function getCombinations( digits, sum, factor, total )
{
   for ( var i = 1; i < digits.length; i++ )
   {
      var front = digits.substr(0,i);
      var back  = digits.substr(i);
     
      total = getCombinations( back, sum +  factor * parseInt(front,10),  1, total );
     total = getCombinations( back, sum +  factor * parseInt(front,10), -1, total );
    }
  
  var solution = sum + factor * parseInt(digits,10);
  return total + ( solution == 0 ||
           solution % 2 == 0 ||
           solution % 3 == 0 ||
           solution % 5 == 0 ||
           solution % 7 == 0 ? 1 : 0 );
}

getResults("1");
getResults("9");
getResults("011");
getResults("12345");

            
          
!
Must be a valid URL.
+ add another resource
via JS Hint
Loading ..................