/// <summary> /// Returns whether the number /// contains 1-9 exactly once and is 9 digits long. /// </summary> /// <param name="number"></param> /// <returns></returns> public bool IsPandigital(int number) { // create 10 booleans to represent 0-9. // for each digit, set the index of that digit // to true. If any are false, then it is not pandigital. // 0 is set to true because it is not included. // first, check to see if we have 9 digits. if (DigitHelper.DigitCount(number) != DigitLength) { return(false); } int temp = number; bool[] nums = new bool[10]; nums[0] = true; while (temp > 0) { int n = temp % 10; // get the next digit nums[n] = true; temp /= 10; // so we can grab the next digit next time } return(nums.All(n => n)); }
/// <summary> /// Returns whether a number is pandigital to its own digit count, /// such that 2143 is pandigital to 4 digits. /// </summary> /// <param name="number"></param> /// <returns></returns> public bool IsPandigitalToOwnDigitCount(int number) { int digitCount = DigitHelper.DigitCount(number); bool[] nums = new bool[digitCount + 1]; nums[0] = true; int temp = number; while (temp > 0) { int n = temp % 10; if (n > digitCount) { return(false); // it can't be pandigital is you have a 6 in a 5 digit number. } nums[n] = true; temp /= 10; } return(nums.All(n => n)); }