Skip to content

Instantly share code, notes, and snippets.

@xenomuta
Last active December 16, 2015 20:59
Show Gist options
  • Save xenomuta/5496513 to your computer and use it in GitHub Desktop.
Save xenomuta/5496513 to your computer and use it in GitHub Desktop.
Búsqueda de números palíndromos dentro de una lista de rangos.
console.time("Tiempo de computo");
var total = 0, unicos = 0, rangos = [], min = 99999999999, max = 0;
function alReves (num) {
for (var rev = 0;; rev = rev * 10 + (num % 10), num = parseInt(num / 10))
if (num <= 0) return rev;
}
var lineas = require("fs").readFileSync("seed.txt", "utf-8").toString().split(/\n/);
for (var linea in lineas) {
var rango = lineas[linea].split(/\s+/)
if (rango.length < 2) break;
var ini = Math.min(rango[0], rango[1]), fin = Math.max(rango[0], rango[1]);
rangos.push([ini, fin]);
if (min > ini) min = ini;
if (max < fin) max = fin;
}
rangos.sort();
for (var i = min; i < max; i++) if (i == alReves(i)) {
++unicos;
for (var r in rangos)
if ((r = rangos[r])[0] > i || r[1] < i) continue;
else total++;
}
console.timeEnd("Tiempo de computo");
console.log("Palindromos unicos:", unicos);
console.log("Total en rangos:", total);
/*
OS: MacOSX 10.7.5
Processor: 2.3.GHz Intel Core i5
Memory: 8GB 1333 MHz DDR3
Node.JS v0.8.1
[xenomuta@MiniXantina:~/Codigo]$ time node palindromos.js
Tiempo de computo: 377ms
Palindromos unicos: 2638
Total en rangos: 106814
real 0m0.448s
user 0m0.436s
sys 0m0.011s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment