Fizzbuzz - un cuarteto de soluciones -- tree campo con fizzbuzz campo con assembly campo con lookup camp codereview Relacionados El problema

FizzBuzz - A quartet of solutions


3
vote

problema

Español

Desde el fizzbuzz desafío se trata de desarrollar ideas, he agregado

  • un tercer caso de prueba para el que elegí divisibilidad por 7 y su acompañamiento Etiqueta "Go". Esta etiqueta se dio a propósito una longitud distinta de 4 para resistir la escritura. una optimización que de otra manera afectaría la adaptabilidad.
  • Salida en una redirección manera amistosa. Ideal para la salida de la consola, impresión, y por qué no mencionar el almacenamiento de cinta, pero complica las cosas cuando utilizando 'decrementar los contadores' como verá a continuación.

He explorado una serie de técnicas, todo igualmente valioso:

  1. un proceso de eliminación
  2. a árbol de decisión binario
  3. decrementar los contadores
  4. a mesa de búsqueda

En todas partes se necesitaba una división, elegí reemplazar el 998877666555443312 por un 0. 0. 1. 0.27639320225002106 0.8506508083520399 0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 0. 0. 1. -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0. 0. 1. -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0. 0. 1. 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0. 0. 1. 0.8944271909999159 0. 0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0. 0. -1. 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0. 0. -1. -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0. 0. -1. -0.8944271909999159 0. -0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0. 0. -1. -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 0. 0. -1. 0.27639320225002106 0.8506508083520399 0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 3 instrucciones. Esto solo puede funcionar si ni dividendos ni divisores. excede los 255. Provió para ser un movimiento ganador como:

  • conserva (un montón de) espacio
  • evita usar un registro adicional para el divisor
  • entrega banderas en el resto (en 0. 0. 1. 0.27639320225002106 0.8506508083520399 0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 0. 0. 1. -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0. 0. 1. -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0. 0. 1. 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0. 0. 1. 0.8944271909999159 0. 0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0. 0. -1. 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0. 0. -1. -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0. 0. -1. -0.8944271909999159 0. -0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0. 0. -1. -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 0. 0. -1. 0.27639320225002106 0.8506508083520399 0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 4 !)

La salida se puede personalizar fácilmente cambiando las 3 variables de tiempo de compilación (número de) cols , (número de) filas , y ancho (de una columna).
Las únicas restricciones son que el producto filas x cols debe caer en [1,255] y que ancho debe permitir mostrar el número de 3 dígitos, un separador y Cualquier combinación de etiquetas.


proceso de eliminación.

El orden en que se realizan las eliminaciones es crucial. No solo para obteniendo resultados correctos, pero también porque la elección de un orden óptimo Reduce el número de divisiones en un 5 a 6 por ciento, ya que se muestra la siguiente tabla.

  0.  0.  1.  0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.7236067977499789 0.5257311121191336  0.4472135954999579 0.  0.  1.  -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.  0.  1.  -0.7236067977499789 -0.5257311121191336 0.4472135954999579  0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.  0.  1.  0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.8944271909999159  0.  0.4472135954999579 0.  0.  1.  0.8944271909999159  0.  0.4472135954999579  0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.  0.  -1. 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.  0.  -1. -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.  0.  -1. -0.8944271909999159 0.  -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.  0.  -1. -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 0.  0.  -1. 0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 5  

Eliminar la mayoría de los números lo más temprano posible se logra utilizando el uso del Divisores más pequeños primero. En fizzbuzzgo pobre significa 105, 35, 21, 15, 7, 5, 3 y mejor significa 105, 15, 21, 35, 3, 5, 7.

  0.  0.  1.  0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.7236067977499789 0.5257311121191336  0.4472135954999579 0.  0.  1.  -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.  0.  1.  -0.7236067977499789 -0.5257311121191336 0.4472135954999579  0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.  0.  1.  0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.8944271909999159  0.  0.4472135954999579 0.  0.  1.  0.8944271909999159  0.  0.4472135954999579  0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.  0.  -1. 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.  0.  -1. -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.  0.  -1. -0.8944271909999159 0.  -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.  0.  -1. -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 0.  0.  -1. 0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 6  

Árbol de decisión binario.

La siguiente tabla muestra que usar un árbol binario reduce el número de divisiones enormemente. Además, gana importancia a medida que aumenta el número de casos de prueba.

  0.  0.  1.  0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.7236067977499789 0.5257311121191336  0.4472135954999579 0.  0.  1.  -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.  0.  1.  -0.7236067977499789 -0.5257311121191336 0.4472135954999579  0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.  0.  1.  0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.8944271909999159  0.  0.4472135954999579 0.  0.  1.  0.8944271909999159  0.  0.4472135954999579  0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.  0.  -1. 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.  0.  -1. -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.  0.  -1. -0.8944271909999159 0.  -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.  0.  -1. -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 0.  0.  -1. 0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 7  

En FIZZBUZZGO El número de divisiones se redujo en un 49 por ciento.

Es muy fácil convertir este árbol binario en algo más corto que no Utilice cualquier salto condicional. Sin embargo, decidí no mantenerlo porque me siento. Hay al menos alguna contradicción en el árbol binario . Además fue un poco más lento.

  0.  0.  1.  0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.7236067977499789 0.5257311121191336  0.4472135954999579 0.  0.  1.  -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.  0.  1.  -0.7236067977499789 -0.5257311121191336 0.4472135954999579  0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.  0.  1.  0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.8944271909999159  0.  0.4472135954999579 0.  0.  1.  0.8944271909999159  0.  0.4472135954999579  0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.  0.  -1. 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.  0.  -1. -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.  0.  -1. -0.8944271909999159 0.  -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.  0.  -1. -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 0.  0.  -1. 0.27639320225002106 0.8506508083520399  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579 0.7236067977499789  -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579  -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 0.7236067977499789  0.5257311121191336  -0.4472135954999579 0.8944271909999159  0.  0.4472135954999579  0.7236067977499789  -0.5257311121191336 -0.4472135954999579 -0.27639320225002106    0.8506508083520399  -0.4472135954999579 0.27639320225002106 0.8506508083520399  0.4472135954999579  0.7236067977499789  0.5257311121191336  -0.4472135954999579 -0.8944271909999159 0.  -0.4472135954999579 -0.7236067977499789 0.5257311121191336  0.4472135954999579  -0.27639320225002106    0.8506508083520399  -0.4472135954999579 -0.27639320225002106    -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579  -0.8944271909999159 0.  -0.4472135954999579 8  

Decrobación de contadores.

Dado que la salida es ser amigable con la redirección y que la matriz en el La pantalla muestra la columna llena de columnas, ajustes a estos contadores decrecientes son necesarios. Estos ajustes representan las decrementos faltantes debido al 0. 0. 1. 0.27639320225002106 0.8506508083520399 0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 0. 0. 1. -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0. 0. 1. -0.7236067977499789 -0.5257311121191336 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0. 0. 1. 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0. 0. 1. 0.8944271909999159 0. 0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0. 0. -1. 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0. 0. -1. -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0. 0. -1. -0.8944271909999159 0. -0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0. 0. -1. -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 0. 0. -1. 0.27639320225002106 0.8506508083520399 0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 0.27639320225002106 -0.8506508083520399 0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 0.8944271909999159 0. 0.4472135954999579 0.7236067977499789 -0.5257311121191336 -0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 0.27639320225002106 0.8506508083520399 0.4472135954999579 0.7236067977499789 0.5257311121191336 -0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 -0.7236067977499789 0.5257311121191336 0.4472135954999579 -0.27639320225002106 0.8506508083520399 -0.4472135954999579 -0.27639320225002106 -0.8506508083520399 -0.4472135954999579 -0.7236067977499789 -0.5257311121191336 0.4472135954999579 -0.8944271909999159 0. -0.4472135954999579 9 paso cuando Ir a la siguiente columna. Los números no se procesan en orden natural. Por eso.

en lugar de decrementar que podría haber usado incrementando en los contadores. Usualmente (1) Si lo hace, se requiere una instrucción extra 9988776655544332020

que sin duda agregará al tamaño del código Pero no necesariamente las cosas lentas hacia abajo.
(1) Es posible evitar el 99887766555443321 mientras aún aumenta cuando nosotros Comience desde números negativos y se mueve hacia cero.

  2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 2  

Tabla de búsqueda.

La tabla de búsqueda (LUT) está llena de bytes de clasificación.
No es necesario que los datos en esta tabla se repitan. Hay más para ganar de reutilizar los mismos datos de búsqueda (almacenamiento en caché) que que hay que perder en el Instrucciones adicionales para buscarla.
La lut se puede crear en tiempo de ejecución con una rutina de inicialización o en tiempo de compilación. Es fácil llenar el lut a mano. Para el simple fuzzbuzz a mero. 15 bytes. Elegí dejarlo el ensamblador haga el trabajo.
Las mesas de búsqueda son instrumentos poderosos. Me pregunto si alguna vez algún solicitante cuando Confrontado con la tarea original de Fizzbuzz ha usado este enfoque. No estoy seguro ¡Sobre lo que el reclutador habría dicho tampoco!

  99887766555443323  

Tiempo de ejecución

Todas las pruebas se realizaron en un Pentium 166MHz que se ejecuta en modo de dirección real.

  2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 4  

No hay mucho que se pueda decir al mirar el tiempo de ejecución total.
La eliminación de la llamada DOS API muestra lo predominante que realmente es.
Tocando todas las rutinas comunes, finalmente establece una clasificación de algún tipo.


Tamaño del código

  2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 5  

Después de golpear todas las rutinas comunes, se vuelve claro (ER) que, como es A menudo, el caso, hay un precio para pagar la velocidad aumentada.


Extensibilidad

Escribí estos programas con la extensibilidad en mente. Si el número de prueba Los casos aumentan, las versiones que utilizan un proceso de eliminación o un árbol binario, Rápidamente se vuelven más complejos. La versión que utiliza decrementar los contadores puede ser modificado fácilmente. Pero la versión que usa una tabla de búsqueda necesita muy poco. cambiar de hecho.

Vea cómo graciosamente no usé el 2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 6 , 2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 7 y 2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 8 Registros para que ¿Se podría usar fácilmente para los casos de 4º, 5º y 6º prueba?
¿Por qué no intente su mano en fizzbuzzgoyesweCan (divisores 3, 5, 7, 11, 13, y 17)? Usar números primos garantiza que cada etiqueta aparecerá por sí misma en menos una vez.

! Sugerencia: Dado que los números van de 1 a 255, es inútil probar con Los divisores superiores a 255. El 2771204605521258752,354.99796946135444,14.540437148032728 2771204742960125824,354.82484279830356,14.415707483801699 2771204742960126080,354.8298951417335,14.415989960651892 2771204777319869440,354.8029071094679,14.423268507989384 2771204811679612544,354.81845541510194,14.431099973159371 2771204880399095936,354.8457063064526,14.440336041720947 2771204983478149760,354.7888726523609,14.428521435486967 2771204983478301312,354.79834319472315,14.426543847147219 2771204983478303104,354.8005161335831,14.42791145520911 2771205223996496896,354.8109855922654,14.463377776632576 2771205223996502784,354.8090346004138,14.471344809515811 2771205228292116224,354.8161795185497,14.466725484321556 2771205258356230784,354.8605444051401,14.45630798849794 2771205430154941312,354.8868752267852,14.481107665444423 2771205533235029504,354.83772161984615,14.47128996739678 2771205601954482176,354.82237084958234,14.481094852179371 2771205670673114880,354.8670878351698,14.48870385525905 2771205670674023936,354.8595228004882,14.485559575409033 2771205773752341248,354.86085950623266,14.505758545293629 2771205778047955456,354.8609952813116,14.502627279103612 2771205808112038528,354.77186051710794,14.449099660599714 2771205876831516416,354.76023619352054,14.449883454390623 2771205876832294272,354.76299178061487,14.45200820799446 2771205945551001216,354.7889731814567,14.46157162357397 2771205979910751104,354.79879415814753,14.476962176777173 2771206220428932992,354.77291041880505,14.494400659822366 2771206254788674048,354.7811307911569,14.497769928711854 2771206392228463360,354.82139167668066,14.493094428947858 2817884371678369280,349.9513847843209,16.81616842372345 2817884406038111744,349.9166809824806,16.822203915110855 2817884406038113408,349.922753905713,16.823778938852282 2817884440397853312,349.9335912366887,16.827163469580135 2817884440397856896,349.93009817366493,16.832567329127055 2817884474757557376,349.8551142973262,16.772287502999728 2817884509117109888,349.85642566007493,16.77622731670913 2817884509117304064,349.86264368355125,16.784407833125968 2817884612196521600,349.8710862987843,16.790036441368127 2817884650851650816,349.87348368092927,16.803791542316404 2817884680916010368,349.85757395459746,16.80772538367227 2817884783995221120,349.8334640252565,16.801516432255852 2817884783995224576,349.84094679068534,16.807143421574697 2817884818354957568,349.8140522573419,16.799147482474005 2817884852714704512,349.8348205657008,16.811726267485383 2817884887074230656,349.8436346912421,16.814360970847027 2817884887074443264,349.8548374335605,16.81220751898652 9 la instrucción no lo digiere de todos modos.

¡Diviértete!

Original en ingles

Since the FizzBuzz challenge is about developing ideas, I've added

  • a third test case for which I chose divisibility by 7 and its accompanying tag "Go". This tag was purposely given a length other than 4 to resist writing an optimization that would otherwise impair adaptability.
  • outputting in a redirection friendly manner. Ideal for console output, printing, and why not mention tape storage, but it complicates matters when using 'decrementing counters' as you'll see below.

I've explored a number of techniques, all equally valuable:

  1. An elimination process
  2. A binary decision tree
  3. Decrementing counters
  4. A lookup table

Everywhere a division was needed I chose to replace the usual div instruction by an aam instruction. This can only work if neither dividend nor divider exceed 255. It proved to be a winning move as it:

  • conserves (a lot of) space
  • avoids using an extra register for the divider
  • delivers flags on the remainder (in AL!)

The output can easily be customized by changing the 3 compile-time variables (number of )COLS, (number of )ROWS, and WIDTH( of one column).
The only restrictions are that the product ROWSxCOLS must fall in [1,255] and that WIDTH must allow displaying the 3 digit number, a separator, and any combination of tags.


Elimination process.

The order in which the eliminations take place is crucial. Not only for obtaining correct results but also because choosing an optimal ordering reduces the number of divisions by 5 to 6 percent, as next table shows.

Processing     FizzBuzz   FizzBuzzGo   FizzBuzzGo+ 255 numbers    (3,5)      (3,5,7)      (3,5,7,11) -------------------------------------------------- poor ordering     697        1596         3199 best ordering     663        1498         2986 

Eliminating the most numbers as early as possible is achieved by using the smaller dividers first. On FizzBuzzGo poor means 105, 35, 21, 15, 7, 5, 3 and best means 105, 15, 21, 35, 3, 5, 7.

COLS=3 ROWS=85 WIDTH=16         ORG     256         cld         mov     bp,1                    ;BP is current number [1,255] Row:    push    bp                      ;(1)         mov     di,String Col:    call    AddNumberToString       ; -> DI (AX)         call    ClassifyNumber          ; -> AL (AH)         call    AddTagsToString         ; -> DI (AL BX CX)         add     bp,ROWS                 ;Step to next column         cmp     bp,ROWS*COLS         jbe     Col         call    PrintString             ; -> (AX DX DI)         pop     bp                      ;(1)         inc     bp                      ;Step to next row         cmp     bp,ROWS         jbe     Row         int     20h                     ;DOS.Terminate ; --------------------------------------         ALIGN   16 ; Sets bit 0 of AL if number in BP is divisible by 3 ; Sets bit 1 of AL if number in BP is divisible by 5 ; Sets bit 2 of AL if number in BP is divisible by 7 ; IN (bp) OUT (al) MOD (ah) ClassifyNumber:         mov     ax,bp         aam     3*5*7         jz      .111         mov     ax,bp         aam     3*5         jz      .011         mov     ax,bp         aam     3*7         jz      .101         mov     ax,bp         aam     5*7         jz      .110         mov     ax,bp         aam     3         jz      .001         mov     ax,bp         aam     5         jz      .010         mov     ax,bp         aam     7         jz      .100 .000:   mov     al,000b                 ;None         ret .111:   mov     al,111b                 ;FizzBuzzGo         ret .011:   mov     al,011b                 ;FizzBuzz         ret .101:   mov     al,101b                 ;FizzGo         ret .110:   mov     al,110b                 ;BuzzGo         ret .001:   mov     al,001b                 ;Fizz         ret .010:   mov     al,010b                 ;Buzz         ret .100:   mov     al,100b                 ;Go         ret ; --------------------------------------         ALIGN   16 ; IN (di,bp) OUT (di) MOD (ax) AddNumberToString:         mov     ax,"  "                 ;Right justify 3 digits         stosw         mov     ax,bp                   ;BP=[0,255]         aam         add     al,"0"         stosb         test    ah,ah         jz      .RET         mov     al,ah         aam         add     al,"0"         mov     [di-2],al         test    ah,ah         jz      .RET         add     ah,"0"         mov     [di-3],ah .RET:   ret ; --------------------------------------         ALIGN   16 ; IN (al,di) OUT (di) MOD (al,bx,cx) AddTagsToString:         lea     bx,[di-3+WIDTH]         ;Start of next column         test    al,al         jz      .Align                  ;No tags needed         mov     byte [di]," "           ;Separator         inc     di         mov     si,Tags                 ;Show one or more tags .Tag:   movzx   cx,[si]                 ;Length of current tag         inc     si         shr     al,1         jnc     .Skip         rep movsb .Skip:  add     si,cx         test    al,al         jnz     .Tag         jmp     .Align .Space: mov     byte [di]," "           ;Align to start of next column         inc     di .Align: cmp     di,bx         jb      .Space         ret ; --------------------------------------         ALIGN   16 ; IN (di) OUT () MOD (ax,dx,di) PrintString:         inc     di                      ;Remove trailing spaces (if any) .Trim:  dec     di         cmp     byte [di-1]," "         je      .Trim         mov     ax,0A0Dh                ;Append carriage return and linefeed         stosw         mov     byte [di],"$"           ;StringTerminator for DOS         mov     dx,String         mov     ah,09h                  ;DOS.PrintString         int     21h         ret ; -------------------------------------- Tags:   db      4,'Fizz',4,'Buzz',2,'Go' String: rb      WIDTH*COLS+3 ; -------------------------------------- 

Binary decision tree.

Next table shows that using a binary tree reduces the number of divisions enormously. Moreover it gains importance as the number of test cases rises.

Processing     FizzBuzz   FizzBuzzGo   FizzBuzzGo+ 255 numbers    (3,5)      (3,5,7)      (3,5,7,11) -------------------------------------------------- Elimination       663        1498         2986 Binary tree       510         765         1020 

On FizzBuzzGo the number of divisions was reduced by 49 percent.

It is very easy to turn this binary tree into something shorter that doesn't use any conditional jumps. I decided however to not keep it because I feel there's at least some contradiction in branchless binary tree. Moreover it was a bit slower.

COLS=3 ROWS=85 WIDTH=16         ORG     256         cld         mov     bp,1                    ;BP is current number [1,255] Row:    push    bp                      ;(1)         mov     di,String Col:    call    AddNumberToString       ; -> DI (AX)         call    ClassifyNumber          ; -> AL (AH)         call    AddTagsToString         ; -> DI (AL BX CX)         add     bp,ROWS                 ;Step to next column         cmp     bp,ROWS*COLS         jbe     Col         call    PrintString             ; -> (AX DX DI)         pop     bp                      ;(1)         inc     bp                      ;Step to next row         cmp     bp,ROWS         jbe     Row         int     20h                     ;DOS.Terminate ; --------------------------------------         ALIGN   16 ; Sets bit 0 of AL if number in BP is divisible by 3 ; Sets bit 1 of AL if number in BP is divisible by 5 ; Sets bit 2 of AL if number in BP is divisible by 7 ; IN (bp) OUT (al) MOD (ah) ClassifyNumber:         mov     ax,bp         aam     7         mov     ax,bp         jz      .7y .7n:    aam     5         mov     ax,bp         jz      .7n5y .7n5n:  aam     3         setz    al         ret                             ;0=None 1=Fizz .7n5y:  aam     3         setz    al         add     al,010b         ret                             ;2=Buzz 3=FizzBuzz .7y:    aam     5         mov     ax,bp         jz      .7y5y .7y5n:  aam     3         setz    al         add     al,100b         ret                             ;4=Go 5=FizzGo .7y5y:  aam     3         setz    al         add     al,110b         ret                             ;6=BuzzGo 7=FizzBuzzGo ; --------------------------------------         ALIGN   16 ; IN (di,bp) OUT (di) MOD (ax) AddNumberToString:         mov     ax,"  "                 ;Right justify 3 digits         stosw         mov     ax,bp                   ;BP=[0,255]         aam         add     al,"0"         stosb         test    ah,ah         jz      .RET         mov     al,ah         aam         add     al,"0"         mov     [di-2],al         test    ah,ah         jz      .RET         add     ah,"0"         mov     [di-3],ah .RET:   ret ; --------------------------------------         ALIGN   16 ; IN (al,di) OUT (di) MOD (al,bx,cx) AddTagsToString:         lea     bx,[di-3+WIDTH]         ;Start of next column         test    al,al         jz      .Align                  ;No tags needed         mov     byte [di]," "           ;Separator         inc     di         mov     si,Tags                 ;Show one or more tags .Tag:   movzx   cx,[si]                 ;Length of current tag         inc     si         shr     al,1         jnc     .Skip         rep movsb .Skip:  add     si,cx         test    al,al         jnz     .Tag         jmp     .Align .Space: mov     byte [di]," "           ;Align to start of next column         inc     di .Align: cmp     di,bx         jb      .Space         ret ; --------------------------------------         ALIGN   16 ; IN (di) OUT () MOD (ax,dx,di) PrintString:         inc     di                      ;Remove trailing spaces (if any) .Trim:  dec     di         cmp     byte [di-1]," "         je      .Trim         mov     ax,0A0Dh                ;Append carriage return and linefeed         stosw         mov     byte [di],"$"           ;StringTerminator for DOS         mov     dx,String         mov     ah,09h                  ;DOS.PrintString         int     21h         ret ; -------------------------------------- Tags:   db      4,'Fizz',4,'Buzz',2,'Go' String: rb      WIDTH*COLS+3 ; -------------------------------------- 

Decrementing counters.

Given that the output is to be redirection friendly and that the matrix on the screen shows up filled column wise, adjustments to these decrementing counters are necessary. These adjustments account for the missing decrements due to the GT 1 step when going to the next column. The numbers aren't processed in natural order. Hence.

Instead of decrementing I could have used incrementing on the counters. Usually (1) doing so requires an extra cmp instruction that will certainly add to the code size but not necessarily slow things down.
(1) It's possible to avoid the additional cmp while still incrementing when we start from negative numbers and move towards zero.

COLS=3 ROWS=85 WIDTH=16         ORG     256         cld         mov     bp,1                    ;BP is current number [1,255]         mov     cl,7                    ;CL is Go-counter         mov     bx,0503h                ;BH is Buzz-counter, BL is Fizz-counter Row:    push    bx cx bp                ;(1)         mov     di,String         jmp     First Col:    call    AdjustCounters          ; -> BX CL First:  call    AddNumberToString       ; -> DI (AX)         call    DecrementCounters       ; -> AL BX CL         push    bx cx                   ;(2)         call    AddTagsToString         ; -> DI (AL BX CX)         pop     cx bx                   ;(2)         add     bp,ROWS                 ;Step to next column         cmp     bp,ROWS*COLS         jbe     Col         call    PrintString             ; -> (AX DX DI)         pop     bp cx bx                ;(1)         call    DecrementCounters       ; -> AL BX CL         inc     bp                      ;Step to next row         cmp     bp,ROWS         jbe     Row         int     20h                     ;DOS.Terminate ; --------------------------------------         ALIGN   16 ; IN (bx,cl) OUT (bx,cl) MOD () AdjustCounters:         sub     bl,(ROWS-1) mod 3         ja      .3a         add     bl,3 .3a:    sub     bh,(ROWS-1) mod 5         ja      .5a         add     bh,5 .5a:    sub     cl,(ROWS-1) mod 7         ja      .7a         add     cl,7 .7a:    ret ; --------------------------------------         ALIGN   16 ; IN (bx,cl) OUT (al,bx,cl) MOD () DecrementCounters:         dec     bl         jnz     .3nz         mov     bl,3                    ;Reset Fizz .3nz:   setz    al         dec     bh         jnz     .5nz         mov     bh,5                    ;Reset Buzz         or      al,2 .5nz:   dec     cl         jnz     .7nz         mov     cl,7                    ;Reset Go         or      al,4 .7nz:   ret                             ;Classification in AL=[0,7] ; --------------------------------------         ALIGN   16 ; IN (di,bp) OUT (di) MOD (ax) AddNumberToString:         mov     ax,"  "                 ;Right justify 3 digits         stosw         mov     ax,bp                   ;BP=[0,255]         aam         add     al,"0"         stosb         test    ah,ah         jz      .RET         mov     al,ah         aam         add     al,"0"         mov     [di-2],al         test    ah,ah         jz      .RET         add     ah,"0"         mov     [di-3],ah .RET:   ret ; --------------------------------------         ALIGN   16 ; IN (al,di) OUT (di) MOD (al,bx,cx) AddTagsToString:         lea     bx,[di-3+WIDTH]         ;Start of next column         test    al,al         jz      .Align                  ;No tags needed         mov     byte [di]," "           ;Separator         inc     di         mov     si,Tags                 ;Show one or more tags .Tag:   movzx   cx,[si]                 ;Length of current tag         inc     si         shr     al,1         jnc     .Skip         rep movsb .Skip:  add     si,cx         test    al,al         jnz     .Tag         jmp     .Align .Space: mov     byte [di]," "           ;Align to start of next column         inc     di .Align: cmp     di,bx         jb      .Space         ret ; --------------------------------------         ALIGN   16 ; IN (di) OUT () MOD (ax,dx,di) PrintString:         inc     di                      ;Remove trailing spaces (if any) .Trim:  dec     di         cmp     byte [di-1]," "         je      .Trim         mov     ax,0A0Dh                ;Append carriage return and linefeed         stosw         mov     byte [di],"$"           ;StringTerminator for DOS         mov     dx,String         mov     ah,09h                  ;DOS.PrintString         int     21h         ret ; -------------------------------------- Tags:   db      4,'Fizz',4,'Buzz',2,'Go' String: rb      WIDTH*COLS+3 ; -------------------------------------- 

Lookup table.

The lookup table (LUT) is filled with classification bytes.
No need to have the data in this table repeat itself. There's more to gain from re-using the same lookup data (caching) than that there's to loose on the additional instructions to fetch it.
The LUT can either be created at run-time with an initialization routine or at compile-time. It's easy to fill the LUT by hand. For the simple FizzBuzz a mere 15 bytes. I chose to let the assembler do the work.
Lookup tables are powerful instruments. I wonder if ever any applicant when confronted with the original FizzBuzz task has used this approach. Not sure about what the recruiter would have said either!

COLS=3 ROWS=85 WIDTH=16 COUNT=ROWS*COLS if 3*5*7 < COUNT                        ;Avoid superfluous data   COUNT=3*5*7 end if         ORG     256         cld         mov     bp,1                    ;BP is current number [1,255] Row:    push    bp                      ;(1)         mov     di,String Col:    call    AddNumberToString       ; -> DI (AX)         lea     ax,[bp-1]         aam     COUNT                   ;Remainder will index the LUT         movzx   si,al         mov     al,[Lut+si]         call    AddTagsToString         ; -> DI (AL BX CX)         add     bp,ROWS                 ;Step to next column         cmp     bp,ROWS*COLS         jbe     Col         call    PrintString             ; -> (AX DX DI)         pop     bp                      ;(1)         inc     bp                      ;Step to next row         cmp     bp,ROWS         jbe     Row         int     20h                     ;DOS.Terminate ; --------------------------------------         ALIGN   16 ; IN (di,bp) OUT (di) MOD (ax) AddNumberToString:         mov     ax,"  "                 ;Right justify 3 digits         stosw         mov     ax,bp                   ;BP=[0,255]         aam         add     al,"0"         stosb         test    ah,ah         jz      .RET         mov     al,ah         aam         add     al,"0"         mov     [di-2],al         test    ah,ah         jz      .RET         add     ah,"0"         mov     [di-3],ah .RET:   ret ; --------------------------------------         ALIGN   16 ; IN (al,di) OUT (di) MOD (al,bx,cx) AddTagsToString:         lea     bx,[di-3+WIDTH]         ;Start of next column         test    al,al         jz      .Align                  ;No tags needed         mov     byte [di]," "           ;Separator         inc     di         mov     si,Tags                 ;Show one or more tags .Tag:   movzx   cx,[si]                 ;Length of current tag         inc     si         shr     al,1         jnc     .Skip         rep movsb .Skip:  add     si,cx         test    al,al         jnz     .Tag         jmp     .Align .Space: mov     byte [di]," "           ;Align to start of next column         inc     di .Align: cmp     di,bx         jb      .Space         ret ; --------------------------------------         ALIGN   16 ; IN (di) OUT () MOD (ax,dx,di) PrintString:         inc     di                      ;Remove trailing spaces (if any) .Trim:  dec     di         cmp     byte [di-1]," "         je      .Trim         mov     ax,0A0Dh                ;Append carriage return and linefeed         stosw         mov     byte [di],"$"           ;StringTerminator for DOS         mov     dx,String         mov     ah,09h                  ;DOS.PrintString         int     21h         ret ; --------------------------------------         ALIGN   16 Lut:    times COUNT db -4*(((% mod 7)-7)/7)-2*(((% mod 5)-5)/5)-(((% mod 3)-3)/3) Tags:   db      4,'Fizz',4,'Buzz',2,'Go' String: rb      WIDTH*COLS+3 ; -------------------------------------- 

Execution time

All testing was done on a Pentium 166MHz running in real address mode.

                complete      w/o api call   w/o routines --------------------------------------------------------- Elimination    172133 xc2xb5sec      693 xc2xb5sec       200 xc2xb5sec Binary tree    171928 xc2xb5sec      474 xc2xb5sec       105 xc2xb5sec Counters       171916 xc2xb5sec      420 xc2xb5sec        52 xc2xb5sec Lookup table   171747 xc2xb5sec      299 xc2xb5sec        43 xc2xb5sec 

There's not much that can be said when looking at the total execution time.
Removing the DOS api call shows how predominant it really is.
Striking all of the common routines finally establishes a ranking of some kind.


Code size

                complete                     w/o routines --------------------------------------------------------- Elimination     260 bytes                       98 bytes Binary tree     244 bytes                       82 bytes Counters        260 bytes                      122 bytes Lookup table    294 bytes                      153 bytes 

After striking all of the common routines it becomes clear(er) that - as is often the case - there's a price to pay for the increased speed.


Extensibility

I wrote these programs with extensibility in mind. If the number of test cases increases, the versions that use an elimination process or a binary tree, rapidly become more complex. The version that uses decrementing counters can be modified easily. But the version that uses a lookup table needs very little change indeed.

See how I gracefully didn't use the CH, DL, and DH registers so they could easily be used for the 4th, 5th, and 6th test cases?
Why then not try your hand at FizzBuzzGoYesWeCan (dividers 3, 5, 7, 11, 13, and 17)? Using prime numbers guarantees every tag will show up by itself at least once.

! Hint: Given the numbers range from 1 to 255, it's useless to test with dividers greater than 255. The aam instruction wouldn't digest it anyway.

Have fun!

           
 
 

Lista de respuestas


Relacionados problema

8  Implementación de Python Property () que almacene en caché mientras está permitiendo un setter  ( Python property implementation that caches getter while still allowing a sette ) 
Para una implementación de sesión Necesitaba una propiedad que almacena en caché su Getter (ya que implica una búsqueda de base de datos), pero aún permite ...

3  Nede.js JSON Búsqueda y actualización  ( Node js json searching updating ) 
Estoy trabajando en un controlador de encendido / apagado basado en la web para múltiples interruptores. Estoy buscando una buena manera de administrar el est...

3  Objeto Javascript a JSON  ( Javascript object to json ) 
Dado que no encontré una manera de transformar adecuadamente el objeto JavaScript a JSON con PHP, escribí un fragmento. Permite asumir el siguiente extract...

2  Optimizar el programa para probar la divisibilidad de los números 3.0  ( Optimize program to test for divisibility of numbers 3 0 ) 
Este es un seguimiento en mi anterior Pregunta . La respuesta de JS1 < / a> sugirió que debería usar una tabla precaputada que contenga todas las permutac...

4  Buscar en un árbol binario  ( Search on a binary tree ) 
de SICP : Ejercicio 2.66. Implementar la búsqueda Procedimiento para el caso donde el conjunto. de los registros está estructurado como un binario Á...

23  Cree una clase reutilizable para obtener diferentes valores de las decoraciones de atributos enum  ( Create a re usable class to get different values from enum attribute decorations ) 
Estoy deseando crear una clase reutilizable para trabajar en las enumeraciones para que pueda decorar varios enumanos con diferentes atributos y puedo usar es...

7  ¿Devolver datos y actualizar fila sin múltiples bonitas?  ( Return data and update row without multiple lookups ) 
Tengo un procedimiento almacenado que busca un artículo basado en el título del artículo. Pero también necesito incrementar una columna en la misma tabla que ...

7  Escribir imagen Datos de píxeles al código de impresora  ( Writing image pixel data to printer code ) 
He estado tratando de tomar datos de píxeles de imagen y escribirlo en el código de la impresora y mis resultados son bastante lentos. Aquí hay una versión ...

11  JSON Búsqueda por clave  ( Json lookup by key ) 
Siempre hice mi búsqueda en la colección JSON así: VersionFactory7 Pero siempre me pregunté si había una forma mejor / más óptima de hacer esto. ¿Algu...

8  Rubygem para enumias  ( Rubygem for enums ) 
Un poco de fondo primero. He estado trabajando en una aplicación de rieles bastante grandes que se convirtió rápidamente en un cero de reproducción para el có...




© 2022 respuesta.top Reservados todos los derechos. Centro de preguntas y respuestas reservados todos los derechos