# Ugly Numbers

## Pen Settings

#### Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

#### Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

#### Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

#### Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

## 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>
```
```
!

## CSS

```            ```

```
```
!

## 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");

```
```
!
999px