Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     var bArray = b.ToArray();
     return from aItem in a
     let aTail = aItem % 100
     from bItem in bArray
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment comment):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
     number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     var bArray = b.ToArray();
     return from aItem in a
     let aTail = aItem % 100
     from bItem in bArray
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
     number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     var bArray = b.ToArray();
     return from aItem in a
     let aTail = aItem % 100
     from bItem in bArray
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
     number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
avoided multiple times enumeration
Source Link
Dmitry
  • 4.6k
  • 1
  • 20
  • 32
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     var bArray = b.ToArray();
     return from aItem in a
     let aTail = aItem % 100
     from bItem in bbArray
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
     number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     return from aItem in a
     let aTail = aItem % 100
     from bItem in b
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
     number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     var bArray = b.ToArray();
     return from aItem in a
     let aTail = aItem % 100
     from bItem in bArray
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
     number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
improved as Eric Lippert advised
Source Link
Dmitry
  • 4.6k
  • 1
  • 20
  • 32
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     return from aItem in a
     let aTail = aItem % 100
     from bItem in b
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need two methods to get digits of a number andmethod to test a number if it is pandigital (based on the Eric Lippert's comment ):

     private static IEnumerable<int>bool GetDigitsIsPandigital(long number)
     {
     whileconst (numberint !TotalDigits = 0)10;
     {
    const int AllBits = (1 << TotalDigits) - 1;
     yield return (int)(number % 10);
    int digitBits = 0;
     while (number /!= 10;0)
     }{
     }
     private static bool IsPandigital(long number)
     {
      bool[]int isDigitdigit = new(int)(number bool[10];% 10);
     foreach (int digit in GetDigits(number))
    int mask = 1 << {digit;
     if (isDigit[digit](digitBits & mask) != 0)
     return false;
     isDigit[digit]digitBits |= mask;
      number /= true;10;
     }
     return isDigit.All(xdigitBits =>== x);AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     return from aItem in a
     let aTail = aItem % 100
     from bItem in b
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need two methods to get digits of a number and to test a number if it is pandigital:

     private static IEnumerable<int> GetDigits(long number)
     {
     while (number != 0)
     {
     yield return (int)(number % 10);
     number /= 10;
     }
     }
     private static bool IsPandigital(long number)
     {
      bool[] isDigit = new bool[10];
     foreach (int digit in GetDigits(number))
     {
     if (isDigit[digit])
     return false;
     isDigit[digit] = true;
     }
     return isDigit.All(x => x);
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
  1. Store in an array all primes we will use:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
    
  2. Create the GetDivisibles method to generate a sequence of 3-digit numbers (possible with leading zero(s)), which are divisible by a given prime number:

     private static IEnumerable<int> GetDivisibles(int divider)
     {
     for (int i = divider; i <= 999; i += divider)
     yield return i;
     }
    
  3. Create a method to join two sequences of divisibles: n-digit (a) and 3-digit (b) to a new sequence of n+1-digit numbers where the last 2 digits of each number from the first sequence (a) equal to the first 2 digits of a number from b.

     private static IEnumerable<int> JoinDivisibles(IEnumerable<int> a, IEnumerable<int> b)
     {
     return from aItem in a
     let aTail = aItem % 100
     from bItem in b
     where bItem / 10 == aTail
     select aItem * 10 + (bItem % 10);
     }
    
  4. Now we need a method to test a number if it is pandigital (based on the Eric Lippert's comment ):

     private static bool IsPandigital(long number)
     {
     const int TotalDigits = 10;
     const int AllBits = (1 << TotalDigits) - 1;
     int digitBits = 0;
     while (number != 0)
     {
     int digit = (int)(number % 10);
     int mask = 1 << digit;
     if ((digitBits & mask) != 0)
     return false;
     digitBits |= mask;
      number /= 10;
     }
     return digitBits == AllBits;
     }
    
  5. Run it, iterating through all divisibles found and all first digits of 10-digit number:

     int[] primes = { 2, 3, 5, 7, 11, 13, 17 };
     var divisibles = GetDivisibles(2);
     for (int i = 1; i < primes.Length; i++)
     {
     divisibles = JoinDivisibles(divisibles, GetDivisibles(primes[i]));
     }
     long sum = 0;
     foreach (int divisible in divisibles)
     {
     for (long i = 1; i <= 9; i++)
     {
     long number = i * 1000000000 + divisible;
     if (IsPandigital(number))
     sum += number;
     }
     }
     Console.WriteLine(sum);
    
Source Link
Dmitry
  • 4.6k
  • 1
  • 20
  • 32
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /