English 中文(简体)
调查结果 图3
原标题:Finding Largest Palindromic Number which is Product of two 3 digit numbers

申斥: 根据“Euler项目”提出的许多棕榈群问题,但我允诺,这个问题略有不同。

Question:

A palindromic number reads the same both ways. The smallest 6 digit palindrome made from the product of two 3-digit numbers is 101101=143*707.

Find the largest palindrome made from the product of two 3-digit numbers which is less than N (a given input which contains 6 digits).

So to make an efficient algo, after doing my research on Palindromic numbers, I concluded this fact: All even digit palindromic numbers are divisible by 11. But all 6 digit multiples of 11 are palindromic.

def palc(n):
near=(n-(n%11))
for i in range(near,100000,-11):
    if str(i)==str(i)[::-1]:
        for j in range(990,110,-11):
            if (i%j)==0 and len(str(i//j))==3:
                return i

So I submitted this solution in a coding site and I passed every known test cases and all unknown test cases EXCEPT one. So the sad part is its a hidden test case and I tried to print the test case but still didn t work.

Is there a still more easier way to make it more efficient? Please kindly help me.

问题回答

Precompute the 279 6-digit palindromic numbers that are the product of 2 3-digit numbers.

使用双轨搜索,或者由于该清单太小,只是一带。

名单上:

[99999, 101101, 102201, 105501, 106601, 108801, 110011, 111111, 117711, 119911, 121121, 122221, 123321, 127721, 128821, 129921, 131131, 133331, 135531, 137731, 138831, 140041, 141141, 142241, 143341, 147741, 149941, 154451, 155551, 156651, 159951, 161161, 162261, 165561, 168861, 171171, 174471, 178871, 180081, 182281, 184481, 187781, 188881, 189981, 198891, 201102, 202202, 204402, 209902, 210012, 212212, 213312, 214412, 215512, 216612, 219912, 220022, 221122, 222222, 225522, 227722, 231132, 232232, 234432, 235532, 238832, 239932, 242242, 244442, 246642, 249942, 252252, 255552, 256652, 257752, 258852, 259952, 262262, 266662, 270072, 272272, 273372, 276672, 277772, 279972, 280082, 282282, 284482, 286682, 289982, 290092, 292292, 294492, 296692, 297792, 299992, 301103, 302203, 303303, 306603, 308803, 320023, 321123, 324423, 329923, 330033, 333333, 335533, 343343, 345543, 348843, 354453, 357753, 359953, 363363, 366663, 367763, 369963, 371173, 372273, 374473, 375573, 377773, 378873, 384483, 391193, 393393, 397793, 399993, 401104, 402204, 404404, 405504, 407704, 408804, 409904, 412214, 414414, 416614, 420024, 421124, 424424, 425524, 426624, 428824, 432234, 434434, 436634, 438834, 440044, 441144, 442244, 443344, 444444, 445544, 447744, 452254, 456654, 459954, 461164, 462264, 464464, 468864, 469964, 470074, 471174, 474474, 476674, 477774, 484484, 485584, 487784, 488884, 489984, 491194, 493394, 505505, 506605, 507705, 508805, 509905, 510015, 512215, 513315, 514415, 515515, 519915, 520025, 522225, 523325, 525525, 528825, 531135, 534435, 535535, 536635, 543345, 545545, 548845, 549945, 550055, 551155, 554455, 555555, 560065, 561165, 564465, 565565, 570075, 571175, 573375, 575575, 576675, 577775, 579975, 580085, 585585, 588885, 589985, 592295, 595595, 601106, 602206, 603306, 604406, 606606, 611116, 612216, 616616, 618816, 619916, 623326, 630036, 631136, 636636, 639936, 642246, 648846, 649946, 650056, 652256, 653356, 654456, 656656, 657756, 660066, 661166, 663366, 666666, 672276, 675576, 678876, 689986, 693396, 696696, 698896, 723327, 729927, 737737, 747747, 749947, 770077, 780087, 793397, 801108, 802208, 804408, 807708, 809908, 819918, 821128, 824428, 828828, 840048, 853358, 855558, 861168, 886688, 888888, 906609]

Here s something to play with. It tries to pick options based on restrictions in the multiplicand digits. We re multiplying

  a1a2a3
*
  b1b2b3

为棕榈地选择一位外部数字限制了3和3的备选办法。 我没有为挑选外部目标数字或1和b1进行自动化,尽管我们能够做到。 这里就是一个例子,就是那些人都到9岁,导致棕榈地最多不足1 000 000 000。

Java印法:

var outer = 9;
var a1 = 9;
var b1 = 9;

var modHash = new Array(10);
var iterations = 0;
var highest = 0;

for (var i = 1; i < 10; i++){
  modHash[i] = {0: [0]}
  for (var j = 1; j < 10; j++){
    iterations ++;
    var r = i * j % 10;
    if (modHash[i][r])
      modHash[i][r].push(j);
    else 
      modHash[i][r] = [j];
  }
}

function multiples(x, y, carry, mod){
  for (var i in modHash[x]){
    var m = (10 + mod - i - carry) % 10;
    
    if (modHash[y][m]){
      for (var j in modHash[x][i]){
        for (var k in modHash[y][m]){
          iterations ++;
          var palindrome = num(
            a1,modHash[y][m][k],x,
            b1,modHash[x][i][j],y);
          
          var str = String(palindrome);
          
          if (str == str.split("").reverse().join("") && palindrome > highest){
            console.log(palindrome);
            highest = palindrome;
          }
        }
      }
    }
  }
}

function num(a1, a2, a3, b1, b2, b3){
  return (100*a1 + 10*a2 + a3)
       * (100*b1 + 10*b2 + b3);
}

var a3b3s = [];

for (let x = 1; x < 10; x++) {
  for (let y = x; y < 10; y++) {
    iterations++;
    const xy = x * y;
    if (xy % 10 == outer) {
      a3b3s.push(
        [x, y, Math.floor(xy / 10)]);
    }
  }
}

console.log("[[a3,b3,carry]]: " + JSON.stringify(a3b3s));

for (var i in a3b3s){
  for (var mod = 0; mod < 10; mod++){
    let x = a3b3s[i][0],
        y = a3b3s[i][1],
        carry = a3b3s[i][2];
    multiples(x, y, carry, mod);
  }
}

console.log("Highest: " + highest);
console.log("Iterations: " + iterations);




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签