Cada vez es más difícil ser compuesto en estos días

14

Dada una lista no vacía L de números enteros mayores que 1 , definimos d (L) como el número entero positivo más pequeño tal que n + d (L) es de material compuesto para cada n en L .

Definimos la secuencia a n como:

  • a 0 = 2
  • a i + 1 es el entero más pequeño mayor que a i tal que d (a 0 , ..., a i , a i + 1 )> d (a 0 , ..., a i )

Tu tarea

Usted puede:

  • Tome un número entero N y devuelva el término N-ésimo de la secuencia (indexado 0 o indexado 1)
  • Tome un entero N y devuelva los primeros N términos de la secuencia
  • No ingrese nada e imprima la secuencia para siempre

Este es el , por lo que gana la respuesta más corta en bytes.

Está bien si su código se vuelve lento a medida que N se hace más grande, pero al menos debería encontrar los 20 primeros términos en menos de 2 minutos.

Primeros términos

  • un 0 = 2 y d (2) = 2 (tenemos que añadir 2 de manera que 2 + 2 es compuesto)
  • a 1 = 3 porque d (2, 3) = 6 (necesitamos sumar 6 para que 2 + 6 y 3 + 6 sean compuestos)
  • a 2 = 5 porque d (2, 3, 5) = 7 (necesitamos sumar 7 para que 2 + 7, 3 + 7 y 5 + 7 sean todos compuestos), mientras que d (2, 3, 4) sigue siendo igual a 6
  • etc.

A continuación se muestran los 100 primeros términos de la secuencia (desconocidos en OEIS en el momento de la publicación).

  2,   3,   5,   6,  10,  15,  17,  19,  22,  24,
 30,  34,  35,  39,  41,  47,  51,  54,  56,  57,
 70,  79,  80,  82,  92,  98, 100, 103, 106, 111,
113, 116, 135, 151, 158, 162, 165, 179, 183, 186,
191, 192, 200, 210, 217, 223, 226, 228, 235, 240,
243, 260, 266, 274, 277, 284, 285, 289, 298, 307,
309, 317, 318, 329, 341, 349, 356, 361, 374, 377,
378, 382, 386, 394, 397, 405, 409, 414, 417, 425,
443, 454, 473, 492, 494, 502, 512, 514, 519, 527,
528, 560, 572, 577, 579, 598, 605, 621, 632, 642
Arnauld
fuente

Respuestas:

1

Pyth, 24 bytes

Pu+GfP_+Tf!fP_+ZYG)eGQ]2

Demostración

Básicamente, comenzamos con [2], luego agregamos elementos 1 a la vez al buscar alternativamente d, luego agregamos un elemento, y así sucesivamente. Salidas las primerasn elementos de la secuencia.

Contiene un filtro dentro de un primer filtro entero dentro de un primer filtro entero dentro de un ciclo aplicar repetidamente.

Explicación:

Pu+GfP_+Tf!fP_+ZYG)eGQ]2
 u                   Q]2    Starting with `[2]`, do the following as many times
                            as the input
         f        )         Starting from 1 and counting upward, find d where
          !f     G          None of the elements in the current list
            P_+ZY           plus d give a prime.
    f              eG       Starting from the end of the current list and counting
                            upward, find the first number which
     P_+T                   plus d gives a prime.
  +G                        Append the new number to the current list
P                           Trim the last element of the list

Hay un esfuerzo obvio repetido entre las dos llamadas "Agregar y verificar si es primo", pero no estoy seguro de cómo eliminarlo.

isaacg
fuente
1

Jalea , 31 bytes

ṛ,;1+ÆPẸṆʋ1#ⱮẎ</
Ḷ߀‘Ṫç1#ƊḢƲ2⁸?

Pruébalo en línea!

La gelatina apesta en las secuencias de recurrencia. : /

Erik el Outgolfer
fuente
1

Retina , 74 bytes

K`__;
"$+"{/;(?!(__+)\1+\b)/+`;
;_
.+$
$&¶$&
)/;(__+)\1+$/+`.+$
_$&_
%`\G_

Pruébalo en línea! 0 indexado. Explicación:

K`__;

Cada línea ien el área de trabajo contiene dos valores unarios, aᵢ;d+aᵢ. Comenzamos con a₀=2y d+a₀=0(porque es más golfista).

"$+"{
...
)

Repita los Ntiempos de bucle .

/;(?!(__+)\1+\b)/+`

Repita mientras haya al menos un número no compuesto.

;
;_

Incremento d.

.+$
$&¶$&

Duplicar la última línea, copiando aᵢ₋₁a aᵢ.

/;(__+)\1+$/+`

Repita mientras d+aᵢes compuesto.

.+$
_$&_

Incremento aᵢ.

%`\G_

Convierte los resultados a decimal.

Neil
fuente
1

Limpio , 138 130 128 bytes

import StdEnv
$l=until(\v=all(\n=any((<)1o gcd(n+v))[2..n+v-1])l)inc 1

map hd(iterate(\l=hd[[n:l]\\n<-[hd l..]| $[n:l]> $l])[2])

Pruébalo en línea!

Expandido:

$l=until(\v=all(\n=any((<)1o gcd(n+v))[2..n+v-1])l)inc 1
$l=until(                                         )inc 1  // first value from 1 upwards where _
         \v=all(                                )l        // _ is true for everything in l
                \n=any(              )[2..n+v-1]          // any of [2,3..n+v-1] match _
                             gcd(n+v)                     // the gcd of (n+v) and ^
                           o                              // (composed) ^ is _
                       (<)1                               // greater than 1

map hd(iterate(\l=hd[[n:l]\\n<-[hd l..]| $[n:l]> $l])[2]) 
map hd(                                                 ) // the first element of each list in _
       iterate(                                     )[2]  // infinite repeated application of _ to [2]
               \l=hd[                              ]      // the head of _
                     [n:l]                                // n prepended to l
                          \\n<-[hd l..]                   // for every integer n greater than the head of l
                                       | $[n:l]> $l       // where d(n0..ni+1) > d(n0..ni)
Οurous
fuente
1

Julia 0.6 , 145 130 bytes

~L=(x=1;while any(map(m->all(m%i>0 for i=2:m-1),L+x));x+=1;end;x)
!n=n<1?[2]:(P=!(n-1);t=P[end]+1;while ~[P;t]<=~P;t+=1;end;[P;t])

Pruébalo en línea!

(-15 bytes usando mis nuevas y mejoradas habilidades de golf: sobrecargas del operador, reemplazo condicional por ternario y, por lo tanto, eliminación return palabra clave).

Ampliado :

function primecheck(m)
    all(m%i>0 for i2:m-1)
end

function d(L)
    x = 1
    while any(map(primecheck, L+x))
        x += 1
    end
    return x
end

function a(n)
    n > 0 || return [2]
    Prev = a(n-1)
    term = Prev[end] + 1
    while d([Prev;term])  d(Prev)
        term += 1
    end
    return [Prev;term]
end
sundar - Restablecer a Monica
fuente