0
  • The only digits that can be used are 3, 6 and 9.
  • A specific digit can occur at most three times in the number.
  • The number must be divisible by 9.

This is my following code:

 for (int i = 0; i <= 9999; i=i+9)
        {
            if (Math.Ceiling(Math.Log10(i)) == 4)
            {
                if (i.ToString().Contains('3') && i.ToString().Contains('6') && i.ToString().Contains('9')&&!i.ToString().Contains('0'))
                {
                    Console.WriteLine(i);
                }
            }
        }

The issue with my code is restriction #2: A specific digit can occur at most three times in the number.

It is not printing out for example: 3339, which is also divisible by 9 and follows all the criteria, any suggestions?

Thank you

3
  • 2
    If your number must be divisible by 9 it would be a performance improvement to increment i by 9, so you don't waste time checking 90% of numbers that cannot meet requirement s Commented Jan 26, 2020 at 19:37
  • If you repeatedly divide and mod your number by 10 you will get the one rightmost digit, which you can then check for being a) in 3,6,9 and b) using some ifs can count the number of occurrences of these digits, checking for breaches of the rule Commented Jan 26, 2020 at 19:40
  • 2
    I'm nearly certain this is an academic exercise, by the way, which does lead me to wonder - do your tutors not teach you to write the algorithm out in English (or your native language) in comments first - then translate it to c#? You're learning c#; you don't think in it. You should write your algorithm in the language you think in, then translate to c#. Bonus: you end up with nicely commented code so your tutor can see the difference between what you wanted to do and what you did. Kinda like showing your working in an exam q Commented Jan 26, 2020 at 19:47

3 Answers 3

1

If i understand you, your friend could be a regular expression + some linq, like this:

if (Regex.IsMatch(i.ToString(), "^[369]{1,}$") && 
    i.ToString().GroupBy(x => x).All(x => x.Count() < 4))
{
    // The Regex checks if the number contains 3/6/9, nothing else.
    // The Linq (second condition) checks if all number occurs at most 3 times.
}

Don't forget the usings:

using System.Linq;
using System.Text.RegularExpressions;
Sign up to request clarification or add additional context in comments.

2 Comments

Your regex will match also 3333, where Andrew told, that one digit can not occur more than 3 times.
Look on my answer. ^(?!.*(.).*\1.*\1.*\1)[369]+$ Checks if number contains only 3, 6 and 9, where single digit can't repeat more than 3 times. You don't need any linq.
1

Your desired alghoritm looks like this:

for (int i = 0; i <= 9999; i += 9) // Increment by 9, to avoid numbers not divisible by 9
{
    if (Regex.IsMatch(i.ToString(), @"^(?!.*(.).*\1.*\1.*\1)[369]+$")) // Checks if number contains only 3, 6 and 9, where single digit can't repeat more than 3 times
        Console.WriteLine(i);
}

1 Comment

Im not a regex pro, so i used some extra linq, but you win :)
0

The array numbers represents the occurrences of the digits of the number

(int)s[j]-(int)'0' transform a character into the corresponding integer number

 int[] numbers = new int[10]{0,0,0,0,0,0,0,0,0,0};
 string s=i.ToString();
 for (int j=0; j<s.Length; j++) {
    numbers[(int)s[j]-(int)'0']++;
 }
 if (numbers[3]+numbers[6]+numbers[9]==4 && numbers[3] <=3 && numbers[6] <=3 && numbers[9] <=3)
 {
     if (checkDivisibleNine(i))
     {
          Console.WriteLine(i);
     }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.