title: Algorithm to find a number following these restrictions
body: <ul>
  <li>The only digits that can be used are 3, 6 and 9.</li>
  <li>A specific digit can occur at most three times in the number.</li>
  <li>The number must be divisible by 9.</li>
  </ul>
  
  <p>This is my following code:</p>
  
  <pre><code> for (int i = 0; i &lt;= 9999; i=i+9)
          {
              if (Math.Ceiling(Math.Log10(i)) == 4)
              {
                  if (i.ToString().Contains('3') &amp;&amp; i.ToString().Contains('6') &amp;&amp; i.ToString().Contains('9')&amp;&amp;!i.ToString().Contains('0'))
                  {
                      Console.WriteLine(i);
                  }
              }
          }
  </code></pre>
  
  
  
  <p>The issue with my code is restriction #2: A specific digit can occur at most three times in the number.</p>
  
  <p>It is not printing out for example: 3339, which is also divisible by 9 and follows all the criteria, any suggestions?</p>
  
  <p>Thank you</p>