performance - Solving Combinatory Problems with LINQ /.NET4 -


I saw a pop-up in my MSDN RSS feed, and after reading through it, I started thinking about the solution . / P>

The rules are simple:

Find a 9-digit number in which only one digit from 1 to 9 appear only once. This number should also meet these divisibility requirements:

  1. The number should be divisible by 9.
  2. If the correct digit has been removed, then the remaining number should be divisible by 8.
  3. If the correct number of the new number has been removed, the remaining number should be divisible by 7.
  4. And so on, unless there is only one digit (which is necessarily divisible 1).

This is his proposed demon LINQ query:

  // # # numerical issue presented in the LINQ solution: http: // http: // software. Intel.com/en-us/blogs/2009/12/07/intel-parallel-studio-great-for-sriial-code-too-episode-1/ intel [] acetine = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9}; // query var query = i1 to iToone from iToNine in iToine where i2! = I1 & amp; Amp; (I1 * 10 + i2)% 2 == 0 from i3 in etononin where i3! = I2 & amp; Amp; I3! = I1 & amp; Amp; (I1 * 100 + i2 * 10 + i3)% 3 == 0 from i4 in acetonein where i4! = I3 & amp; Amp; I4! = I2 & amp; Amp; I4! = I1 & amp; Amp; (I1 * 1000 + i2 * 100 + i3 * 10 + i4) Once in i5 = 4 = Where i5! = I4 & amp; Amp; I5! = I3 & amp; Amp; I5! = I2 & amp; Amp; I5! = I1 & amp; Amp; (I1 * 10000 + i2 * 1000 + i3 * 100 + i4 * 10 + i5)% 5 == 0 from i1 in atononein where i6! = I5 & amp; Amp; I6! = I4 & amp; Amp; I6! = I3 & amp; Amp; I6! = I2 & amp; Amp; I6! = I1 & amp; Amp; (I1 * 100000 + i2 * 10000 + i3 * 1000 + i4 * 100 + i5 * 10 + i6) * 6 == = 1 from i7 in acetonein where i7! = I6 & amp; Amp; I7! = I5 & amp; Amp; I7! = I4 & amp; Amp; I7! = I3 & amp; Amp; I7! = I2 & amp; Amp; I7! = I1 & amp; Amp; (I1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + i5 * 100 + i6 * 10 + i7) * 7 == 0 from i8 in acetonein where i8! = I7 & amp; Amp; I8! = I6 & amp; Amp; I8! = I5 & amp; Amp; I8! = I4 & amp; Amp; I8! = I3 & amp; Amp; I8! = I2 & amp; Amp; I8! = I1 & amp; Amp; (I1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + i5 * 1000 + i6 * 100 + i7 * 10 + i8) * 8 == 0 in iTonin from i9 where i9! = I8 & amp; Amp; I9! = I7 & amp; Amp; I9! = I6 & amp; Amp; I9! = I5 & amp; Amp; I9! = I4 & amp; Amp; I9! = I3 & amp; Amp; I9! = I2 & amp; Amp; I9! = I1 number = i1 * 100000000 + i2 * 10000000 + i3 * 1000000 + i4 * 100000 + i5 * 10000 + i6 * 1000 + i7 * 100 + i8 * 10 + i9 * 1 where number is% 9 == 0 select number; // Drive! Foreach (int n in the console) console WrightLine (n);  

Octavio said "No note has been made to customize the code", what I want to know is that if we try to optimize this code are doing. Can this really get the best code? I would like to know how we can do the best with NT4, especially in parallel as much as we can. I'm not necessarily looking for an answer in pure LINQ, manage (.NET4) in any form (managed c ++, c #, etc. all acceptable).

If you have access to an ImmutableList class, then it can do a very minimal solution for each Instead of trying each one at the level, you pass only the remaining prospects to the next state. Plus, keeping total totals at each level reduces the number of calculations.

  In slow query = I1, in Tuple.Create (0L, allNums). Choosing NextNumber (1) from i2 to i1 in i3 from i3. Select next number (3) _ i4 in i4. Choose the next number (4) _ i5 to i4. Select the next number (5) _ i5 to i6. Select next number (6) i7 to i7 to i6. Choose from I8.ChooseNextNumber (8) _ I8 in I8 in IN.ChooseNextNumber (9) Select I9.Item1 & amp; System; Runtime.CompilerServices.Extension () & gt; _ Select the personal function group (as the integer, the integer, the integer as integer), the form of the built-in module as integer) _Inumerable as integer (integer, immutableList (integer)) as the previous I NewTotal = previous.Item1 * 10 + i where the newColed Mod moduleSubbs = 0 select Tupl.Create (NewTool, Previous Item2.Remove (i)) Last Function  
< / Html>

Comments