Created
September 5, 2017 17:06
-
-
Save josephbales/0c61b37ecaad38dbc672249963b1ae79 to your computer and use it in GitHub Desktop.
Pandigital Helpers in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication4 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
GetPandigitalList(0, 500, 1, 3); | |
} | |
public static List<long> GetPandigitalList(long startingNum, long endingNum, int panNumStart, int panNumEnd) | |
{ | |
List<long> numberList = new List<long>(); | |
//int numLength = panNumEnd - panNumStart + 1; | |
List<string> startNumStrList = new List<string>(); | |
for (int i = panNumStart; i <= panNumEnd; i++) | |
{ | |
startNumStrList.Add(i.ToString()); | |
} | |
long startNum = Convert.ToInt64(String.Join("", startNumStrList.ToArray())); | |
long endNum = Convert.ToInt64(String.Join("", startNumStrList.ToArray().Reverse())); | |
startNum = (startingNum > startNum) ? startingNum : startNum; | |
endNum = (endingNum > endNum) ? endNum : endingNum; | |
for (long i = startNum; i <= endNum; i++) | |
{ | |
if (IsPandigital(i, startNumStrList)) numberList.Add(i); | |
} | |
return numberList; | |
} | |
public static bool IsPandigital(long candidate, List<string> numList) | |
{ | |
List<char> candidateCharList = candidate.ToString().ToList(); | |
if (candidate.ToString().Length != candidateCharList.Distinct().Count()) | |
{ | |
return false; | |
} | |
List<string> candidateStringList = candidateCharList.Select(x => x.ToString()).ToList(); | |
if (candidateStringList.Intersect(numList).Count() != candidateStringList.Count) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment