2007/02/12

 

Riddle


/**
 * This solves the riddle:<BR><BR>
 
 * If you have a 10 digit number using all the digits 0-9, find a number
 * where the leftmost value is divisible by 1, the two leftmost values are
 * divisible by 2, etc. The full number will eventually be divisible by 10.
 
 * The answer to this riddle is 3816547290.
 */
private static void solve() {
  for (int x0 = 0; x0 <= 9; x0++) {
    final long t0 = x0;
    for (int x1 = 0; x1 <= 9; x1++) {
      if (x1 == x0continue;
      final long t1 = 10 * t0 + x1;
      if (t1 % != 0continue;
      for (int x2 = 0; x2 <= 9; x2++) {
        if (x2 == x1 || x2 == x0continue;
        final long t2 = 10 * t1 + x2;
        if (t2 % != 0continue;
        for (int x3 = 0; x3 <= 9; x3++) {
          if (x3 == x2 || x3 == x1 || x3 == x0continue;
          final long t3 = 10 * t2 + x3;
          if (t3 % != 0continue;
          for (int x4 = 0; x4 <= 9; x4++) {
            if (x4 == x3 || x4 == x2 || x4 == x1 || x4 == x0continue;
            final long t4 = 10 * t3 + x4;
            if (t4 % != 0continue;
            for (int x5 = 0; x5 <= 9; x5++) {
              if (x5 == x4 || x5 == x3 || x5 == x2
               || x5 == x1 || x5 == x0continue;
              final long t5 = 10 * t4 + x5;
              if (t5 % != 0continue;
              for (int x6 = 0; x6 <= 9; x6++) {
                if (x6 == x5 || x6 == x4 || x6 == x3
                 || x6 == x2 || x6 == x1 || x6 == x0continue;
                final long t6 = 10 * t5 + x6;
                if (t6 % != 0continue;
                for (int x7 = 0; x7 <= 9; x7++) {
                  if (x7 == x6 || x7 == x5 || x7 == x4
                   || x7 == x3 || x7 == x2 || x7 == x1 || x7 == x0continue;
                  final long t7 = 10 * t6 + x7;
                  if (t7 % != 0continue;
                  for (int x8 = 0; x8 <= 9; x8++) {
                    if (x8 == x7 || x8 == x6 || x8 == x5
                     || x8 == x4 || x8 == x3 || x8 == x2
                     || x8 == x1 || x8 == x0continue;
                    final long t8 = 10 * t7 + x8;
                    if (t8 % != 0continue;
                    for (int x9 = 0; x9 <= 9; x9++) {
                      if (x9 == x8 || x9 == x7 || x9 == x6
                       || x9 == x5 || x9 == x4 || x9 == x3
                       || x9 == x2 || x9 == x1 || x9 == x0continue;
                      final long t9 = 10 * t8 + x9;
                      if (t9 % 10 != 0continue;
                      System.out.println(x0 + "" + x1 + "" + x2 + ""
                                       + x3 + "" + x4 + "" + x5 + ""
                                       + x6 + "" + x7 + "" + x8 + "" + x9);
                      return;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Comments: Post a Comment



<< Home

Copyright © 2006 Stein Gunnar Bakkeby