zondag 6 maart 2011

Euler problem 32

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

Solution:

First we write down all the combinations that will yield to 9 digits.

    i   x   j  =  p
    1    9999  9999 
   10     999  9990
   99     100  9900
  100      99  9900 -> duplicated
  999      10  9990
  999        1  9999 
 1000       1  10000 -> not pandigital

axioma 1: The product must be less than 10000 to get pandigital. 
axioma 2: To prohibit duplicates i must be less than j. According axioma 1 i must be less than sqrt(10000)=100
axioma 3: String handling and Linq are slow.


Performance improvements:

ad 1) j < 10000/i     // 63409 -> 118 ms.
ad 2) i < 100           //    118 ->  75 ms.
ad 3) digits[i % 10]   //     75 ->  11 ms.

Below there is an improved version of the pandigital test according axioma 3.



Problem32 =       45228 elapsed time:   11 ms. Test Passed.

Geen opmerkingen:

Een reactie posten