You test every candidate number for primality, even though the only even prime is 2. Your particular implementation could be more efficient, however: There are other prime number sieves, but the SofE is pretty simple to implement and surely adequate for this task. The Sieve of Eratosthenes is a reasonably good choice. Sieve of Eratosthenes: private static int GetPrimes(int n)Ĭonsole.WriteLine(primes.First(IsPandigital)) Int allBitsExceptBitOne = (1 maxDigit || (digitBits & mask) != 0) Int maxDigit = (int)Math.Log10(number) + 1 My implementation of IsPandigital method: private static bool IsPandigital(int number) Since you need only the largest pandigital number, it makes sense to scan down starting from the largest 7-digit pandigital number. There only the 4-digit and 7-digit numbers left. You can exclude all n-digit pandigitals for n = 2, 3, 5, 6, 8, 9 since they cannot be prime due to the Divisibility by 3 or 9 rule. Long lastIndex = Array.LastIndexOf(primes, true) Ĭonsole.WriteLine("Time to calculate in milliseconds : ", sw.ElapsedMilliseconds)
![pandigital scanner motor stopped working pandigital scanner motor stopped working](https://i1.wp.com/the-digital-reader.com/wp-content/uploads/2010/12/pandigital-9-inch-tablet-2.jpg)
#Pandigital scanner motor stopped working code#
Any improvements concerning the performance and the code style are welcome. It's currently running for about 75-78k milliseconds which really bothers me that's a lot of time. Here we take the last index that has true value (the last prime number) and than we start decreasing the index that we are looking for if we incur a number that is pandigital and it's prime we just break out of all the loops and print it on the screen. Lastly we iterate through the array of prime : long lastIndex = Array.LastIndexOf(primes, true)
![pandigital scanner motor stopped working pandigital scanner motor stopped working](https://i.pcmag.com/imagery/reviews/04Z2gOJlDmLI9ANZGcB5L9F-1.fit_lpad.size_625x365.v1569471423.jpg)
Pretty simple check just taking the current index of the primes array, convert's it to a char array and we know that a pandigital number is that number that it has each from 1 to it's length exactly one (1, number.Length) so we do that in the for loop and than we check if it's contained exactly once if so we return true else we return false. First we need an algorithm that calculates all the primes up to our upper limit (987654321 ), I went for the sieve of Eratosthenes : bool primes = SetPrimes(987654321) įor (long i = 2 i char.GetNumericValue(x) = i) We are working with primes and more specifically pandigital primes. So we know that the upper bound should be a maximum of 987654321 which is the biggest pandigital number it's not a prime however so we probably can lower this but for now let's keep it like this. What is the largest n-digit pandigital prime that exists? For example, 2143 is a 4-digit pandigital and is also prime. We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. I recently finished the project Euler problem # 41 :