Números de contenção principais (edição rápida)


25

Esta é a sequência A054261

O th número de contenção principal é o número mais baixo que contém os primeiros primos números como subsequências. Por exemplo, o número é o número mais baixo que contém os 3 primeiros números primos como substrings, tornando-o o terceiro número de contenção principal.nn235

É trivial descobrir que os quatro primeiros números de contenção primos são , , e , mas depois fica mais interessante. Como o próximo primo é 11, o próximo número de contenção do primo não é , mas é pois é definido como o menor número com a propriedade.2232352357235711112357

No entanto, o verdadeiro desafio surge quando você ultrapassa 11. O próximo número de contenção principal é . Observe que, neste número, as substrings e estão sobrepostas. O número também se sobrepõe ao número .1132571113313

É fácil provar que essa sequência está aumentando, pois o próximo número precisa atender a todos os critérios do número anterior e ter mais uma substring. No entanto, a sequência não está aumentando estritamente, como é mostrado pelos resultados para n=10e n=11.

Desafio

Seu objetivo é encontrar o maior número possível de contenção primária. Seu programa deve produzi-los de maneira ordenada, começando com 2 e subindo.

Regras

  1. Você tem permissão para codificar números primos.
  2. Você não tem permissão para codificar números de contenção primária ( 2é a única exceção) ou qualquer número mágico que torne o desafio trivial. Por favor seja gentil.
  3. Você pode usar qualquer idioma que desejar. Inclua uma lista de comandos para preparar o ambiente para a execução do código.
  4. Você é livre para usar CPU e GPU e pode usar multithreading.

Pontuação

A pontuação oficial será no meu laptop (dell XPS 9560). Seu objetivo é gerar o maior número possível de contenção possível dentro de 5 minutos.

Especificações

  • Intel Core i7-7700HQ de 2,8 GHz (aumento de 3,8 GHz) 4 núcleos, 8 threads.
  • 16GB 2400MHz DDR4 RAM
  • NVIDIA GTX 1050
  • Linux Mint 18.3 de 64 bits

Os números encontrados até o momento, juntamente com o último primo adicionado ao número:

 1 =>                                                       2 (  2)
 2 =>                                                      23 (  3)
 3 =>                                                     235 (  5)
 4 =>                                                    2357 (  7)
 5 =>                                                  112357 ( 11)
 6 =>                                                  113257 ( 13)
 7 =>                                                 1131725 ( 17)
 8 =>                                               113171925 ( 19)
 9 =>                                              1131719235 ( 23)
10 =>                                            113171923295 ( 29)
11 =>                                            113171923295 ( 31)
12 =>                                           1131719237295 ( 37)
13 =>                                          11317237294195 ( 41)
14 =>                                        1131723294194375 ( 43)
15 =>                                      113172329419437475 ( 47)
16 =>                                     1131723294194347537 ( 53)
17 =>                                   113172329419434753759 ( 59)
18 =>                                  2311329417434753759619 ( 61)
19 =>                                231132941743475375961967 ( 67)
20 =>                               2311294134347175375961967 ( 71)
21 =>                              23112941343471735375961967 ( 73)
22 =>                             231129413434717353759619679 ( 79)
23 =>                           23112941343471735359619678379 ( 83)
24 =>                         2311294134347173535961967837989 ( 89)
25 =>                        23112941343471735359619678378979 ( 97)
26 =>                      2310112941343471735359619678378979 (101)
27 =>                    231010329411343471735359619678378979 (103)
28 =>                 101031071132329417343475359619678378979 (107)
29 =>              101031071091132329417343475359619678378979 (109)
30 =>              101031071091132329417343475359619678378979 (113)
31 =>           101031071091131272329417343475359619678378979 (127)
32 =>           101031071091131272329417343475359619678378979 (131)
33 =>         10103107109113127137232941734347535961967838979 (137)
34 =>      10103107109113127137139232941734347535961967838979 (139)
35 =>   10103107109113127137139149232941734347535961967838979 (149)
36 => 1010310710911312713713914923294151734347535961967838979 (151)

Agradecemos a Ardnauld, Ourous e japh por estender esta lista.

Observe que n = 10e n = 11são o mesmo número, pois 113171923295 é o número mais baixo que contém todos os números [2,3,5,7,11,13,17,19,23,29] , mas também contém 31 .

Para referência, você pode usar o fato de que o script Python original que escrevi para gerar esta lista acima calcula os 12 primeiros termos em cerca de 6 minutos.

Regras adicionais

Após os primeiros resultados, percebi que há uma boa chance de que os melhores resultados possam ter a mesma pontuação. No caso de empate, o vencedor será o que tiver menos tempo para gerar seu resultado. Se duas ou mais respostas produzirem resultados igualmente rápidos, será simplesmente uma vitória empatada.

Nota final

O tempo de execução de 5 minutos é colocado apenas para garantir uma pontuação justa. Eu ficaria muito interessado em ver se podemos avançar ainda mais na sequência OEIS (agora ela contém 17 números). Com o código de Ourous, gerei todos os números até n = 26, mas pretendo deixar o código funcionar por um longo período de tempo.

Placar

  1. Python 3 + Ferramentas OR do Google : 169
  2. Scala : 137 (não oficial)
  3. Solucionador Concorde TSP : 84 (não oficial)
  4. Conjunto C ++ (GCC) + x86 : 62
  5. Limpo : 25
  6. JavaScript (Node.js) : 24

1
Recentemente, mudei para o driver nouveau, em vez do driver da nvidia, devido à horrível limitação da CPU ao usar a nvidia. Se alguém enviar uma solução otimizada para cuda, não poderei testá-la imediatamente, mas tentarei testá-la dentro de um período de tempo razoável.
maxb

em relação à regra 2: e se, em vez de codificarmos n, codificarmos n-1 e começarmos a pesquisar a partir daí? :)
ngn

@ngn Talvez eu precise especificar um pouco mais o que é permitido. Obviamente, você pode salvar o resultado anterior, o que torna a localização n=11trivial, pois você só precisa verificar se n=10também satisfaz a nova condição. Eu também argumentaria que a codificação embutida só ajuda até n=17, pois nenhum número é conhecido além desse ponto, até onde pude descobrir.
maxb

eu quis dizer codificação [1,22,234,2356,112356,113256,1131724,113171924,1131719234,113171923294,113171923294,1131719237294]e iniciar uma pesquisa de cada um
ngn

4
Até onde eu sei, esse é apenas um caso especial do menor problema comum de supercordas, e que já é conhecido por ser NP-completo, então esse é basicamente um caso de evitar ineficiência.
Neil

Respostas:


9

Python 3 + Google OR-Tools , pontuação 169 em 295 segundos (pontuação oficial)

Como funciona

Após descartar números primos redundantes contidos em outros números primos, desenhe um gráfico direcionado com uma aresta de cada primo para cada um de seus sufixos, com distância zero e uma aresta para cada primo de cada um de seus prefixos, com distância definida pelo número de dígitos adicionados . Buscamos o primeiro caminho lexicograficamente mais curto através do gráfico, começando no prefixo vazio, passando por cada primo (mas não necessariamente por cada prefixo ou sufixo) e terminando no sufixo vazio.

Por exemplo, aqui estão as arestas do caminho ideal ε → 11 → 1 → 13 → 3 → 31 → 1 → 17 → ε → 19 → ε → 23 → ε → 29 → ε → 5 → ε para n = 11, correspondente para a sequência de saída 113171923295.

gráfico

Comparado à redução direta ao problema do vendedor ambulante , observe que, ao conectar os primos indiretamente por esses nós de sufixo / prefixo extras, em vez de diretamente um ao outro, reduzimos drasticamente o número de arestas que precisamos considerar. Mas como os nós extras não precisam ser atravessados ​​exatamente uma vez, isso não é mais uma instância do TSP.

Usamos o solucionador de restrições incremental CP-SAT do Google OR-Tools, primeiro para minimizar o comprimento total do caminho e, em seguida, para minimizar cada grupo de dígitos adicionados em ordem. Inicializamos o modelo apenas com restrições locais: cada primo precede um sufixo e sucede um prefixo, enquanto cada sufixo / prefixo precede e sucede o mesmo número de primos. O modelo resultante pode conter ciclos desconectados; nesse caso, adicionamos restrições de conectividade adicionais dinamicamente e executamos novamente o solucionador.

Código

import multiprocessing
from ortools.sat.python import cp_model


def superstring(strings):
    def gen_prefixes(s):
        for i in range(len(s)):
            a = s[:i]
            if a in affixes:
                yield a

    def gen_suffixes(s):
        for i in range(1, len(s) + 1):
            a = s[i:]
            if a in affixes:
                yield a

    def solve():
        def find_string(s):
            found_strings.add(s)
            for i in range(1, len(s) + 1):
                a = s[i:]
                if (
                    a in affixes
                    and a not in found_affixes
                    and solver.Value(suffix[s, a])
                ):
                    found_affixes.add(a)
                    q.append(a)
                    break

        def cut(skip):
            model.AddBoolOr(
                skip
                + [
                    suffix[s, a]
                    for s in found_strings
                    for a in gen_suffixes(s)
                    if a not in found_affixes
                ]
                + [
                    prefix[a, s]
                    for s in unused_strings
                    if s not in found_strings
                    for a in gen_prefixes(s)
                    if a in found_affixes
                ]
            )
            model.AddBoolOr(
                skip
                + [
                    suffix[s, a]
                    for s in unused_strings
                    if s not in found_strings
                    for a in gen_suffixes(s)
                    if a in found_affixes
                ]
                + [
                    prefix[a, s]
                    for s in found_strings
                    for a in gen_prefixes(s)
                    if a not in found_affixes
                ]
            )

        def search():
            while q:
                a = q.pop()
                for s in prefixed[a]:
                    if (
                        s in unused_strings
                        and s not in found_strings
                        and solver.Value(prefix[a, s])
                    ):
                        find_string(s)
            return not (unused_strings - found_strings)

        while True:
            if solver.Solve(model) != cp_model.OPTIMAL:
                raise RuntimeError("Solve failed")

            found_strings = set()
            found_affixes = set()
            if part is None:
                found_affixes.add("")
                q = [""]
            else:
                part_ix = solver.Value(part)
                p, next_affix, next_string = parts[part_ix]
                q = []
                find_string(next_string)
            if search():
                break

            if part is not None:
                if part_ix not in partb:
                    partb[part_ix] = model.NewBoolVar("partb%s_%s" % (step, part_ix))
                    model.Add(part == part_ix).OnlyEnforceIf(partb[part_ix])
                    model.Add(part != part_ix).OnlyEnforceIf(partb[part_ix].Not())
                cut([partb[part_ix].Not()])
                if last_string is None:
                    found_affixes.add(next_affix)
                else:
                    find_string(last_string)
                q.append(next_affix)
                if search():
                    continue

            cut([])

    solver = cp_model.CpSolver()
    solver.parameters.num_search_workers = 4
    affixes = {s[:i] for s in strings for i in range(len(s))} & {
        s[i:] for s in strings for i in range(1, len(s) + 1)
    }
    prefixed = {}
    for s in strings:
        for a in gen_prefixes(s):
            prefixed.setdefault(a, []).append(s)
    suffixed = {}
    for s in strings:
        for a in gen_suffixes(s):
            suffixed.setdefault(a, []).append(s)
    unused_strings = set(strings)
    last_string = None
    part = None

    model = cp_model.CpModel()
    prefix = {
        (a, s): model.NewBoolVar("prefix_%s_%s" % (a, s))
        for a in affixes
        for s in prefixed[a]
    }
    suffix = {
        (s, a): model.NewBoolVar("suffix_%s_%s" % (s, a))
        for a in affixes
        for s in suffixed[a]
    }
    for s in strings:
        model.Add(sum(prefix[a, s] for a in gen_prefixes(s)) == 1)
        model.Add(sum(suffix[s, a] for a in gen_suffixes(s)) == 1)
    for a in affixes:
        model.Add(
            sum(suffix[s, a] for s in suffixed[a])
            == sum(prefix[a, s] for s in prefixed[a])
        )

    length = sum(prefix[a, s] * (len(s) - len(a)) for a in affixes for s in prefixed[a])
    model.Minimize(length)
    solve()
    model.Add(length == solver.Value(length))

    out = ""
    for step in range(len(strings)):
        in_parts = set()
        parts = []
        for a in [""] if last_string is None else gen_suffixes(last_string):
            for s in prefixed[a]:
                if s in unused_strings and s not in in_parts:
                    in_parts.add(s)
                    parts.append((s[len(a) :], a, s))
        parts.sort()
        part = model.NewIntVar(0, len(parts) - 1, "part%s" % step)
        partb = {}
        for part_ix, (p, a, s) in enumerate(parts):
            if last_string is not None:
                model.Add(part != part_ix).OnlyEnforceIf(suffix[last_string, a].Not())
            model.Add(part != part_ix).OnlyEnforceIf(prefix[a, s].Not())
        model.Minimize(part)
        solve()
        part_ix = solver.Value(part)
        model.Add(part == part_ix)
        p, a, last_string = parts[part_ix]
        unused_strings.remove(last_string)
        out += p
    return out


def gen_primes():
    yield 2
    n = 3
    d = {}
    for p in gen_primes():
        p2 = p * p
        d[p2] = 2 * p
        while n <= p2:
            if n in d:
                q = d.pop(n)
                m = n + q
                while m in d:
                    m += q
                d[m] = q
            else:
                yield n
            n += 2


def gen_inputs():
    num_primes = 0
    strings = []

    for new_prime in gen_primes():
        num_primes += 1
        new_string = str(new_prime)
        strings = [s for s in strings if s not in new_string] + [new_string]
        yield strings


with multiprocessing.Pool() as pool:
    for i, out in enumerate(pool.imap(superstring, gen_inputs())):
        print(i + 1, out, flush=True)

Resultados

Aqui estão os primeiros 1000 números de contenção primária , calculados em 1 ½ dias em um sistema de 8 núcleos / 16 threads.


Solução fantástica! Usar as especificidades do problema de uma maneira inteligente é exatamente o que eu queria das respostas a esta pergunta. Eu o executei no meu laptop agora para obter uma pontuação não oficial e cheguei a 153 em 5 minutos. Darei a você sua pontuação oficial ainda hoje e verifique se sua saída parece correta. Parece que você está na liderança, parabéns!
12/12/18

Confirmei os resultados do @ AndersKaseorg em até 1000 com o solucionador baseado em Concorde (cerca de 5 vezes mais lento!). Decidi verificar novamente porque os dois solucionadores parecem usar LP de ponto flutuante internamente e vi o Concorde abortar algumas vezes devido a erros de arredondamento.
japh

Sei que é um pouco tarde, mas finalmente decidi enviar os resultados para o OEIS. Desde que você foi o vencedor do desafio, você quer ser creditado como o descobridor dos novos números?
maxb

@ maxb Parece bom para mim, obrigado!
Anders Kaseorg

14

Montagem C ++ (GCC) + x86, pontuação 32 36 62 em 259 segundos (oficial)

Resultados calculados até o momento. Meu computador fica sem memória depois 65.

1 2
2 23
3 235
4 2357
5 112357
6 113257
7 1131725
8 113171925
9 1131719235
10 113171923295
11 113171923295
12 1131719237295
13 11317237294195
14 1131723294194375
15 113172329419437475
16 1131723294194347537
17 113172329419434753759
18 2311329417434753759619
19 231132941743475375961967
20 2311294134347175375961967
21 23112941343471735375961967
22 231129413434717353759619679
23 23112941343471735359619678379
24 2311294134347173535961967837989
25 23112941343471735359619678378979
26 2310112941343471735359619678378979
27 231010329411343471735359619678378979
28 101031071132329417343475359619678378979
29 101031071091132329417343475359619678378979
30 101031071091132329417343475359619678378979
31 101031071091131272329417343475359619678378979
32 101031071091131272329417343475359619678378979
33 10103107109113127137232941734347535961967838979
34 10103107109113127137139232941734347535961967838979
35 10103107109113127137139149232941734347535961967838979
36 1010310710911312713713914923294151734347535961967838979
37 1010310710911312713713914915157232941734347535961967838979
38 1010310710911312713713914915157163232941734347535961967838979
39 10103107109113127137139149151571631672329417343475359619798389
40 10103107109113127137139149151571631672329417343475359619798389
41 1010310710911312713713914915157163167173232941794347535961978389
42 101031071091131271371391491515716316717323294179434753596181978389
43 101031071091131271371391491515716316723294173434753596181917978389
44 101031071091131271371391491515716316717323294179434753596181919383897
45 10103107109113127137139149151571631671731792329418191934347535961978389
46 10103107109113127137139149151571631671731791819193232941974347535961998389
47 101031071091271313714915157163167173179181919321139232941974347535961998389
48 1010310710912713137149151571631671731791819193211392232941974347535961998389
49 1010310710912713137149151571631671731791819193211392232272941974347535961998389
50 10103107109127131371491515716316717317918191932113922322722941974347535961998389
51 101031071091271313714915157163167173179181919321139223322722941974347535961998389
52 101031071091271313714915157163167173179181919321139223322722923941974347535961998389
53 1010310710912713137149151571631671731791819193211392233227229239241974347535961998389
54 101031071091271313714915157163167173179211392233227229239241819193251974347535961998389
55 101031071091271313714915157163167173179211392233227229239241819193251972574347535961998389
56 101031071091271313714915157163167173179211392233227229239241819193251972572634347535961998389
57 101031071091271313714915157163167173179211392233227229239241819193251972572632694347535961998389
58 101031071091271313714915157163167173179211392233227229239241819193251972572632694347535961998389
59 1010310710912713137149151571631671731792113922332277229239241819193251972572632694347535961998389
60 101031071091271313714915157163167173211392233227722923924179251819193257263269281974347535961998389
61 1010310710912713137149151571631671732113922332277229239241792518191932572632692819728343475359619989
62 10103107109127131371491515716316717321139223322772293239241792518191932572632692819728343475359619989
63 1010307107109127131371491515716316717321139223322772293239241792518191932572632692819728343475359619989
64 10103071071091271311371391491515716316721173223322772293239241792518191932572632692819728343475359619989
65 10103071071091271311371491515716313916721173223322772293239241792518191932572632692819728343475359619989

Todos eles concordam com a saída do solucionador baseado em Concorde , portanto, eles têm uma boa chance de estarem corretos.

Changelog:

  • Cálculo incorreto para o comprimento de contexto necessário. A versão anterior era 1 muito grande e também apresentava um erro. Pontuação: 32 34

  • Adicionado otimização de grupo de contexto igual. Pontuação: 34 36

  • Revisão do algoritmo para usar seqüências livres de contexto corretamente, além de outras otimizações. Pontuação: 36 62

  • Adicionada uma redação adequada.

  • Adicionada a variante dos números primos.

Como funciona

Aviso: este é um despejo de cérebro. Role até o fim, se você quiser apenas o código.

Abreviações:

Este programa basicamente usa o algoritmo de programação dinâmica do livro didático para o TSP.

  1. Mais uma redução do PCN / SCS, o problema que estamos realmente resolvendo, para o TSP.
  2. Além disso, use contextos de item em vez de todos os dígitos em cada item.
  3. Além disso, subdividir o problema com base em números primos que não podem se sobrepor às extremidades de outros números primos.
  4. Além disso, mescla cálculos para números primos com os mesmos dígitos de início / fim.
  5. Além de tabelas de pesquisa pré-computadas e uma tabela de hash personalizada.
  6. Além de algumas pré-buscas e pacotes de bits de baixo nível.

São muitos bugs em potencial. Depois de brincar com a entrada de anselm e não conseguir obter resultados errados, devo pelo menos provar que minha abordagem geral está correta.

Embora a solução baseada no Concorde seja (muito, muito) mais rápida, ela se baseia na mesma redução, portanto, essa explicação se aplica a ambos. Além disso, esta solução pode ser adaptada para OEIS A054260 , a sequência de primos contendo primos; Não sei como resolver isso com eficiência na estrutura do TSP. Portanto, ainda é um pouco relevante.

Redução de TSP

Vamos começar provando que a redução ao TSP está correta. Temos um conjunto de strings, digamos

A = 13, 31, 37, 113, 137, 211

e queremos encontrar a menor supercorda que contém esses itens.

Saber o comprimento é suficiente

Para o PCN, se houver várias seqüências menores, precisamos retornar a menor lexicograficamente. Mas veremos um problema diferente (e mais fácil).

  • SCS : dado um prefixo inicial e um conjunto de itens, localize qualquer sequência mais curta que contenha todos os itens como substrings e comece com esse prefixo.
  • Comprimento do SCS : Basta encontrar o comprimento do SCS.

Se conseguirmos resolver o comprimento do SCS, podemos reconstruir a menor solução e obter o PCN. Se sabemos que a menor solução começa com o nosso prefixo, tentamos estendê-lo anexando cada item, em ordem lexicográfica, e resolvendo o tamanho novamente. Quando encontrarmos o item menor para o qual o comprimento da solução é o mesmo, sabemos que esse deve ser o próximo item na menor solução (por quê?); Portanto, adicione-o e recorra aos itens restantes. Esse método de alcançar a solução é chamado de auto-redução .

Percorrer o gráfico de sobreposição máxima

Suponha que começamos a resolver o SCS para o exemplo acima manualmente. Provavelmente:

  • Livre-se 13e 37, porque eles já são substrings dos outros itens. Qualquer solução que contenha 137, por exemplo, também deve conter 13e 37.
  • Comece considerando as combinações 113,137 → 1137, 211,113 → 2113etc.

De fato, é a coisa certa a fazer, mas vamos provar isso por uma questão de perfeição. Pegue qualquer solução SCS; por exemplo, uma supercorda mais curta Aé

2113137

e pode ser decomposto em uma concatenação de todos os itens em A:

211
 113
   31
    137

(Ignoramos os itens redundantes 13, 37.) Observe o seguinte:

  1. As posições inicial e final de cada item aumentam em pelo menos 1.
  2. Cada item é sobreposto ao item anterior na maior extensão possível.

Mostraremos que todas as supercordas menores podem ser decompostas desta maneira:

  1. Para cada par de itens adjacentes x,y, yinicia e termina em posições posteriores a x. Se isso não for verdade, xé uma substring you vice-versa. Mas já removemos todos os itens que são substrings, para que isso não possa acontecer.

  2. Suponha que itens adjacentes na sequência tenham sobreposição menor que a máxima, por exemplo, em 21113vez de 2113. Mas isso tornaria o 1redundante extra . Nenhum item posterior precisa da inicial 1(como em 2 1 113), porque ocorre antes 113e todos os itens que aparecem depois 113não podem começar com um dígito antes.113 (consulte o ponto 1). Um argumento semelhante impede que o último extra 1(como em 211 1 3) seja usado por qualquer item anterior 211. Mas nossa supercorda mais curta , por definição, não terá dígitos redundantes; portanto, essas sobreposições não máximas não ocorrerão.

Com essas propriedades, podemos converter qualquer problema do SCS em um TSP:

  1. Remova todos os itens que são substrings de outros itens.
  2. Crie um gráfico direcionado que tenha um vértice para cada item.
  3. Para cada par de itens x, y, adicionar uma aresta xda ycujo peso é o número de símbolos extra adicionado anexando ypara xcom sobreposição máxima. Por exemplo, adicionaríamos uma aresta de 211a 113com o peso 1, porque2113 adiciona mais um dígito 211. Repita para a borda de yaté x.
  4. Adicione um vértice para o prefixo inicial e as arestas a todos os outros itens.

Qualquer caminho neste gráfico, a partir do prefixo inicial, corresponde a uma concatenação de sobreposição máxima de todos os itens desse caminho, e o peso total do caminho é igual ao comprimento da sequência concatenada. Portanto, todo passeio de menor peso, que visita todos os itens pelo menos uma vez, corresponde a uma supercorda mais curta.

E essa é a redução de SCS (e comprimento SCS) para TSP.

Algoritmo de programação dinâmica

Este é um algoritmo clássico, mas vamos modificá-lo um pouco, então aqui está um lembrete rápido.

(Eu escrevi isso como um algoritmo para o comprimento do SCS em vez do TSP. Eles são essencialmente equivalentes, mas o vocabulário do SCS ajuda quando chegamos às otimizações específicas do SCS.)

Chame o conjunto de itens de entrada Ae o prefixo fornecido P. Para cada ksubconjunto -element Sem Ae todo elemento ede S, calculamos o comprimento da string mais curta que começa com P, contém todos Se termina com e. Isso envolve armazenar uma tabela a partir dos valores de(S, e) para seus comprimentos de SCS.

Quando chegamos a cada subconjunto S, a tabela já precisa conter os resultados S - {e}para todos eem S. Como a tabela pode ficar muito grande, eu calcular os resultados para todos os ksubconjuntos -element, então k+1, etc. Para isso, precisamos apenas para armazenar os resultados para ke k+1em qualquer momento um. Isso reduz o uso de memória em um fator de aproximadamentesqrt(|A|) .

Mais um detalhe: em vez de calcular o comprimento mínimo do SCS, na verdade eu calculo a sobreposição total máxima entre os itens. (Para obter o comprimento do SCS, basta subtrair a sobreposição total da soma dos comprimentos dos itens.) O uso de sobreposições ajuda a algumas das otimizações a seguir.

[2.] Contextos de itens

Um contexto é o sufixo mais longo de um item que pode se sobrepor aos itens a seguir. Se nossos itens forem 113,211,311, então 11é o contexto para 211e 311. (Também é o contexto do prefixo 113, que veremos na parte [4.])

No algoritmo DP acima, acompanhamos as soluções de SCS que terminam com cada item, mas na verdade não nos importamos com qual item um SCS termina. Tudo o que precisamos saber é o contexto. Assim, por exemplo, se dois SCSs para o mesmo conjunto terminarem em , 23e 43qualquer SCS que continuar de um também funcionará para o outro.

Essa é uma otimização significativa, porque os números primos não triviais terminam apenas nos dígitos 1 3 7 9. Os quatro contextos de um dígito 1,3,7,9(mais o contexto vazio) são de fato suficientes para calcular os PCNs para números primos de até 131.

[3.] Itens sem contexto

Outros já apontaram que muitos números primos começam com os dígitos 2,4,5,6,8, como 23,29,41,43.... Nada disso pode se sobrepor a um primo anterior (além de 2e 5, primos não podem terminar com esses dígitos; 2e 5já foram removidos como redundantes). No código, eles são chamados de seqüências livres de contexto .

Se nossa entrada tiver itens livres de contexto, todas as soluções SCS poderão ser divididas em blocos

<prefix>... 23... 29... 41... 43...

e as sobreposições em cada bloco são independentes dos outros blocos. Podemos embaralhar os blocos ou trocar itens entre os blocos que têm o mesmo contexto, sem alterar o comprimento do SCS.

Portanto, precisamos apenas acompanhar os possíveis vários conjuntos de contextos, um para cada bloco.

Exemplo completo: para os primos menores que 100, temos 11 itens sem contexto e seus contextos:

23 29 41 43 47 53 59 61 67 83 89
 3  9  1  3  7  3  9  1  7  3  9

Nosso contexto multiset inicial:

1 1 3 3 3 3 7 7 9 9 9

O código se refere a eles como contextos combinados ou ccontexts . Então, precisamos considerar apenas subconjuntos dos itens restantes:

11 13 17 19 31 37 71 73 79 97

[4.] Mesclagem de contexto

Quando chegamos aos números primos com 3 dígitos ou mais, há mais redundâncias:

 101 151 181 191 ...
 107 127 157 167 197 ...
 109 149 1009 ...

Esses grupos compartilham os mesmos contextos inicial e final (geralmente - depende de quais outros números primos estão na entrada); portanto, eles são indistinguíveis ao se sobrepor a outros itens. Só nos preocupamos com sobreposições, para que possamos tratar os números primos nesses grupos de contexto igual como indistinguíveis. Agora, nossos subconjuntos DP são condensados ​​em multisubsets

4 × 1_1
5 × 1_7
3 × 1_9

(É também por isso que o solucionador maximiza o comprimento da sobreposição em vez de minimizar o comprimento do SCS: essa otimização preserva o comprimento da sobreposição.)

Resumo: as otimizações de alto nível

A execução com INFOsaída de depuração imprimirá estatísticas como

solve: N=43, N_search=26, ccontext_size=18, #contexts=7, #eq_context_groups=16

Esta linha específica é para o comprimento de SCS dos primeiros 62 primos, 2para 293.

  • Depois de remover itens redundantes, ficamos com 43 primos que não são substrings um do outro.
  • Existem 7 contextos únicos :1,3,7,11,13,27 mais a sequência vazia.
  • 17 dos 43 primos são livres de contexto : 43,47,53,59,61,89,211,223,227,229,241,251,257,263,269,281,283. Esses e o prefixo fornecido (nesse caso, cadeia vazia) formam a base do contexto combinado inicial .
  • Nos 26 itens restantes ( N_search), existem 16 grupos de contexto igual não triviais .

Ao explorar essas estruturas, o cálculo do comprimento do SCS precisa apenas verificar 8498336 (multiset, ccontext)combinações. A programação dinâmica direta tomaria 43×2^43 > 3×10^14medidas, e a força bruta das permutações tomaria 6×10^52medidas. O programa ainda precisa executar o SCS-Length várias vezes para reconstruir a solução PCN, mas isso não leva muito mais tempo.

[5., 6.] As otimizações de baixo nível

Em vez de executar operações de cadeia, o solucionador de comprimento de SCS trabalha com índices de itens e contextos. Também pré-calculo a quantidade de sobreposição entre cada contexto e par de itens.

O código usava inicialmente os GCCs unordered_map, que parecem ser uma tabela de hash com intervalos de lista vinculada e tamanhos de hash principais (ou seja, divisões caras). Então, escrevi minha própria tabela de hash com sondagem linear e potência de dois tamanhos. Isso permite uma aceleração de 3x e redução de 3x na memória.

Cada estado da tabela consiste em um conjunto múltiplo de itens, um contexto combinado e uma contagem de sobreposições. Eles são compactados em entradas de 128 bits: 8 para a contagem de sobreposição, 56 para o multiset (como um conjunto de bits com codificação de comprimento de execução) e 64 para o ccontext (RLE delimitado por 1). Codificar e decodificar o ccontext foi a parte mais complicada e acabei usando o novoPDEP instrução (é tão nova que o GCC ainda não tem uma intrínseca para ela).

Finalmente, o acesso a uma tabela de hash é muito lento quando Nfica grande, porque a tabela não se encaixa mais no cache. Mas a única razão pela qual escrevemos na tabela de hash é atualizar a contagem de sobreposições mais conhecida para cada estado. O programa divide essa etapa em uma fila de pré-busca e o loop interno pré-busca cada tabela de algumas iterações antes de realmente atualizar esse slot. Outra aceleração 2 × no meu computador.

Bônus: novas melhorias

AKA Como o Concorde é tão rápido?

Eu não sei muito sobre algoritmos TSP, então aqui está um palpite.

O Concorde usa o método de ramificação e corte para resolver TSPs.

  • Codifica o TSP como um programa linear inteiro
  • Ele usa métodos de programação linear, bem como heurísticas iniciais, para obter limites inferior e superior na distância ideal do passeio
  • Esses limites são então alimentados em um algoritmo recursivo ramificado e vinculado que procura a solução ideal. Grandes partes da árvore de pesquisa podem ser removidas, se o limite inferior calculado para uma subárvore exceder um limite superior conhecido
  • Também procura planos de corte para aumentar o relaxamento do LP e obter melhores limites. Normalmente, esses cortes codificam o conhecimento de que as variáveis ​​de decisão devem ser números inteiros

Idéias óbvias que poderíamos tentar:

  • Poda no solucionador de comprimento de SCS, especialmente ao reconstruir a solução PCN (nesse ponto, já sabemos qual é o comprimento da solução)
  • Derivando limites inferiores fáceis de calcular para o SCS, que podem ser usados ​​para ajudar na remoção
  • Encontrar mais simetrias ou redundâncias na distribuição de números primos para explorar

No entanto, a combinação de ramificação e corte é muito poderosa, portanto, talvez não consigamos vencer um solucionador de última geração como o Concorde, para grandes valores de N.

Bônus de bônus: os primos de contenção principais

Diferentemente da solução baseada no Concorde, este programa pode ser modificado para encontrar os menores números primos que contêm ( OEIS A054260 ). Isso envolve três alterações:

  1. 1/ln(n)

  2. Modifique o código do solucionador de comprimento do SCS para categorizar as soluções com base em se as somas de dígitos são divisíveis por 3. Isso envolve adicionar outra entrada, a soma dos dígitos mod 3, a cada estado DP. Isso reduz bastante as chances de o solucionador principal ficar preso com permutações não principais. Esta é a mudança que eu não consegui descobrir como traduzir para o TSP. Ele pode ser codificado com ILP, mas então eu teria que aprender sobre isso chamado “desigualdade de subtore” e como gerá-los.

  3. Pode ser que todos os PCNs mais curtos sejam divisíveis por 3. Nesse caso, o menor prime de contenção primária deve ter pelo menos um dígito a mais que o PCN. Se o nosso solucionador de comprimento de SCS detectar isso, o código de reconstrução da solução terá a opção de adicionar um dígito extra em qualquer ponto do processo. Ele tenta adicionar cada dígito possível 0..9e cada item restante ao prefixo da solução atual, em ordem lexicográfica como antes.

Com essas mudanças, posso obter as soluções até N=62. Exceto por 47onde o código de reconstrução fica preso e desiste após 1 milhão de etapas (ainda não sei por que). Os primos de contenção principal são:

1 2
2 23
3 523
4 2357
5 112573
6 511327
7 1135217
8 1113251719
9 11171323519
10 113171952923
11 113171952923
12 11131951723729
13 11317237419529
14 1131723294375419
15 113172329541947437
16 1131723294195343747
17 1113172329419434753759
18 11231329417437475361959
19 231132941743475375967619
20 2311294134347175967619537
21 23112941343471735967619537
22 231129413434717359537679619
23 23112941343471735375961983679
24 11231294134347173535961967983789
25 23112941343471735359679837619789
26 2310112941343471735359619783789679
27 231010329411343471735359619678379897
28 101031071132329417343475359619798376789
29 101031071091132329417343475359619767898379
30 101031071091132329417343475359619767898379
31 1010310710911131272329417343475359619678979837
32 1010310710911131272329417343475359619678979837
33 10103107109113127137232941734347535978961967983
34 10103107109113127137139232941734347535961967838979
35 10103107109113127137139149232941734347535961976798389
36 1010310710911312713713914923294151734347535976198389679
37 1010310710911312713713914915157232941734347535967619798389
38 10103107109111312713713914915157163232941734347535967897961983
39 10103107109113127137139149151571631672329417343475961979838953
40 10103107109113127137139149151571631672329417343475961979838953
41 10103107109111312713713914915157163167173232941794347535976198983
42 1010310710911131271371391491515716316717323294179434761819535989783
43 1010310710911131271371391491515716316723294173434753596181917989783
44 101031071091131271371391491515716316717323294179434753836181919389597
45 10103107109113127137139149151571631671731792329418191934347538961975983
46 101031071091113127137139149151571631671731791819193232941974347535989836199
47 (failed)
48 1010310710912713137149151571631671731791819193211392232941974347895359836199
49 10103107109112713137149151571631671731791819193211392232272941974347619983535989
50 10103107109127131371491515716316717317918191932113922322722941974347595389836199
51 101031071091271313714915157163167173179181919321139223322722941974347595389619983
52 101031071091271313714915157163167173179181919321139223322722923941974347538361995989
53 10103107109112713137149151571631671731791819193211392233227229239241974347619983538959
54 101031071091271313714915157163167173179211392233227229239241819193251974347619953835989
55 1010310710911271313714915157163167173179211392233227229239241819193251974325747596199538983
56 101031071091271313714915157163167173179211392233227229239241819193251972572634347619959895383
57 101031071091271313714915157163167173179211392233227229239241819193251972572632694359538983619947
58 101031071091271313714915157163167173179211392233227229239241819193251972572632694359538983619947
59 1010310710912713137149151571631671731792113922332277229239241819193251972572632694347535983896199
60 1010310710911271313714915157163167173211392233227722923924179251819193257263269281974347535961998389
61 1010310710912713137149151571631671732113922332277229239241792518191932572632692819728343538947619959
62 10103107109127131371491515716316717321139223322772293239241792518191932572632692819728343534759896199

Código

Ajuntar com

g++ -std=c++14 -O3 -march=native pcn.cpp -o pcn

Para a versão do número primo, também vincule ao GMPlib, por exemplo

g++ -std=c++14 -O3 -march=native pcn-prime.cpp -o pcn-prime -lgmp -lgmpxx

Este programa usa a instrução PDEP, disponível apenas nos processadores x86 recentes (Haswell +). Tanto o meu computador como o maxb suportam. Caso contrário, o programa será compilado em uma versão lenta do software. Um aviso de compilação será impresso quando isso acontecer.

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <array>

using namespace std;

void debug_dummy(...) {
}

#ifndef INFO
//#  define INFO(...) fprintf(stderr, __VA_ARGS__)
#  define INFO debug_dummy
#endif

#ifndef DEBUG
//#    define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#  define DEBUG debug_dummy
#endif

bool is_prime(size_t n)
{
    for (size_t d = 2; d * d <= n; ++d) {
        if (n % d == 0) {
            return false;
        }
    }
    return true;
}

// bitset, works for up to 64 strings
using bitset_t = uint64_t;
const size_t bitset_bits = 64;

// Find position of n-th set bit of x
uint64_t bit_select(uint64_t x, size_t n) {
#ifdef __BMI2__
    // Bug: GCC doesn't seem to provide the _pdep_u64 intrinsic,
    // despite what its manual claims. Neither does Clang!
    //size_t r = _pdep_u64(ccontext_t(1) << new_context, ccontext1);
    size_t r;
    // NB: actual operand order is %2, %1 despite the intrinsic taking %1, %2
    asm ("pdep %2, %1, %0"
         : "=r" (r)
         : "r" (uint64_t(1) << n), "r" (x)
         );
    return __builtin_ctzll(r);
#else
#  warning "bit_select: no x86 BMI2 instruction set, falling back to slow code"
    size_t k = 0, m = 0;
    for (; m < 64; ++m) {
        if (x & (uint64_t(1) << m)) {
            if (k == n) {
                break;
            }
            ++k;
        }
    }
    return m;
#endif
}

#ifndef likely
#  define likely(x) __builtin_expect(x, 1)
#endif
#ifndef unlikely
#  define unlikely(x) __builtin_expect(x, 0)
#endif

// Return the shortest string that begins with a and ends with b
string join_strings(string a, string b) {
    for (size_t overlap = min(a.size(), b.size()); overlap > 0; --overlap) {
        if (a.substr(a.size() - overlap) == b.substr(0, overlap)) {
            return a + b.substr(overlap);
        }
    }
    return a + b;
}

vector <string> dedup_items(string context0, vector <string> items)
{
    vector <string> items2;
    for (size_t i = 0; i < items.size(); ++i) {
        bool dup = false;
        if (context0.find(items[i]) != string::npos) {
                dup = true;
        } else {
            for (size_t j = 0; j < items.size(); ++j) {
                if (items[i] == items[j]?
                    i > j
                        : items[j].find(items[i]) != string::npos) {
                    dup = true;
                    break;
                }
            }
        }
        if (!dup) {
            items2.push_back(items[i]);
        }
    }
    return items2;
}

// Table entry used in main solver
const size_t solver_max_item_set = bitset_bits - 8;
struct Solver_entry
{
    uint8_t score : 8;
    bitset_t items : solver_max_item_set;
    bitset_t context;

    Solver_entry()
    {
        score = 0xff;
        items = 0;
        context = 0;
    }
    bool is_empty() const {
        return score == 0xff;
    }
};

// Simple hash table to avoid stdlib overhead
struct Solver_table
{
    vector <Solver_entry> t;
    size_t t_bits;
    size_t size_;
    size_t num_probes_;

    Solver_table()
    {
        // 256 slots initially -- this needs to be not too small
        // so that the load factor formula in update_score works
        t_bits = 8;
        size_ = 0;
        num_probes_ = 0;
        resize(t_bits);
    }
    static size_t entry_hash(bitset_t items, bitset_t context)
    {
        uint64_t h = 0x3141592627182818ULL;
        // Add context first, since its bits are generally
        // less well distributed than items
        h += context;
        h ^= h >> 23;
        h *= 0x2127599bf4325c37ULL;
        h ^= h >> 47;
        h += items;
        h ^= h >> 23;
        h *= 0x2127599bf4325c37ULL;
        h ^= h >> 47;
        return h;
    }
    size_t probe_index(size_t hash) const {
        return hash & ((size_t(1) << t_bits) - 1);
    }
    void resize(size_t t2_bits)
    {
        assert (size_ < size_t(1) << t2_bits);
        vector <Solver_entry> t2(size_t(1) << t2_bits);
        for (auto entry: t) {
            if (!entry.is_empty()) {
                size_t h = entry_hash(entry.items, entry.context);
                size_t mask = (size_t(1) << t2_bits) - 1;
                size_t idx = h & mask;
                while (!t2[idx].is_empty()) {
                    idx = (idx + 1) & mask;
                    ++num_probes_;
                }
                t2[idx] = entry;
            }
        }
        t.swap(t2);
        t_bits = t2_bits;
    }
    uint8_t update_score(bitset_t items, bitset_t context, uint8_t score)
    {
        // Ensure we can insert a new item without resizing
        assert (size_ < t.size());

        size_t index = probe_index(entry_hash(items, context));
        size_t mask = (size_t(1) << t_bits) - 1;
        for (size_t p = 0; p < t.size(); ++p, index = (index + 1) & mask) {
            ++num_probes_;
            if (likely(t[index].items == items && t[index].context == context)) {
                t[index].score = max(t[index].score, score);
                return t[index].score;
            }
            if (t[index].is_empty()) {
                // add entry
                t[index].score = score;
                t[index].items = items;
                t[index].context = context;
                ++size_;
                // load factor 4/5 -- ideally 2-3 average probes per lookup
                if (5*size_ > 4*t.size()) {
                    resize(t_bits + 1);
                }
                return score;
            }
        }
        assert (false && "bug: hash table probe loop");
    }
    size_t size() const {
        return size_;
    }
    void swap(Solver_table table)
    {
        t.swap(table.t);
        ::swap(size_, table.size_);
        ::swap(t_bits, table.t_bits);
        ::swap(num_probes_, table.num_probes_);
    }
};

/*
 * Main solver code.
 */
struct Solver
{
    // Inputs
    vector <string> items;
    string context0;
    size_t context0_index;

    // Mapping between strings and indices
    vector <string> context_to_string;
    unordered_map <string, size_t> string_to_context;

    // Items that have context-free prefixes, i.e. prefixes that
    // never overlap with the end of other items nor context0
    vector <bool> contextfree;

    // Precomputed contexts (suffixes) for each item
    vector <size_t> item_context;
    // Precomputed updates: (context, string) to overlap amount
    vector <vector <size_t>> join_overlap;

    Solver(vector <string> items, string context0)
        :items(items), context0(context0)
    {
        items = dedup_items(context0, items);
        init_context_();
    }

    void init_context_()
    {
        /*
         * Generate all relevant item-item contexts.
         *
         * At this point, we know that no item is a substring of
         * another, nor of context0. This means that the only contexts
         * we need to care about, are those generated from maximal join
         * overlaps between any two items.
         *
         * Proof:
         * Suppose that the shortest containing string needs some other
         * kind of context. Maybe it depends on a context spanning
         * three or more items, say X,Y,Z. But if Z ends after Y and
         * interacts with X, then Y must be a substring of Z.
         * This cannot happen, because we removed all substrings.
         *
         * Alternatively, it depends on a non-maximal join overlap
         * between two strings, say X,Y. But if this overlap does not
         * interact with any other string, then we could maximise it
         * and get a shorter solution. If it does, then call this
         * other string Z. We would get the same contradiction as in
         * the previous case with X,Y,Z.
         */
        size_t N = items.size();
        vector <size_t> max_prefix_overlap(N), max_suffix_overlap(N);
        size_t context0_suffix_overlap = 0;
        for (size_t i = 0; i < N; ++i) {
            for (size_t j = 0; j < N; ++j) {
                if (i == j) continue;
                string joined = join_strings(items[j], items[i]);
                size_t overlap = items[j].size() + items[i].size() - joined.size();
                string context = items[i].substr(0, overlap);
                max_prefix_overlap[i] = max(max_prefix_overlap[i], overlap);
                max_suffix_overlap[j] = max(max_suffix_overlap[j], overlap);

                if (string_to_context.find(context) == string_to_context.end()) {
                    string_to_context[context] = context_to_string.size();
                    context_to_string.push_back(context);
                }
            }

            // Context for initial join with context0
            {
                string joined = join_strings(context0, items[i]);
                size_t overlap = context0.size() + items[i].size() - joined.size();
                string context = items[i].substr(0, overlap);
                max_prefix_overlap[i] = max(max_prefix_overlap[i], overlap);
                context0_suffix_overlap = max(context0_suffix_overlap, overlap);

                if (string_to_context.find(context) == string_to_context.end()) {
                    string_to_context[context] = context_to_string.size();
                    context_to_string.push_back(context);
                }
            }
        }
        // Now compute all canonical trailing contexts
        context0_index = string_to_context[
                           context0.substr(context0.size() - context0_suffix_overlap)];
        item_context.resize(N);
        for (size_t i = 0; i < N; ++i) {
            item_context[i] = string_to_context[
                                items[i].substr(items[i].size() - max_suffix_overlap[i])];
        }

        // Now detect context-free items
        contextfree.resize(N);
        for (size_t i = 0; i < N; ++i) {
            contextfree[i] = (max_prefix_overlap[i] == 0);
            if (contextfree[i]) {
                DEBUG("  contextfree: %s\n", items[i].c_str());
            }
        }

        // Now compute all possible overlap amounts
        join_overlap.resize(context_to_string.size(), vector <size_t> (N));
        for (size_t c_index = 0; c_index < context_to_string.size(); ++c_index) {
            const string& context = context_to_string[c_index];
            for (size_t i = 0; i < N; ++i) {
                string joined = join_strings(context, items[i]);
                size_t overlap = context.size() + items[i].size() - joined.size();
                join_overlap[c_index][i] = overlap;
            }
        }
    }

    // Main solver.
    // Returns length of shortest string containing all items starting
    // from context0 (context0's length not included).
    size_t solve() const
    {
        size_t N = items.size();

        // Length, if joined without overlaps. We try to improve this by
        // finding overlaps in the main iteration
        size_t base_length = 0;
        for (auto s: items) {
            base_length += s.size();
        }

        // Now take non-context-free items. We will only need to search
        // over these items.
        vector <size_t> search_items;
        for (size_t i = 0; i < N; ++i) {
            if (!contextfree[i]) {
                search_items.push_back(i);
            }
        }
        size_t N_search = search_items.size();

        /*
         * Some groups of strings have the same context transitions.
         * For example "17", "107", "127", "167" all have an initial
         * context of "1" and a trailing context of "7", no other
         * overlaps are possible with other primes.
         *
         * We group these strings and treat them as indistinguishable
         * during the main algorithm.
         */
        auto eq_context = [&](size_t i, size_t j) {
            if (item_context[i] != item_context[j]) {
                return false;
            }
            for (size_t ci = 0; ci < context_to_string.size(); ++ci) {
                if (join_overlap[ci][i] != join_overlap[ci][j]) {
                    return false;
                }
            }
            return true;
        };
        vector <size_t> eq_context_group(N_search, size_t(-1));
        for (size_t si = 0; si < N_search; ++si) {
            for (size_t sj = si-1; sj+1 > 0; --sj) {
                size_t i = search_items[si], j = search_items[sj];
                if (!contextfree[j] && eq_context(i, j)) {
                    DEBUG("  eq context: %s =c= %s\n", items[i].c_str(), items[j].c_str());
                    eq_context_group[si] = sj;
                    break;
                }
            }
        }

        // Figure out the combined context size. A combined context has
        // one entry for each context-free item plus one for context0.
        size_t ccontext_size = N - N_search + 1;

        // Assert that various parameters all fit into our data types
        using ccontext_t = bitset_t;
        assert (context_to_string.size() + ccontext_size <= bitset_bits);
        assert (N_search <= solver_max_item_set);
        assert (base_length < 0xff);

        // Initial combined context.
        unordered_map <size_t, size_t> cc0_full;
        ++cc0_full[context0_index];
        for (size_t i = 0; i < N; ++i) {
            if (contextfree[i]) {
                ++cc0_full[item_context[i]];
            }
        }
        // Now pack into unary-encoded bitset. The bitset stores the
        // count for each context as <count> number of 0 bits,
        // followed by a 1 bit.
        ccontext_t cc0 = 0;
        for (size_t ci = 0, b = 0; ci < context_to_string.size(); ++ci, ++b) {
            b += cc0_full[ci];
            cc0 |= ccontext_t(1) << b;
        }

        // Map from (item set, context) to maximum achievable overlap
        Solver_table k_solns;
        // Base case: cc0 with empty set
        k_solns.update_score(0, cc0, 0);

        // Now start dynamic programming. k is current subset size
        size_t eq_context_groups = 0;
        for (size_t g: eq_context_group) eq_context_groups += (g != size_t(-1));
        if (context0.empty()) {
            INFO("solve: N=%zu, N_search=%zu, ccontext_size=%zu, #contexts=%zu, #eq_context_groups=%zu\n",
                 N, N_search, ccontext_size, context_to_string.size(), eq_context_groups);
        } else {
            DEBUG("solve: context=%s, N=%zu, N_search=%zu, ccontext_size=%zu, #contexts=%zu, #eq_context_groups=%zu\n",
                  context0.c_str(), N, N_search, ccontext_size, context_to_string.size(), eq_context_groups);
        }
        for (size_t k = 0; k < N_search; ++k) {
            decltype(k_solns) k1_solns;

            // The main bottleneck of this program is updating k1_solns,
            // which (for larger N) becomes a huge table.
            // We use a prefetch queue to reduce memory latency.
            const size_t prefetch = 8;
            array <Solver_entry, prefetch> entry_queue;
            size_t update_i = 0;

            // Iterate every k-subset
            for (Solver_entry entry: k_solns.t) {
                if (entry.is_empty()) continue;

                bitset_t s = entry.items;
                ccontext_t ccontext = entry.context;
                size_t overlap = entry.score;

                // Try adding a new item
                for (size_t si = 0; si < N_search; ++si) {
                    bitset_t s1 = s | bitset_t(1) << si;
                    if (s == s1) {
                        continue;
                    }
                    // Add items in each eq_context_group sequentially
                    if (eq_context_group[si] != size_t(-1) &&
                        !(s & bitset_t(1) << eq_context_group[si])) {
                        continue;
                    }
                    size_t i = search_items[si]; // actual item index

                    size_t new_context = item_context[i];
                    // Increment ccontext's count for new_context.
                    // We need to find its delimiter 1 bit
                    size_t bit_n = bit_select(ccontext, new_context);
                    ccontext_t ccontext_n =
                        ((ccontext & ((ccontext_t(1) << bit_n) - 1))
                         | ((ccontext >> bit_n << (bit_n + 1))));

                    // Select non-empty sub-contexts to substitute for new_context
                    for (size_t ci = 0, bit1 = 0, count;
                         ci < context_to_string.size();
                         ++ci, bit1 += count + 1)
                    {
                        assert (ccontext_n >> bit1);
                        count = __builtin_ctzll(ccontext_n >> bit1);
                        if (!count
                            // We just added new_context; we can only remove an existing
                            // context entry there i.e. there must be at least two now
                            || (ci == new_context && count < 2)) {
                            continue;
                        }

                        // Decrement ci in ccontext_n
                        bitset_t ccontext1 =
                            ((ccontext_n & ((ccontext_t(1) << bit1) - 1))
                             | ((ccontext_n >> (bit1 + 1)) << bit1));

                        size_t overlap1 = overlap + join_overlap[ci][i];

                        // do previous prefetched update
                        if (update_i >= prefetch) {
                            Solver_entry entry = entry_queue[update_i % prefetch];
                            k1_solns.update_score(entry.items, entry.context, entry.score);
                        }

                        // queue the current update and prefetch
                        Solver_entry entry1;
                        size_t probe_index = k1_solns.probe_index(Solver_table::entry_hash(s1, ccontext1));
                        __builtin_prefetch(&k1_solns.t[probe_index]);
                        entry1.items = s1;
                        entry1.context = ccontext1;
                        entry1.score = overlap1;
                        entry_queue[update_i % prefetch] = entry1;

                        ++update_i;
                    }
                }
            }

            // do remaining queued updates
            for (size_t j = 0; j < min(update_i, prefetch); ++j) {
                Solver_entry entry = entry_queue[j];
                k1_solns.update_score(entry.items, entry.context, entry.score);
            }

            if (context0.empty()) {
                INFO("  hash stats: |solns[%zu]| = %zu, %zu lookups, %zu probes\n",
                     k+1, k1_solns.size(), update_i, k1_solns.num_probes_);
            } else {
                DEBUG("  hash stats: |solns[%zu]| = %zu, %zu lookups, %zu probes\n",
                      k+1, k1_solns.size(), update_i, k1_solns.num_probes_);
            }
            k_solns.swap(k1_solns);
        }

        // Overall solution
        size_t max_overlap = 0;
        for (Solver_entry entry: k_solns.t) {
            if (entry.is_empty()) continue;
            max_overlap = max(max_overlap, size_t(entry.score));
        }
        return base_length - max_overlap;
    }
};

// Wrapper for Solver that also finds the smallest solution string
string smallest_containing_string(vector <string> items)
{
    items = dedup_items("", items);

    size_t soln_length;
    {
        Solver solver(items, "");
        soln_length = solver.solve();
    }
    DEBUG("Found solution length: %zu\n", soln_length);

    string soln;
    vector <string> remaining_items = items;
    while (remaining_items.size() > 1) {
        // Add all possible next items, in lexicographic order
        vector <pair <string, size_t>> next_solns;
        for (size_t i = 0; i < remaining_items.size(); ++i) {
            const string& item = remaining_items[i];
            next_solns.push_back(make_pair(join_strings(soln, item), i));
        }
        assert (next_solns.size() == remaining_items.size());
        sort(next_solns.begin(), next_solns.end());

        // Now try every item in order
        bool found_next = false;
        for (auto ns: next_solns) {
            size_t i;
            string next_soln;
            tie(next_soln, i) = ns;
            DEBUG("Trying: %s + %s -> %s\n",
                  soln.c_str(), remaining_items[i].c_str(), next_soln.c_str());
            vector <string> next_remaining;
            for (size_t j = 0; j < remaining_items.size(); ++j) {
                if (next_soln.find(remaining_items[j]) == string::npos) {
                    next_remaining.push_back(remaining_items[j]);
                }
            }

            Solver solver(next_remaining, next_soln);
            size_t next_size = solver.solve();
            DEBUG("  ... next_size: %zu + %zu =?= %zu\n", next_size, next_soln.size(), soln_length);
            if (next_size + next_soln.size() == soln_length) {
                INFO("  found next item: %s\n", remaining_items[i].c_str());
                soln = next_soln;
                remaining_items = next_remaining;
                // found lexicographically smallest solution, break now
                found_next = true;
                break;
            }
        }
        assert (found_next);
    }
    soln = join_strings(soln, remaining_items[0]);

    return soln;
}

int main()
{
    string prev_soln;
    vector <string> items;
    size_t p = 1;
    for (size_t N = 1;; ++N) {
        for (++p; items.size() < N; ++p) {
            if (is_prime(p)) {
                char buf[99];
                snprintf(buf, sizeof buf, "%zu", p);
                items.push_back(buf);
                break;
            }
        }

        // Try to reuse previous solution (this works for N=11,30,32...)
        string soln;
        if (prev_soln.find(items.back()) != string::npos) {
            soln = prev_soln;
        } else {
            soln = smallest_containing_string(items);
        }
        printf("%s\n", soln.c_str());
        prev_soln = soln;
    }
}

Experimente online!

E a versão exclusiva no TIO . Desculpe, mas não joguei golfe nesses programas e há um limite de tamanho de postagem.


Não relacionado: em vez de debug_dummy, você pode usar #define DEBUG(x) void(0).
user202729

Surpreendente! Eu estava esperando por uma resposta C / C ++. Vou tentar executá-lo o mais rápido possível! Quanta RAM você tem na sua máquina? Tentarei maximizar a quantidade disponível para o seu script quando compará-lo adequadamente.
Max4

usuário: uso debug_dummyporque quero que os argumentos sejam do tipo verificado e avaliado, mesmo quando a depuração estiver desativada.
japh

@maxb: também 16GB. Mas N=32só precisa de cerca de 500 MB, eu acho.
Japh

1
Grande melhoria! Hoje vou correr mais tarde. O código que você colou acima não inclui o main, mas eu o encontrei no link TIO.
maxb

13

JavaScript (Node.js) , pontuação 24 em 241 segundos

Resultados

  • a(1)a(21)
  • a(22)=231129413434717353759619679
  • a(23)=23112941343471735359619678379
  • a(1)a(24)

Algoritmo

Essa é uma pesquisa recursiva que tenta todas as formas possíveis de mesclar números e, eventualmente, classifica as listas resultantes em ordem lexicográfica quando um nó folha é atingido.

xykxkykykx

No início de cada iteração, qualquer entrada que possa ser encontrada em outra entrada é removida da lista.

Foi alcançada uma aceleração significativa acompanhando os nós visitados, para que possamos abortar cedo quando operações diferentes levarem à mesma lista.

Uma pequena aceleração foi alcançada atualizando e restaurando a lista quando possível, em vez de gerar uma cópia, conforme sugerido por um usuário anônimo Neil.

Exemplo

n=7[2,3,5,7,11,13,17]

[]                        // start with an empty list
[ 2 ]                     // append 2
[ 2, 3 ]                  // append 3
[ 2, 3, 5 ]               // append 5
[ 2, 3, 5, 7 ]            // append 7
[ 2, 3, 5, 7, 11 ]        // append 11
[ 2, 3, 5, 7, 11, 13 ]    // append 13
[ 2, 5, 7, 11, 13 ]       // remove 3, which appears in 13
  [ 2, 5, 7, 113, 13 ]    //   try to merge 11 and 13 into 113
  [ 2, 5, 7, 113 ]        //   remove 13, which now appears in 113
  [ 2, 5, 7, 113, 17 ]    //   append 17
  [ 2, 5, 113, 17 ]       //   remove 7, which appears in 17
  --> leaf node: 1131725  //   new best result
[ 2, 5, 7, 11, 13, 17 ]   // append 17
[ 2, 5, 11, 13, 17 ]      // remove 7, which appears in 17
  [ 2, 5, 113, 13, 17 ]   //   try to merge 11 and 13 into 113
  [ 2, 5, 113, 17 ]       //   remove 13, which now appears in 113
                          //   abort because this node was already visited
                          //   (it was a leaf node anyway, so we don't save much here)
  [ 2, 5, 117, 13, 17 ]   //   try to merge 11 and 17 into 117
  [ 2, 5, 117, 13 ]       //   remove 17, which now appears in 117
  --> leaf node: 1171325  //   not better than the previous one
--> leaf node: 11131725   // not better than the previous one

Código

Experimente online!

let f = n => {
  let visited = {},
      a, d, k, best, search;

  // build the list of primes, as strings
  for(a = [ '2' ], n--, k = 3; n; k++) {
    for(d = k; k % (d -= 2);) {}
    d == 1 && n-- && a.push(k + '');
  }

  best = a.join('');

  // recursive search function
  (search = (a, n = 0, r = []) => {
    let x, y, i, j, k, s;

    // remove all entries in r[] that can be found in another entry
    r = r.filter((p, i) => !r.some((q, j) => i != j && ~q.indexOf(p)));

    // abort early if this node was already visited
    if(visited[r]) {
      return;
    }

    // otherwise, mark it as visited
    visited[r] = 1;

    // walk through all distinct pairs (x, y) in r[]
    for(i = 0; i < r.length; i++) {
      for(j = i + 1; j < r.length; j++) {
        x = r[i];
        y = r[j];

        // try to merge x and y if:
        // 1) the first k digits of x equal the last k digits of y
        for(k = 1; x.slice(0, k) == y.slice(-k); k++) {
          r[i] = y + x.slice(k);
          search(a, n, r);
        }

        // or:
        // 2) the first k digits of y equal the last k digits of x
        for(k = 1; y.slice(0, k) == x.slice(-k); k++) {
          r[i] = x + y.slice(k);
          search(a, n, r);
        }
        r[i] = x;
      }
    }

    if(x = a[n]) {
      // there are other primes to process, so go on with the next one
      search(a, n + 1, [...r, x]);
    }
    else {
      // this is a leaf node: see if we've improved our current score
      s = r.join('');

      if(s.length <= best.length) {
        s = r.sort().join('');

        if(s.length < best.length || s < best) {
          best = s;
        }
      }
    }
  })(a);

  return best;
}

2
Bom trabalho para encontrar (18).
ouflak

Ótima resposta! Eu não sou especialista em JavaScript, mas o algoritmo parece estar na linha do que foi vinculado por Kevin Cruijssen. Boa explicação do algoritmo, é fácil ver que você encontrará o valor mínimo. Eu não fiz pessoalmente o benchmarking em JS, posso executá-lo no meu navegador ou existe outra maneira preferida de fazê-lo?
maxb

@ maxb Eu não recomendaria executar isso em um navegador, pois ele irá congelá-lo. Ele deve ser executado com o Node.js (como no TIO).
Arnauld

10

Solucionador Concorde TSP , pontuação 84 em 299 segundos

Bem ... eu me sinto boba por perceber isso agora.

Tudo isso é essencialmente um problema de vendedor ambulante . Para cada par de números primos pe q, adicione uma aresta cujo peso seja o número de dígitos adicionados por q(removendo dígitos sobrepostos). Além disso, adicione uma aresta inicial a cada prime p, cujo peso é o comprimento de p. O caminho mais curto do vendedor itinerante corresponde ao tamanho do menor número de contenção principal.

Então, um solucionador TSP de nível industrial, como o Concorde , fará um breve trabalho com esse problema.

Essa entrada provavelmente deve ser considerada não concorrente.

Resultados

O solucionador chega N=350em cerca de 20 horas de CPU. Os resultados completos são muito longos para uma postagem do SE, e a OEIS não deseja tantos termos assim mesmo. Aqui estão os primeiros 200:

1 2
2 23
3 235
4 2357
5 112357
6 113257
7 1131725
8 113171925
9 1131719235
10 113171923295
11 113171923295
12 1131719237295
13 11317237294195
14 1131723294194375
15 113172329419437475
16 1131723294194347537
17 113172329419434753759
18 2311329417434753759619
19 231132941743475375961967
20 2311294134347175375961967
21 23112941343471735375961967
22 231129413434717353759619679
23 23112941343471735359619678379
24 2311294134347173535961967837989
25 23112941343471735359619678378979
26 2310112941343471735359619678378979
27 231010329411343471735359619678378979
28 101031071132329417343475359619678378979
29 101031071091132329417343475359619678378979
30 101031071091132329417343475359619678378979
31 101031071091131272329417343475359619678378979
32 101031071091131272329417343475359619678378979
33 10103107109113127137232941734347535961967838979
34 10103107109113127137139232941734347535961967838979
35 10103107109113127137139149232941734347535961967838979
36 1010310710911312713713914923294151734347535961967838979
37 1010310710911312713713914915157232941734347535961967838979
38 1010310710911312713713914915157163232941734347535961967838979
39 10103107109113127137139149151571631672329417343475359619798389
40 10103107109113127137139149151571631672329417343475359619798389
41 1010310710911312713713914915157163167173232941794347535961978389
42 101031071091131271371391491515716316717323294179434753596181978389
43 101031071091131271371391491515716316723294173434753596181917978389
44 101031071091131271371391491515716316717323294179434753596181919383897
45 10103107109113127137139149151571631671731792329418191934347535961978389
46 10103107109113127137139149151571631671731791819193232941974347535961998389
47 101031071091271313714915157163167173179181919321139232941974347535961998389
48 1010310710912713137149151571631671731791819193211392232941974347535961998389
49 1010310710912713137149151571631671731791819193211392232272941974347535961998389
50 10103107109127131371491515716316717317918191932113922322722941974347535961998389
51 101031071091271313714915157163167173179181919321139223322722941974347535961998389
52 101031071091271313714915157163167173179181919321139223322722923941974347535961998389
53 1010310710912713137149151571631671731791819193211392233227229239241974347535961998389
54 101031071091271313714915157163167173179211392233227229239241819193251974347535961998389
55 101031071091271313714915157163167173179211392233227229239241819193251972574347535961998389
56 101031071091271313714915157163167173179211392233227229239241819193251972572634347535961998389
57 101031071091271313714915157163167173179211392233227229239241819193251972572632694347535961998389
58 101031071091271313714915157163167173179211392233227229239241819193251972572632694347535961998389
59 1010310710912713137149151571631671731792113922332277229239241819193251972572632694347535961998389
60 101031071091271313714915157163167173211392233227722923924179251819193257263269281974347535961998389
61 1010310710912713137149151571631671732113922332277229239241792518191932572632692819728343475359619989
62 10103107109127131371491515716316717321139223322772293239241792518191932572632692819728343475359619989
63 1010307107109127131371491515716316717321139223322772293239241792518191932572632692819728343475359619989
64 10103071071091271311371391491515716316721173223322772293239241792518191932572632692819728343475359619989
65 10103071071091271311371491515716313916721173223322772293239241792518191932572632692819728343475359619989
66 10103071071091271311371491515716313921167223317322772293239241792518191932572632692819728343475359619989
67 10103071071091271311371491515716313921167223317322772293239241792518191932572632692819728343475359619989
68 1010307107109127131137149151571631392116722331732277229323924179251819193257263269281972833743475359619989
69 1010307107109127131137149151571631392116722331732277229323924179251819193257263269281972833743475359619989
70 101030710710912713113714915157163139211672233173227722932392417925181919325726326928197283374347534959619989
71 101030710710912713113714915157163139211672233173227722932392417925181919325726337269281972834743534959619989
72 101030710710912713113714915157163139211672233173227722932392417925181919337257263472692819728349435359619989
73 10103071071091271311371491515716313921167223317322772293372392417925181919347257263492692819728353594367619989
74 101030710710912713113714915157163139211672233173227722932392417925181919337347257263492692819728353594367619989
75 1010307107109127131137313914915157163211672233173227722933792392417925181919347257263492692819728353594367619989
76 101030710710912713113731391491515716321167223317322772293379239241792518191934725726349269281972835359438367619989
77 101030710710912713113731391491515716321167223317337922772293472392417925181919349257263535926928197283674383896199
78 1010307107109127131137313914915157163211672233173379227722934723972417925181919349257263535926928197283674383896199
79 101030710710912713113731391491515721163223317337922772293472397241672517925726349269281819193535928367401974383896199
80 101030710710912713113731391491515721163223317337922772293472397241672517925726349269281819193535928367401974094383896199
81 101030710710912713113731391491515721163223317337922772293472397241916725179257263492692818193535928367401974094383896199
82 1010307107109127131137313914915157223317322772293379239724191634725167257263492692817928353594018193674094211974383896199
83 1010307107109127131137313914922331515722772293379239724191634725167257263492692817353592836740181938389409421197431796199
84 101030710710912713113731391492233151572277229323972419163472516725726349269281735359283674018193838940942119743179433796199
85 101030710710912713113731391492233151572277229323924191634725167257263492692817353592836740181938389409421197431794337943976199
86 1010307107109127131137313914922331515722772293239241916347251672572634926928173535928367401819383894094211974317943379443976199
87 1010307107109127131137313914922331515722772293239241916347251672572634926928173535928367401819383894094211974317943379443974496199
88 1010307107109127131137313914922331515722772293239241916347251672572634926928173535928367401819383894094211974317943379443974494576199
89 10103071071091271311373139149223315157227722932392419163472516725726349269281735359283674018193838940942119743179433794439744945746199
90 10103071071091271311373139149223315157227722932392419163251672572634726928173492835359401819367409421197431794337944397449457461994638389
91 10103071071091271311373139149223315157227722932392419163251672572634726928173492835359401819367409421197431794337944397449457461994638389467
92 101030710710912713113731391492233151572277229323924191632516725726347926928173492835359401819367409421197431794337944397449457461994638389467
93 101030710710912713113731391492233151572277229323924191632516725726347926928173492835359401819367409421197431794337944397449457461994638389467487
94 101030710710912713113731392233149151572277229323924191632516725726347926928173492835359401819367409421197431794337944397449457461994638389467487
95 1010307107109127131137313922331491515722772293239241916325167257263479269281734928353594018193674094211974317943379443974499457461994638389467487
96 1010307107109127131137313922331491515722772293239241916325167257263269281734792834940181935359409421197431794337944397449945746199463674674875038389
97 1010307107109127131137313922331491515722772293239241916325167257263269281734792834940181935359409421197431794337944397449945746199463674674875038389509
98 101030710710912713113732233139227722932392419149151572516325726326928167283479401734940942118193535943179433794439744994574619746367467487503838950952199
99 1010307107109127131137322331392277229324191491515725163257263269281672834794017349409421181935359431794337944394499457461974636746748750383895095219952397
100 101030710710922331127131373227722932414915157251632572632692816728347940173494094211394317943379443944994574618191935359463674674875038389509521975239754199
101 101030710710922331127131373227722932414915157251632572632692816728347401734940942113943179433794439449945746181919353594636746748750383895095219752397541995479
102 101030710710922331127131373227722932414915157251632572632692816728347401734940942113943179433794439449945746181919353594636746748750383895095219752397541995479557
103 101030710710922331127131373227722932414915157251632572632692816728340173474094211394317943379443944945746181919349946353594674875036750952197523975419954795575638389
104 101030710710922331127131373227722932414915157251632572632692816728340173474094211394317943379443944945746181919349946353594674875036750952197523975419954795575638389569
105 101030710722331109227127722932413137325149151571632572632692816728340173474094211394317943379443944945746181919349946353594674875036750952197523975419954795575638389569
106 1010307107223311092271277229324131373251491515716325726326928167283401734740942113943179433794439449457461819193499463535946748750367509521975239754199547955775638389569
107 1010307107223311092271277229324131373251491515716325726326928167283401734740942113943179433794439449457461819193499463535946748750367509521975239754199547955775638389569587
108 10103071072233110922712772293241313732514915157163257263269281672834017340942113943179433794439449457461819193474634994674875035359367509521975239754199547955775638389569587
109 10103071072233110922712772293241313732514915157163257263269281672834017340942113943179433794439449457461819193474634994674875035359367509521975239754199547955775638389569587599
110 1010307223311072271092293241277251313732571491515726326928163283401674094211394317343379443944945746179463474674875034995095218191935359367523975419754795577563838956958759960199
111 1010307223311072271092293241277251313732571491515726326928163283401674094211394317343379443944945746179463474674875034995095218191935359367523975419754795577563838956958759960199607
112 1010307223311072271092293241277251491515716325726326928167283401734094211313734317943379443944945746139463474674875034995095218191935359367523975419754795577563838956958759960199607
113 22331101030722710722932410925127725714915157263269281632834016740942113137343173433794439449457461394634746748750349950952181919353593675239754197547955775638389569587599601996076179
114 2233110103072271072293241092512571277263269281491515728340163409421131373431734337944394494574613946347467487503499509521675239754191819353593675479557756383895695875996019760761796199
115 22331010307227107229324109251257126311277269281491515728340163409421131373431734337944394494574613946347467487503499509521675239754191819353593675479557756383895695875996019760761796199
116 22331010307227107229324109251257126311269281277283401491515740942113137343173433794439449457461394634674875034750952163499523975416754795577563535936756958759960181919383896076179619764199
117 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346748750347509521634995239541675479557756353593675695875996018191938389607617961976419964397
118 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346748750347509521634995239541675475577563535936756958759960181919383896076179619764199643976479
119 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346748750347509521634995239541675475577563535695875935996018191936760761796197641996439764796538389
120 2233101030722710722932410925125712631126928127728340149151574094211313734317344337944945746139463467487503475095216349952395416754755775635356958760181919359367607617961976419964397647965383896599
121 22331010307227107229324109251257126311269281277283401491515740942113137343173443379449457461394634674875034750952163499523954167547557756353569587601819193593676076179641976439764796538389659966199
122 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346734748750349950952163523954167547557756353569587601819193593676076179641976439764796538389659966199
123 2233101030722710722932410925125712631126928127728340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936776076179641976439764796538389659966199
124 2233101030722710722932410925125712631126928127728340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
125 22331010307227107229324109251257126311269127728128340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
126 2233101030701072271092293241251257126311269127728128340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
127 223310103070107092271092293241251257126311269127728128340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
128 223310103070107092271092293241251257191263112691277281283401491515740942113137343173443379449457461394634673474875034995095216352395416754755775635356958760181935936076179641976439764796536776599661996838389
129 22331010307010709227109229324125125719126311269127277281283401491515740942113137343173443379449457461394634673474875034995095216352395416754755775635356958760181935936076179641976439764796536776599661996838389
130 223307010103227092293241072510925712631126912719128128340140942113137331491515727743173443379449457461394634673474875034995095216352395416754755775635356958760181935936076179641976439764796536776599661996838389
131 2233070101032270922932410725109257126311269127191281283401409421131373314915157277431734433794494574613946346739487503475095216349952395416754755775635356958760181935936076179641976439764796536776599661996838389
132 2233070101032270922932410725109257126311269127191281283401409421131373314915157277431734433794494574613946346739487503475095216349952395416754755775635356958760181935936076179641976439764796536776599661996838389
133 223307010103227092293241072510925712631126912719128128340140942113137331443173449149457277433794613946346739487503475095215157516349952395416754755775635356958760181935936076179641976439764796536776599661996838389
134 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727743379461394634673948750347509521515751634995239541675475575635356958757760181935936076179641976439764796536776599661996838389
135 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727743379461394634673948750347509521515751634995239541675475575635356958757760181935936076179641976439764796536776599661996838389
136 2233070101032270922932410725109257126311269127191281283401409421131373314431734491494572774337946139463467394875034750952151575163499523954167547557563535695875776018193593607617964197643976479653677696599661996838389
137 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479653677696599661996838389
138 2233070101032270922932410725109257126311269127191281283401409421131373314431734491494572773461394634673948743379503475095215157516349952395416754755756353569587577601819359360761796419764397647965367787696599661996838389
139 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479765367787696599661996838389
140 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479765367787696599661996838389809
141 223307010103227092293241072510925712631126912719128112834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479765367787696599661996838389809
142 223307010103227092293241072510925712631126912719128112834014094211313733144317344914572773461394634673948743379503475095214952395415157516349954755756353569587577601676076179641935936439764797653677659966197876968383898098218199
143 223070101032270922932410725109257126311269127191281128340140942113137331443173449145727734613946346739487433475034950952149952337954151575163535475575635695875776016760761796419359364396479765367765996619768383898098218199823978769
144 223070101032270922932410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151575163535475575635695875773960167607617964193593643964797653677659966197683838980982181998239769827787
145 223070101032270922924107251092571263112691271912811283401409421131373314431734491457274334613946346734748750349509521499523379541515751635354755756356958757739601676076179641935936439647976536599661976836776980982181998239782778782938389
146 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587577396016760761796419359364396479765367765996619768383976980982181998239827787829389
147 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587577396016760761796419359364396479765365996619768367769809821819982397827787829383985389
148 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587576016760761796419359364396479765365996619768367739769809821819982398277829383985389857787
149 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587576016760761796419359364396479765365966197683677397698098218199823982778293839853898577878599
150 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587576016760761796419359364396479765365966197683677397698098218199823982778293839853857787859986389
151 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151575163535475575635695875760167607617964193593643964797653659661976836773976980982181998239827782938398538577877859986389
152 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383985385778778599863898818199
153 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857787785998638988181998839
154 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857785998638988181998839887787
155 2230701010322709072292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857785998638988181998839887787
156 22307010103227090722924107251092571263112691127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857785998638988181998839887787
157 22307010103227090722924107251092571263112691127191281128340140942113137331443173449193457274334613946346734748750349509521499523379541515475155756353569587576015760761796419764396479765359365966199683676980982163823978277398293838538577859986389881816778778839887
158 2230701010322709072292410725109257126311269112719128112834014092934211313733144317344919345727433461394634673474875034950952149952337954151547515575635356958757601576076179641976439647976535936596619968367698098216382397827739829853838577859986389881816778778839887
159 22307010103227090722924107251092571263112691127191281128340140929342113137274314433173344919345746139463467347487503495095214995233735354151547515575635695875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
160 2230701010322709072292410725109257126311269112719128112834014092934211313727431443317334491934574613941463467347487503495095214995233735354151547515575635695875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
161 223070101032270907229241072510925712631126911271912811283401409293421131372743144331733449193457461394146346734748750349475095214995233735354151547515575635695875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
162 22307010103227090722924107251092571263112691127191281128340140929342113137274314433173344919345746139414634673474875034947509521499523373535415154751557563569535875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
163 2230701010322709072292410725109257126311269112719128112834014092934211313727431443317334491934574613941463467347487503494750952149952337353541515475155756356953587576015760761796419764396479653593797659661996768367698098216382397827739829853838577859986389881816778778839887
164 22307010103227090722924107251092571263112691127128112834014092934211313727431443317334491457461394146346734748750349475095214995233735354151547515575635695358757601576076179641919359379643964797197653659661996768367698098216382397827739829853838577859986389881816778778839887
165 223070101032270907229241072510925712631126911271281128340140929342113137274314433173344914574613941463467347487503494750952149952337353541515475155756356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829853838577859986389881816778778839887
166 22307010103227090722924107251092571263112691127128112834014092934211313727431443317334491457461394146346734748750349475095214995233735354151547515575635695358757601576076179641919359379643964797197653659661996768367698098216382397739827782983838538577859986389881816778778839887
167 223070101032270907229241072510925712631126911271281128340140929342113137274314433173344914574613941463467347487503494750952149915152337353541547515575635695358757601576076179641919359379643964797197653659661996768367698098216382397739827782983838538577859986389881816778778839887
168 2230701010322709072292410725109257126311269112712811283401409293421131372743144331733449145746139414634673474875034947509521499151523373535415475155756356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829838385385778599786389881816778778839887
169 2230701009070922710103229241072510925712631126911272728112834014092934211313733144317344914574334613941463467347487503494750952149915152337515415475575635356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829838385385778599786389881816778778839887
170 22307010090709227101310322924107251092571263112691127272811283401409293421134431373317344914574334613941463467347487503494750952149915152337515415475575635356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829838385385778599786389881816778778839887
171 22307010090709227101310191032292410725109257126311269112727281128340140929342113443137331734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
172 22307010090709227101310191021032292410725109257126311269112727281128340140929342113443137331734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
173 223070100907092271013101910210310722924109251257126311269112727281128340140929342113443137331734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
174 223070100907092271013101910210310331107229241092512571263132691127272811283401409293421137334431734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
175 223070100907092271013101910210310331103922924107251092571263132691127272811283401409293421137334431734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
176 223070100907092271013101910210310331103922924104910725109257126313269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
177 223070100907092271013101910210310331103922924104910510725109257126313269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
178 223070100907092271013101910210310331103922924104910510610725109257126313269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
179 223070100907092271013101910210310331103922924104910510610631325107257109263269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
180 223070100907092271013101910210310331103922924104910510610631325106911072571092632692811272728340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
181 223070100907092271013101910210310331103922924104910510610631325106911072571087263269281092834012727409293421137334431734494145743346139463467347487503494750952149919352337515154154755756353569535875760157607617964196439647965359379719765966199676836769809821638239773982778298383853857785997863898811816778778839887
182 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109112727283401409293421137334431734494145743346139463467347487503494750952149919352337515154154755756353569535875760157607617964196439647965359379719765966199676836769809821638239773982778298383853857785997863898811816778778839887
183 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834012727409293421137334431734494145743346139463467347487503494750952149919352337515154154755756353569535875760157607617964196439647965359379719765966199676836769809821638239773982778298383853857785997863898811816778778839887
184 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834010971929340941272742113733443173449457433461394634673474875034947509521499193523375151541547557563535695358757601576076179641976439647965359379765966199676836769809821638239773982778298383853857785997863898811816778778839887
185 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834010971929340941272742113733443173449457433461394634673474875034947509521499193523375151541547557563535695358757601576076179641976439647965359379765966199676836769809821638239773982778298383853857785997863898811816778778839887
186 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834010971929340941272742113733443173449457433461394634673474875034947509521499193523375151541547557563535695358757601576076179641976439647965359379765966199676836769809821638239773982778298383853857785997863898811816778778839887
187 223070100907092271013101910210310331103922924104910510610631325106910725710872632692810911093283401097192934094127274211173344317433449457461373463467347487503494750952149919352337515154157547557563535695358757601635937960761796419764396479765365966199676836769809821677397782398277829838385385778599786389881811398839887787
188 223070100907092271013101910210310331103922924104910510610631325106910725710872632692810911093283401097192934094111727421123344317334494574337346137463467347487503494750952127514991935235354151575475575635695358757601635937960761796419764396479765365966199676836769809821677397782398277829838385385778599786389881811398839887787
189 1009070101307092232271019102103103310491051061063110392292410691072510872571091109326326928109719283401117274092934211233443131733449411294574337346137463467347487503494750952127514991935235354151575475575635695358757601635937960761796419764396479765365966199676836769809821677397782398277829838385385778599786389881811398839887787
190 10090701013070922322710191021031033104910510610631103922924106910725108725710911093263269281097192834011172740929342112334431317334494112945743373461374634673474875034947509521139523535412751499193547557563569535875760157607617964197643964796535937976596619967683676980982163823977398277829838385385778599786389881151816778778839887
191 100907010130709101910210310331049105106106311039223227106910722924108725109110932571097192632692811172728340112334092934211294113137334431734494574337461394634673474875034947509521151153523535412751499193547557563569535875760157607617964197643964796535937976596619967683676980982163823977398277829838385385778599786389881816778778839887
192 1009070101307091019102103103310491051061063110392232271069107229241087251091109325710971926326928111727283401123340929342112941131373344317344945743374613946346734748750349475095211511535235354116354751275575635695358757601499193593796076179641976439647976536596619967683676980982157739778239827782983838538578599786389881816778778839887
193 1009070101307092232271019102103103310491051061063110392292410691072510872571091109326326928109711171928340112334092934211294113137274317334433734494574613946346734748750349475095211511535235354127514991935475575635695358757601576076179641976439647965359379765966199676836769809821677397782398277829838385385778599786388181163898839887787
194 10090701013070922322710191021031033104910510610631103922924106910725108725710911093263269281097111719283401123340929342112941131372743173344337344945746139463467347487503494750952115115352353541163547512755756356953587576014991935937960761796419764396479765365966199676836769809821577397782398277829838385385785997863898811816778778839887
195 100907010130709101910210310331049105106106311039223227106910722924108725109110932571097111719263269281123283401129293409411313727421151153443173344945743346139463467347487503494750952116352337353541181187512754755756356953587576014991935937960761796419764396479765365966199676836769809821577397782398277829838385385785997863898816778778839887
196 100907010130709101910210310331049105106106310691072231103922710872292410911093251097111711232571926326928112928340113137274092934211511534431733449411634574334613946346734748750349475095211811875119352337353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
197 100907010130709101910210310331049105106106310691072231103922710872292410911093251097111711232571926326928112928340113137274092934211511534431733449411634574334613946346734748750349475095211811875119352337353541201275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
198 1009070101307091019102103103310491051061063106910710872231103922710911093229241097111711232511292571926326928113132834011511534092934211634431733449411811872743345746137346346734748750349475095211935233751201213953535412754755756356958757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
199 10090701013070910191021031033104910510610631069107108710911039223110932271097111711232292411292511313257192632692811511532834011634092934211811872743173344334494119345746137346346734748750349475095212012139523375121754127547557563535695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
200 100907010130709101910210310331049105106106310691071087109109311039110971117112322711292292411313251151153257192632692811632834011811872740929342119344317334494120121373457433461394634673474875034947509521217512233752353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887

Código

Aqui está um script Python 3 para chamar o solucionador de Concorde várias vezes, até que ele construa as soluções.

O Concorde é gratuito para uso acadêmico. É possível fazer o download de um binário executável do Concorde criado com seu próprio pacote de programação linear QSopt ou, se você tiver uma licença para o IBM CPLEX, de alguma forma, poderá criar o Concorde a partir da origem para usar o CPLEX.

#!/usr/bin/env python3
'''
Find prime containment numbers (OEIS A054261) using the Concorde
TSP solver.

The n-th prime containment number is the smallest natural number
which, when written in decimal, contains the first n primes.
'''

import argparse
import itertools
import os
import sys
import subprocess
import tempfile

def join_strings(a, b):
  '''Shortest string that starts with a and ends with b.'''
  for overlap in range(min(len(a), len(b)), 0, - 1):
    if a[-overlap:] == b[:overlap]:
      return a + b[overlap:]
  return a + b

def is_prime(n):
  if n < 2:
    return False
  d = 2
  while d*d <= n:
    if n % d == 0:
      return False
    d += 1
  return True

def prime_list_reduced(n):
  '''First n primes, with primes that are substrings of other
     primes removed.'''
  primes = []
  p = 2
  while len(primes) < n:
    if is_prime(p):
      primes.append(p)
    p += 1

  reduced = []
  for p in primes:
    if all(p == q or str(p) not in str(q) for q in primes):
      reduced.append(p)
  return reduced

# w_med is an offset for actual weights
# (we use zero as a dummy weight when splitting nodes)
w_med = 10**4
# w_big blocks edges from being taken
w_big = 10**8

def gen_tsplib(prefix, strs, start_candidates):
  '''Generate TSP formulation in TSPLIB format.

     Returns a TSPLIB format string that encodes the length of the
     shortest string starting with 'prefix' and containing all 'strs'.

     start_candidates is the set of strings that solution paths are
     allowed to start with.
     '''
  N = len(strs)

  # Concorde only supports symmetric TSPs. Therefore we encode the
  # asymmetric TSP instances by doubling each node.
  node_in = lambda i: 2*i
  node_out = lambda i: node_in(i) + 1
  # 2*(N+1) nodes because we add an artificial node with index N
  # for the start/end of the tour. This node is also doubled.
  num_nodes = 2*(N+1)

  # Ensure special offsets are big enough
  assert w_med > len(prefix) + sum(map(len, strs))
  assert w_big > w_med * num_nodes

  weight = [[w_big] * num_nodes for _ in range(num_nodes)]
  def edge(src, dest, w):
    weight[node_out(src)][node_in(dest)] = w
    weight[node_in(dest)][node_out(src)] = w

  # link every incoming node with the matching outgoing node
  for i in range(N+1):
    weight[node_in(i)][node_out(i)] = 0
    weight[node_out(i)][node_in(i)] = 0

  for i, p in enumerate(strs):
    if p in start_candidates:
      prefix_w = len(join_strings(prefix, p))
      # Initial length
      edge(N, i, w_med + prefix_w)
    else:
      edge(N, i, w_big)
    # Link every str to the end to allow closed tours
    edge(i, N, w_med)

  for i, p in enumerate(strs):
    for j, q in enumerate(strs):
      if i != j:
        w = len(join_strings(p, q)) - len(p)
        edge(i, j, w_med + w)

  out = '''NAME: prime-containment-number
TYPE: TSP
DIMENSION: %d
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: FULL_MATRIX
EDGE_WEIGHT_SECTION
''' % num_nodes

  out += '\n'.join(
    ' '.join(str(w) for w in row)
    for row in weight
  ) + '\n'

  out += 'EOF\n'
  return out

def parse_tour_soln(prefix, strs, text):
  '''This constructs the solution from Concorde's 'tour' output format.
     The format simply consists of a permutation of the graph nodes.'''
  N = len(strs)
  node_in = lambda i: 2*i
  node_out = lambda i: node_in(i) + 1
  nums = list(map(int, text.split()))

  # The file starts with the number of nodes
  assert nums[0] == 2*(N+1)
  nums = nums[1:]

  # Then it should list a permutation of all nodes
  assert len(nums) == 2*(N+1)

  # Find and remove the artificial starting point
  start = nums.index(node_out(N))
  nums = nums[start+1:] + nums[:start]
  # Also find and remove the end point
  if nums[-1] == node_in(N):
    nums = nums[:-1]
  elif nums[0] == node_in(N):
    # Tour printed in reverse order
    nums = reversed(nums[1:])
  else:
    assert False, 'bad TSP tour'
  soln = prefix
  for i in nums:
    # each prime appears in two adjacent nodes, pick one arbitrarily
    if i % 2 == 0:
      soln = join_strings(soln, strs[i // 2])
  return soln

def scs_length(prefix, strs, start_candidates, concorde_path, concorde_verbose):
  '''Find length of shortest containing string using one call to Concorde.'''
  # Concorde's small-input solver CCHeldKarp, tends to fail with the
  # cryptic error message 'edge too long'. Brute force instead
  if len(strs) <= 5:
    best = len(prefix) + sum(map(len, strs))
    for perm in itertools.permutations(range(len(strs))):
      if perm and strs[perm[0]] not in start_candidates:
        continue
      soln = prefix
      for i in perm:
        soln = join_strings(soln, strs[i])
      best = min(best, len(soln))
    return best

  with tempfile.TemporaryDirectory() as tempdir:
    concorde_path = os.path.join(os.getcwd(), concorde_path)
    with open(os.path.join(tempdir, 'prime.tsplib'), 'w') as f:
      f.write(gen_tsplib(prefix, strs, start_candidates))

    if concorde_verbose:
      subprocess.check_call([concorde_path, os.path.join(tempdir, 'prime.tsplib')],
                            cwd=tempdir)
    else:
      try:
        subprocess.check_output([concorde_path, os.path.join(tempdir, 'prime.tsplib')],
                                cwd=tempdir, stderr=subprocess.STDOUT)
      except subprocess.CalledProcessError as e:
        print('Concorde exited with error code %d\nOutput log:\n%s' %
              (e.returncode, e.stdout.decode('utf-8', errors='ignore')),
              file=sys.stderr)
        raise

    with open(os.path.join(tempdir, 'prime.sol'), 'r') as f:
      soln = parse_tour_soln(prefix, strs, f.read())
    return len(soln)

# Cache results from previous N's
pcn_solve_cache = {} # (prefix fragment, strs) -> soln

def pcn(n, concorde_path, concorde_verbose):
  '''Find smallest prime containment number for first n primes.'''
  strs = list(map(str, prime_list_reduced(n)))
  target_length = scs_length('', strs, strs, concorde_path, concorde_verbose)

  def solve(prefix, strs, target_length):
    if not strs:
      return prefix

    # Extract part of prefix that is relevant to cache
    prefix_fragment = ''
    for s in strs:
      next_prefix = join_strings(prefix, s)
      overlap = len(prefix) + len(s) - len(next_prefix)
      fragment = prefix[len(prefix) - overlap:]
      if len(fragment) > len(prefix_fragment):
        prefix_fragment = fragment
    fixed_prefix = prefix[:len(prefix) - len(prefix_fragment)]
    assert fixed_prefix + prefix_fragment == prefix

    cache_key = (prefix_fragment, tuple(strs))
    if cache_key in pcn_solve_cache:
      return fixed_prefix + pcn_solve_cache[cache_key]

    # Not in cache, we need to calculate it.
    soln = None

    # Try strings in ascending order until scs_length reports a
    # solution with equal length. That string will be the
    # lexicographically smallest extension of our solution.
    next_prefixes = sorted((join_strings(prefix, s), s)
                           for s in strs)

    # Try first string -- often works
    next_prefix, _ = next_prefixes[0]
    next_prefixes = next_prefixes[1:]
    next_strs = [s for s in strs if s not in next_prefix]
    next_length = scs_length(next_prefix, next_strs, next_strs,
                             concorde_path, concorde_verbose)
    if next_length == target_length:
      soln = solve(next_prefix, next_strs, next_length)
    else:
      # If not, do a weighted binary search on remaining strings
      while len(next_prefixes) > 1:
        split = (len(next_prefixes) + 2) // 3
        group = next_prefixes[:split]
        group_length = scs_length(prefix, strs, [s for _, s in group],
                                  concorde_path, concorde_verbose)
        if group_length == target_length:
          next_prefixes = group
        else:
          next_prefixes = next_prefixes[split:]
      if next_prefixes:
        next_prefix, _ = next_prefixes[0]
        next_strs = [s for s in strs if s not in next_prefix]
        check = True
        # Uncomment if paranoid
        #next_length = scs_length(next_prefix, next_strs, next_strs,
        #                         concorde_path, concorde_verbose)
        #check = (next_length == target_length)
        if check:
          soln = solve(next_prefix, next_strs, target_length)

    assert soln is not None, (
      'solve failed! prefix=%r, strs=%r, target_length=%d' %
      (prefix, strs, target_length))

    pcn_solve_cache[cache_key] = soln[len(fixed_prefix):]
    return soln

  return solve('', strs, target_length)

parser = argparse.ArgumentParser()
parser.add_argument('--concorde', type=str, default='concorde',
                    help='path to Concorde binary')
parser.add_argument('--verbose', action='store_true',
                    help='dump all Concorde output')
parser.add_argument('--start', type=int, metavar='N', default=1,
                    help='start at this N')
parser.add_argument('--end', type=int, metavar='N', default=1000000,
                    help='stop after this N')
parser.add_argument('--one', type=int, metavar='N',
                    help='solve for a single N and exit')

def main():
  opts = parser.parse_args(sys.argv[1:])

  if opts.one is not None:
    opts.start = opts.one
    opts.end = opts.one

  prev_soln = ''
  for n in range(opts.start, opts.end+1):
    primes = map(str, prime_list_reduced(n))
    if all(p in prev_soln for p in primes):
      soln = prev_soln
    else:
      soln = pcn(n, opts.concorde, opts.verbose)

    print('%d %s' % (n, soln))
    sys.stdout.flush()
    prev_soln = soln

if __name__ == '__main__':
  main()

Isso é simplesmente incrível. Como o problema é NP-completo, eu sabia que você poderia transformá-lo em TSP teoricamente. Mas usar o solucionador TSP é muito inteligente! Vou precisar compará-lo hoje mais tarde, mas tenho certeza de que essa será a solução mais rápida até agora.
maxb

Também verifiquei se as duas soluções oferecem o mesmo resultado para os 62 primeiros números. Quanta memória essa solução requer? Eu poderia colocar meu laptop velho para trabalhar por alguns dias analisando os números.
maxb

Estou tão surpreso quanto você. Antes disso, meu modelo mental de solucionadores de TSP limitava-se a cenários envolvendo passeios a distância euclidianos de cidades, aeroportos, armazéns, etc. Encontrar essas cadeias é um problema combinatório desafiador (os pesos das arestas são todos 1, 2 e 3). Concorde corta através deles como manteiga quente.
japh

O solucionador de Concorde ainda usa menos RAM que o script Python que o supervisiona.
japh

Resultados impressionantes! Já visitei o site Concorde por causa desse desafio antes de você postar isso, mas ainda pensei que provavelmente não vale a pena tentar. De qualquer forma, tenho certeza de que a OEIS está interessada em todos os seus resultados. Apenas dê-os como arquivo b para resultados com no máximo 1000 dígitos e como arquivo para resultados mais longos.
Christian Sievers

9

Limpo , marcar 25 em 231 segundos (pontuação oficial)

Resultados

  • 1 < n <= 23em 42 36 segundos no TIO
  • n = 24 (2311294134347173535961967837989)em 32 24 segundos localmente
  • n = 25 (23112941343471735359619678378979)em 210 160 segundos localmente
  • n = 1a n = 25foi encontrado em 231 segundos para que a pontuação oficial (editado por maxb)

Isso usa uma abordagem semelhante à solução JS da Arnauld, com base na rejeição recursiva da permutação, usando um conjunto de árvores especializado para ganhar muita velocidade.

Para cada primo que precisa caber no número:

  1. verifique se o prime é uma sub-string de outro prime e, se estiver, remova-o
  2. classifique a lista atual de sub-strings principais, junte-a e adicione-a ao conjunto de árvores balanceadas
  3. verifique se há números primos na frente de outros e, se houver, junte-se a eles - ignorando os elementos já ordenados adjacentes que são testados pela etapa de rejeição

Em seguida, para cada par de sub-strings às quais unimos, remova quaisquer sub-strings desse par unido da lista de sub-strings e recorra a ele.

Depois que nenhuma sub-string pode ser unida a outras sub-strings em qualquer parte de nossa recursão, usamos o conjunto de árvores já ordenadas para encontrar rapidamente o número mais baixo que contém as sub-strings.

Coisas a serem melhoradas / adicionadas:

  • Afaste-se de permutar todo o espaço de pesquisa, gere candidatos
  • Geração de candidato baseada em prefixo / sufixo para ativar a memorização
  • Multithreading, trabalho dividido sobre prefixos uniformemente ao número de threads

Houve grandes quedas de desempenho entre 19 -> 20e24 -> 25 devido à manipulação duplicada na etapa de teste de mesclagem e na etapa de rejeição do candidato, mas elas foram corrigidas.

Otimizações:

  • removeOverlap foi projetado para fornecer sempre um conjunto de sub-strings na ordem ideal
  • uInsertMSpec reduz check-if-is-member e insert-new-member para um conjunto transversal
  • containmentNumbersSt verifica se a solução anterior funciona para um novo número
module main
import StdEnv,StdOverloadedList,_SystemEnumStrict
import Data.List,Data.Func,Data.Maybe,Data.Array
import Text,Text.GenJSON

// adapted from Data.Set to work with a single specific type, and persist uniqueness
:: Set a = Tip | Bin !Int a !.(Set a) !.(Set a)
derive JSONEncode Set
derive JSONDecode Set

delta :== 4
ratio :== 2

:: NumberType :== String

:: SetType :== NumberType

//uSingleton :: SetType -> Set
uSingleton x :== (Bin 1 x Tip Tip)

// adapted from Data.Set to work with a single specific type, and persist uniqueness
uFindMin :: !.(Set .a) -> .a
uFindMin (Bin _ x Tip _) = x
uFindMin (Bin _ _ l _)   = uFindMin l

uSize set :== case set of
	Tip = (0, Tip)
	s=:(Bin sz _ _ _) = (sz, s)
	
uMemberSpec :: String !u:(Set String) -> .(.Bool, v:(Set String)), [u <= v]
uMemberSpec x Tip = (False, Tip)
uMemberSpec x set=:(Bin s y l r)
	| sx < sy || sx == sy && x < y
		# (t, l) = uMemberSpec x l
		= (t, Bin s y l r)
		//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceL y l r)
	| sx > sy || sx == sy && x > y
		# (t, r) = uMemberSpec x r
		= (t, Bin s y l r)
		//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceR y l r)
	| otherwise = (True, set)
where
	sx = size x
	sy = size y

uInsertM :: !(a a -> .Bool) -> (a u:(Set a) -> v:(.Bool, w:(Set a))), [v u <= w]
uInsertM cmp = uInsertM`
where
	//uInsertM` :: a (Set a) -> (Bool, Set a)
	uInsertM` x Tip = (False, uSingleton x)
	uInsertM` x set=:(Bin _ y l r)
		| cmp x y//sx < sy || sx == sy && x < y
			# (t, l) = uInsertM` x l
			= (t, uBalanceL y l r)
			//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceL y l r)
		| cmp y x//sx > sy || sx == sy && x > y
			# (t, r) = uInsertM` x r
			= (t, uBalanceR y l r)
			//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceR y l r)
		| otherwise = (True, set)
		
uInsertMCmp :: a !u:(Set a) -> .(.Bool, v:(Set a)) | Enum a, [u <= v]
uInsertMCmp x Tip = (False, uSingleton x)
uInsertMCmp x set=:(Bin _ y l r)
	| x < y
		# (t, l) = uInsertMCmp x l
		= (t, uBalanceL y l r)
		//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceL y l r)
	| x > y
		# (t, r) = uInsertMCmp x r
		= (t, uBalanceR y l r)
		//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceR y l r)
	| otherwise = (True, set)

uInsertMSpec :: NumberType !u:(Set NumberType) -> .(.Bool, v:(Set NumberType)), [u <= v]
uInsertMSpec x Tip = (False, uSingleton x)
uInsertMSpec x set=:(Bin sz y l r)
	| sx < sy || sx == sy && x < y
		#! (t, l) = uInsertMSpec x l
		= (t, uBalanceL y l r)
		//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceL y l r)
	| sx > sy || sx == sy && x > y
		#! (t, r) = uInsertMSpec x r
		= (t, uBalanceR y l r)
		//= (t, Bin sz y l r)
		//= (t, if(t)(\y` l` r` = Bin sz y` l` r`) uBalanceR y l r)
	| otherwise = (True, set)
where
	sx = size x
	sy = size y

// adapted from Data.Set to work with a single specific type, and persist uniqueness
uBalanceL :: .a !u:(Set .a) !v:(Set .a) -> w:(Set .a), [v u <= w]
//a .(Set a) .(Set a) -> .(Set a)
uBalanceL x Tip Tip
	= Bin 1 x Tip Tip
uBalanceL x l=:(Bin _ _ Tip Tip) Tip
	= Bin 2 x l Tip
uBalanceL x l=:(Bin _ lx Tip (Bin _ lrx _ _)) Tip
	= Bin 3 lrx (Bin 1 lx Tip Tip) (Bin 1 x Tip Tip)
uBalanceL x l=:(Bin _ lx ll=:(Bin _ _ _ _) Tip) Tip
	= Bin 3 lx ll (Bin 1 x Tip Tip)
uBalanceL x l=:(Bin ls lx ll=:(Bin lls _ _ _) lr=:(Bin lrs lrx lrl lrr)) Tip
	| lrs < ratio*lls
		= Bin (1+ls) lx ll (Bin (1+lrs) x lr Tip)
	# (lrls, lrl) = uSize lrl
	# (lrrs, lrr) = uSize lrr
	| otherwise
		= Bin (1+ls) lrx (Bin (1+lls+lrls) lx ll lrl) (Bin (1+lrrs) x lrr Tip)
uBalanceL x Tip r=:(Bin rs _ _ _)
	= Bin (1+rs) x Tip r
uBalanceL x l=:(Bin ls lx ll=:(Bin lls _ _ _) lr=:(Bin lrs lrx lrl lrr)) r=:(Bin rs _ _ _)
	| ls > delta*rs
		| lrs < ratio*lls
			= Bin (1+ls+rs) lx ll (Bin (1+rs+lrs) x lr r)
		# (lrls, lrl) = uSize lrl
		# (lrrs, lrr) = uSize lrr
		| otherwise
			= Bin (1+ls+rs) lrx (Bin (1+lls+lrls) lx ll lrl) (Bin (1+rs+lrrs) x lrr r)
	| otherwise
		= Bin (1+ls+rs) x l r
uBalanceL x l=:(Bin ls _ _ _) r=:(Bin rs _ _ _)
	= Bin (1+ls+rs) x l r

// adapted from Data.Set to work with a single specific type, and persist uniqueness
uBalanceR :: .a !u:(Set .a) !v:(Set .a) -> w:(Set .a), [v u <= w]
uBalanceR x Tip Tip
	= Bin 1 x Tip Tip
uBalanceR x Tip r=:(Bin _ _ Tip Tip)
	= Bin 2 x Tip r
uBalanceR x Tip r=:(Bin _ rx Tip rr=:(Bin _ _ _ _))
	= Bin 3 rx (Bin 1 x Tip Tip) rr
uBalanceR x Tip r=:(Bin _ rx (Bin _ rlx _ _) Tip)
	= Bin 3 rlx (Bin 1 x Tip Tip) (Bin 1 rx Tip Tip)
uBalanceR x Tip r=:(Bin rs rx rl=:(Bin rls rlx rll rlr) rr=:(Bin rrs _ _ _))
	| rls < ratio*rrs
		= Bin (1+rs) rx (Bin (1+rls) x Tip rl) rr
	# (rlls, rll) = uSize rll
	# (rlrs, rlr) = uSize rlr
	| otherwise
		= Bin (1+rs) rlx (Bin (1+rlls) x Tip rll) (Bin (1+rrs+rlrs) rx rlr rr)
uBalanceR x l=:(Bin ls _ _ _) Tip
	= Bin (1+ls) x l Tip
uBalanceR x l=:(Bin ls _ _ _) r=:(Bin rs rx rl=:(Bin rls rlx rll rlr) rr=:(Bin rrs _ _ _))
	| rs > delta*ls
		| rls < ratio*rrs
			= Bin (1+ls+rs) rx (Bin (1+ls+rls) x l rl) rr
		# (rlls, rll) = uSize rll
		# (rlrs, rlr) = uSize rlr
		| otherwise
			= Bin (1+ls+rs) rlx (Bin (1+ls+rlls) x l rll) (Bin (1+rrs+rlrs) rx rlr rr)	
	| otherwise
		= Bin (1+ls+rs) x l r
uBalanceR x l=:(Bin ls _ _ _) r=:(Bin rs _ _ _)
	= Bin (1+ls+rs) x l r
		
primes :: [Int]
primes =: [2: [i \\ i <- [3, 5..] | let
		checks :: [Int]
		checks = TakeWhile (\n . i >= n*n) primes
	in All (\n . i rem n <> 0) checks]]

primePrefixes :: [[NumberType]]
primePrefixes =: (Scan removeOverlap [|] [toString p \\ p <- primes])

removeOverlap :: !u:[NumberType] NumberType -> v:[NumberType], [u <= v]
removeOverlap [|] nsub = [|nsub]
removeOverlap [|h: t] nsub
	| indexOf h nsub <> -1
		= removeOverlap t nsub
	| nsub > h
		= [|h: removeOverlap t nsub]
	| otherwise
		= [|nsub, h: Filter (\s = indexOf s nsub == -1) t]

tryMerge :: !NumberType !NumberType -> .Maybe .NumberType
tryMerge a b = first_prefix (max (size a - size b) 0)
where
	sa = size a - 1
	max_len = min sa (size b - 1)
	first_prefix :: !Int -> .Maybe .NumberType
	first_prefix n
		| n > max_len
			= Nothing
		| b%(0,sa-n) == a%(n,sa)
			= Just (a%(0,n-1) +++. b)
		| otherwise
			= first_prefix (inc n)

mergeString :: !NumberType !NumberType -> .NumberType
mergeString a b = first_prefix (max (size a - size b) 0) 
where
	sa = size a - 1
	first_prefix :: !Int -> .NumberType
	first_prefix n
		| b%(0,sa-n) == a%(n,sa)
			= a%(0,n-1) +++. b
		| n == sa
			= a +++. b
		| otherwise
			= first_prefix (inc n)
	
// todo: keep track of merges that we make independent of the resulting whole number
mapCandidatePermsSt :: ![[NumberType]] !u:(Set .NumberType) -> v:(Set NumberType), [u <= v]
mapCandidatePermsSt [|] returnSet = returnSet
mapCandidatePermsSt [h:t] returnSet
	#! (mem, returnSet) = uInsertMSpec (foldl mergeString "" h) returnSet
	= let merges = [removeOverlap h y \\ [x:u=:[_:v]] <- tails h, (Just y) <- Map (tryMerge x) v ++| Map (flip tryMerge x) u]
	in (mapCandidatePermsSt t o if(mem) id (mapCandidatePermsSt merges)) returnSet

containmentNumbersSt =: Tl (containmentNumbersSt` primePrefixes "")
where
	containmentNumbersSt` [p:pref] prev
		| all (\e = indexOf e prev <> -1) p
			= [prev: containmentNumbersSt` pref prev]
		| otherwise
			#! next = uFindMin (mapCandidatePermsSt [p] Tip)
			= [next: containmentNumbersSt` pref next]

minFinder :== (\a b = let sa = size a; sb = size b in if(sa == sb) (a < b) (sa < sb))

Start = [(i, ' ', n, "\n") \\ i <- [1..] & n <- containmentNumbersSt]

Experimente online!

Salvar em main.icl e compile com:clm -fusion -b -IL Dynamics -IL StdEnv -IL Platform main

Isso produz um arquivo a.outque deve ser executado como a.out -h <heap_size>M -s <stack_size>M, onde <heap_size> + <stack_size>está a memória que será usada pelo programa em megabytes.
(Geralmente, defino a pilha como 50 MB, mas raramente os programas usam essa quantidade)


2

Scala , pontuação 137

Editar:

O código aqui simplifica demais o problema.

Portanto, a solução funciona para muitas entradas, mas não para todas.


Mensagem original:

Ideia básica

Problema mais simples

n

Primeiro, geramos o conjunto de números primos e removemos todos os que já são substrings de outros. Em seguida, podemos aplicar várias regras, ou seja, se houver apenas uma sequência terminando em uma sequência e apenas uma começando com a mesma sequência, podemos mesclá-las. Outro seria que, se uma string começa e termina com a mesma sequência (como 101), podemos anexá-la / anexá-la a outra string sem alterar as extremidades. (Essas regras são válidas apenas sob certas condições, portanto, tenha cuidado ao aplicá-las)

n primeiros primos.

O(n4) ou menos.

n=128 ), em que essas regras não são suficientes. Lá, temos que voltar a um algoritmo que leva tempo NP.

O verdadeiro problema

k

10103..............
     ^ we want to know this digit

Depois, podemos pegar nosso algoritmo do problema simplificado para testar se há uma sequência começando com 101030, contendo todosnk101031O(nlog(n))×the time for the simpler algorithm .

Portanto, se as regras no algoritmo acima fossem sempre suficientes, o problema teria mostrado não ser NP-difícil.

findSeqn=128 .

Experimente on-line

n75

Código

import scala.annotation.tailrec

object Better {
  var primeLength: Int = 3
  var knownLengths: Map[(String,List[String]), Int] = Map()

  def main(args: Array[String]): Unit = {
    val start = System.currentTimeMillis()
    var last = ""
    Stream.from(1).foreach { i =>
      primeLength = primeList(i-1).toString.length
      val pcn = if (last.contains(primeList(i-1).toString)) last else calcPrimeContainingNumber(i)
      last = pcn
      if (System.currentTimeMillis() - start > 300 * 1000) // reached the time limit while calculating the last number, so, discard it and exit
        return
      println(i + ": " + pcn)
    }
  }

  def calcPrimeContainingNumber(n: Int): String = {
    val numbers = relevantNumbers(n)
    generateIntegerContainingSeq(numbers, numOfDigitsRequired(numbers, "X"), "X").tail
  }

  def relevantNumbers(n: Int): List[String] = {
    val primesRaw = primeList.take(n)
    val primes = primesRaw.map(_.toString).foldRight(List[String]())((i, l) => if (l.exists(_.contains(i))) l else i +: l)
    primes.sorted
  }

  @tailrec
  def generateIntegerContainingSeq(numbers: List[String], maxDigits: Int, soFar: String): String = {
    if (numbers.isEmpty)
      return soFar
    val nextDigit = (0 to 9).find(i => numOfDigitsRequired(numbers.filterNot((soFar + i).contains), soFar + i) == maxDigits).get
    generateIntegerContainingSeq(numbers.filterNot((soFar + nextDigit).contains), maxDigits, soFar + nextDigit)
  }

  def numOfDigitsRequired(numbers: List[String], soFar: String): Int = {
    soFar.length +
      knownLengths.getOrElse((soFar.takeRight(primeLength - 1), numbers), {
        val len = findAnySeq(soFar :: numbers).length - soFar.length
        knownLengths += (soFar.takeRight(primeLength - 1), numbers) -> len
        len
      })
  }

  def findAnySeq(numbers: List[String]): String = {
    val tails = numbers.flatMap(_.tails.drop(1).toSeq.dropRight(1)).distinct
      .filter(t => numbers.exists(n1 => n1.startsWith(t) && numbers.exists(n2 => n1 != n2 && n2.endsWith(t)))) // require different strings for start & end
      .sorted.sortBy(-_.length)
    val safeTails = tails.filterNot(t1 => tails.exists(t2 => t1 != t2 && t2.contains(t1))) // all those which are not substring of another tail

    @inline def merge(e: String, s: String, i: Int): String = findAnySeq((numbers diff List(e, s)) :+ (e + s.drop(i)))

    safeTails.foreach { overlap =>
      val ending = numbers.filter(_.endsWith(overlap))
      val starting = numbers.filter(_.startsWith(overlap))
      if (ending.nonEmpty && starting.nonEmpty) {
        if (ending.size == 1 && starting.size == 1 && ending != starting) { // there is really only one way
          return merge(ending.head, starting.head, overlap.length)
        }
        val startingAndEnding = ending.filter(_.startsWith(overlap))
        if (startingAndEnding.nonEmpty && ending.size > 1) {
          return merge(ending.filter(_ != startingAndEnding.head).head, startingAndEnding.head, overlap.length)
        } else if (startingAndEnding.nonEmpty && starting.size > 1) {
          return merge(startingAndEnding.head, starting.filter(_ != startingAndEnding.head).head, overlap.length)
        }
      }
    }

    @inline def startsRelevant(n: String): Boolean = tails.exists(n.startsWith)

    @inline def endsRelevant(n: String): Boolean = tails.exists(n.endsWith)

    safeTails.foreach { overlap =>
      val ending = numbers.filter(_.endsWith(overlap))
      val starting = numbers.filter(_.startsWith(overlap))
      ending.find(!startsRelevant(_)).foreach { e =>
        starting.find(endsRelevant)
          .orElse(starting.headOption) // if there is no relevant starting, take head (ending is already shown to be irrelevant)
          .foreach { s =>
          return merge(e, s, overlap.length)
        }
      }
      ending.find(startsRelevant).foreach { e =>
        starting.find(!endsRelevant(_)).foreach { s =>
          return merge(e, s, overlap.length)
        }
      }
    }
    safeTails.foreach { overlap =>
      val ending = numbers.filter(_.endsWith(overlap))
      val starting = numbers.filter(_.startsWith(overlap))
      return ending
        .flatMap(e => starting.filter(_ != e).map(s => merge(e, s, overlap.length)))
        .minBy(_.length)
    }

    if (tails.nonEmpty)
      throw new Error("that was unexpected :( " + numbers)

    numbers.mkString("")
  }


  // 1k primes
  val primeList = Seq(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
    , 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173
    , 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281
    , 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409
    , 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541
    , 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659
    , 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809
    , 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941
    , 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069
    , 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223
    , 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373
    , 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511
    , 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657
    , 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811
    , 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987
    , 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129
    , 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287
    , 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423
    , 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617
    , 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741
    , 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903
    , 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079
    , 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257
    , 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413
    , 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571
    , 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727
    , 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907
    , 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057
    , 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231
    , 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409
    , 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583
    , 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751
    , 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937
    , 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087
    , 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279
    , 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443
    , 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639
    , 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791
    , 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939
    , 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133
    , 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301
    , 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473
    , 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673
    , 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833
    , 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997
    , 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207
    , 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411
    , 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561
    , 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723
    , 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919)
}

Como Anders Kaseorg apontou nos comentários, esse código pode retornar resultados abaixo do ideal (portanto, incorretos).

Resultados

Os resultados para n[1,200]187188189193

1: 2
2: 23
3: 235
4: 2357
5: 112357
6: 113257
7: 1131725
8: 113171925
9: 1131719235
10: 113171923295
11: 113171923295
12: 1131719237295
13: 11317237294195
14: 1131723294194375
15: 113172329419437475
16: 1131723294194347537
17: 113172329419434753759
18: 2311329417434753759619
19: 231132941743475375961967
20: 2311294134347175375961967
21: 23112941343471735375961967
22: 231129413434717353759619679
23: 23112941343471735359619678379
24: 2311294134347173535961967837989
25: 23112941343471735359619678378979
26: 2310112941343471735359619678378979
27: 231010329411343471735359619678378979
28: 101031071132329417343475359619678378979
29: 101031071091132329417343475359619678378979
30: 101031071091132329417343475359619678378979
31: 101031071091131272329417343475359619678378979
32: 101031071091131272329417343475359619678378979
33: 10103107109113127137232941734347535961967838979
34: 10103107109113127137139232941734347535961967838979
35: 10103107109113127137139149232941734347535961967838979
36: 1010310710911312713713914923294151734347535961967838979
37: 1010310710911312713713914915157232941734347535961967838979
38: 1010310710911312713713914915157163232941734347535961967838979
39: 10103107109113127137139149151571631672329417343475359619798389
40: 10103107109113127137139149151571631672329417343475359619798389
41: 1010310710911312713713914915157163167173232941794347535961978389
42: 101031071091131271371391491515716316717323294179434753596181978389
43: 101031071091131271371391491515716316723294173434753596181917978389
44: 101031071091131271371391491515716316717323294179434753596181919383897
45: 10103107109113127137139149151571631671731792329418191934347535961978389
46: 10103107109113127137139149151571631671731791819193232941974347535961998389
47: 101031071091271313714915157163167173179181919321139232941974347535961998389
48: 1010310710912713137149151571631671731791819193211392232941974347535961998389
49: 1010310710912713137149151571631671731791819193211392232272941974347535961998389
50: 10103107109127131371491515716316717317918191932113922322722941974347535961998389
51: 101031071091271313714915157163167173179181919321139223322722941974347535961998389
52: 101031071091271313714915157163167173179181919321139223322722923941974347535961998389
53: 1010310710912713137149151571631671731791819193211392233227229239241974347535961998389
54: 101031071091271313714915157163167173179211392233227229239241819193251974347535961998389
55: 101031071091271313714915157163167173179211392233227229239241819193251972574347535961998389
56: 101031071091271313714915157163167173179211392233227229239241819193251972572634347535961998389
57: 101031071091271313714915157163167173179211392233227229239241819193251972572632694347535961998389
58: 101031071091271313714915157163167173179211392233227229239241819193251972572632694347535961998389
59: 1010310710912713137149151571631671731792113922332277229239241819193251972572632694347535961998389
60: 101031071091271313714915157163167173211392233227722923924179251819193257263269281974347535961998389
61: 1010310710912713137149151571631671732113922332277229239241792518191932572632692819728343475359619989
62: 10103107109127131371491515716316717321139223322772293239241792518191932572632692819728343475359619989
63: 1010307107109127131371491515716316717321139223322772293239241792518191932572632692819728343475359619989
64: 10103071071091271311371391491515716316721173223322772293239241792518191932572632692819728343475359619989
65: 10103071071091271311371491515716313916721173223322772293239241792518191932572632692819728343475359619989
66: 10103071071091271311371491515716313921167223317322772293239241792518191932572632692819728343475359619989
67: 10103071071091271311371491515716313921167223317322772293239241792518191932572632692819728343475359619989
68: 1010307107109127131137149151571631392116722331732277229323924179251819193257263269281972833743475359619989
69: 1010307107109127131137149151571631392116722331732277229323924179251819193257263269281972833743475359619989
70: 101030710710912713113714915157163139211672233173227722932392417925181919325726326928197283374347534959619989
71: 101030710710912713113714915157163139211672233173227722932392417925181919325726337269281972834743534959619989
72: 101030710710912713113714915157163139211672233173227722932392417925181919337257263472692819728349435359619989
73: 10103071071091271311371491515716313921167223317322772293372392417925181919347257263492692819728353594367619989
74: 101030710710912713113714915157163139211672233173227722932392417925181919337347257263492692819728353594367619989
75: 1010307107109127131137313914915157163211672233173227722933792392417925181919347257263492692819728353594367619989
76: 101030710710912713113731391491515716321167223317322772293379239241792518191934725726349269281972835359438367619989
77: 101030710710912713113731391491515716321167223317337922772293472392417925181919349257263535926928197283674383896199
78: 1010307107109127131137313914915157163211672233173379227722934723972417925181919349257263535926928197283674383896199
79: 101030710710912713113731391491515721163223317337922772293472397241672517925726349269281819193535928367401974383896199
80: 101030710710912713113731391491515721163223317337922772293472397241672517925726349269281819193535928367401974094383896199
81: 101030710710912713113731391491515721163223317337922772293472397241916725179257263492692818193535928367401974094383896199
82: 1010307107109127131137313914915157223317322772293379239724191634725167257263492692817928353594018193674094211974383896199
83: 1010307107109127131137313914922331515722772293379239724191634725167257263492692817353592836740181938389409421197431796199
84: 101030710710912713113731391492233151572277229323972419163472516725726349269281735359283674018193838940942119743179433796199
85: 101030710710912713113731391492233151572277229323924191634725167257263492692817353592836740181938389409421197431794337943976199
86: 1010307107109127131137313914922331515722772293239241916347251672572634926928173535928367401819383894094211974317943379443976199
87: 1010307107109127131137313914922331515722772293239241916347251672572634926928173535928367401819383894094211974317943379443974496199
88: 1010307107109127131137313914922331515722772293239241916347251672572634926928173535928367401819383894094211974317943379443974494576199
89: 10103071071091271311373139149223315157227722932392419163472516725726349269281735359283674018193838940942119743179433794439744945746199
90: 10103071071091271311373139149223315157227722932392419163251672572634726928173492835359401819367409421197431794337944397449457461994638389
91: 10103071071091271311373139149223315157227722932392419163251672572634726928173492835359401819367409421197431794337944397449457461994638389467
92: 101030710710912713113731391492233151572277229323924191632516725726347926928173492835359401819367409421197431794337944397449457461994638389467
93: 101030710710912713113731391492233151572277229323924191632516725726347926928173492835359401819367409421197431794337944397449457461994638389467487
94: 101030710710912713113731392233149151572277229323924191632516725726347926928173492835359401819367409421197431794337944397449457461994638389467487
95: 1010307107109127131137313922331491515722772293239241916325167257263479269281734928353594018193674094211974317943379443974499457461994638389467487
96: 1010307107109127131137313922331491515722772293239241916325167257263269281734792834940181935359409421197431794337944397449945746199463674674875038389
97: 1010307107109127131137313922331491515722772293239241916325167257263269281734792834940181935359409421197431794337944397449945746199463674674875038389509
98: 101030710710912713113732233139227722932392419149151572516325726326928167283479401734940942118193535943179433794439744994574619746367467487503838950952199
99: 1010307107109127131137322331392277229324191491515725163257263269281672834794017349409421181935359431794337944394499457461974636746748750383895095219952397
100: 101030710710922331127131373227722932414915157251632572632692816728347940173494094211394317943379443944994574618191935359463674674875038389509521975239754199
101: 101030710710922331127131373227722932414915157251632572632692816728347401734940942113943179433794439449945746181919353594636746748750383895095219752397541995479
102: 101030710710922331127131373227722932414915157251632572632692816728347401734940942113943179433794439449945746181919353594636746748750383895095219752397541995479557
103: 101030710710922331127131373227722932414915157251632572632692816728340173474094211394317943379443944945746181919349946353594674875036750952197523975419954795575638389
104: 101030710710922331127131373227722932414915157251632572632692816728340173474094211394317943379443944945746181919349946353594674875036750952197523975419954795575638389569
105: 101030710722331109227127722932413137325149151571632572632692816728340173474094211394317943379443944945746181919349946353594674875036750952197523975419954795575638389569
106: 1010307107223311092271277229324131373251491515716325726326928167283401734740942113943179433794439449457461819193499463535946748750367509521975239754199547955775638389569
107: 1010307107223311092271277229324131373251491515716325726326928167283401734740942113943179433794439449457461819193499463535946748750367509521975239754199547955775638389569587
108: 10103071072233110922712772293241313732514915157163257263269281672834017340942113943179433794439449457461819193474634994674875035359367509521975239754199547955775638389569587
109: 10103071072233110922712772293241313732514915157163257263269281672834017340942113943179433794439449457461819193474634994674875035359367509521975239754199547955775638389569587599
110: 1010307223311072271092293241277251313732571491515726326928163283401674094211394317343379443944945746179463474674875034995095218191935359367523975419754795577563838956958759960199
111: 1010307223311072271092293241277251313732571491515726326928163283401674094211394317343379443944945746179463474674875034995095218191935359367523975419754795577563838956958759960199607
112: 1010307223311072271092293241277251491515716325726326928167283401734094211313734317943379443944945746139463474674875034995095218191935359367523975419754795577563838956958759960199607
113: 22331101030722710722932410925127725714915157263269281632834016740942113137343173433794439449457461394634746748750349950952181919353593675239754197547955775638389569587599601996076179
114: 2233110103072271072293241092512571277263269281491515728340163409421131373431734337944394494574613946347467487503499509521675239754191819353593675479557756383895695875996019760761796199
115: 22331010307227107229324109251257126311277269281491515728340163409421131373431734337944394494574613946347467487503499509521675239754191819353593675479557756383895695875996019760761796199
116: 22331010307227107229324109251257126311269281277283401491515740942113137343173433794439449457461394634674875034750952163499523975416754795577563535936756958759960181919383896076179619764199
117: 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346748750347509521634995239541675479557756353593675695875996018191938389607617961976419964397
118: 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346748750347509521634995239541675475577563535936756958759960181919383896076179619764199643976479
119: 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346748750347509521634995239541675475577563535695875935996018191936760761796197641996439764796538389
120: 2233101030722710722932410925125712631126928127728340149151574094211313734317344337944945746139463467487503475095216349952395416754755775635356958760181919359367607617961976419964397647965383896599
121: 22331010307227107229324109251257126311269281277283401491515740942113137343173443379449457461394634674875034750952163499523954167547557756353569587601819193593676076179641976439764796538389659966199
122: 223310103072271072293241092512571263112692812772834014915157409421131373431734433794494574613946346734748750349950952163523954167547557756353569587601819193593676076179641976439764796538389659966199
123: 2233101030722710722932410925125712631126928127728340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936776076179641976439764796538389659966199
124: 2233101030722710722932410925125712631126928127728340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
125: 22331010307227107229324109251257126311269127728128340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
126: 2233101030701072271092293241251257126311269127728128340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
127: 223310103070107092271092293241251257126311269127728128340149151574094211313734317344337944945746139463467347487503499509521635239541675475577563535695876018191935936076179641976439764796536776599661996838389
128: 223310103070107092271092293241251257191263112691277281283401491515740942113137343173443379449457461394634673474875034995095216352395416754755775635356958760181935936076179641976439764796536776599661996838389
129: 22331010307010709227109229324125125719126311269127277281283401491515740942113137343173443379449457461394634673474875034995095216352395416754755775635356958760181935936076179641976439764796536776599661996838389
130: 223307010103227092293241072510925712631126912719128128340140942113137331491515727743173443379449457461394634673474875034995095216352395416754755775635356958760181935936076179641976439764796536776599661996838389
131: 2233070101032270922932410725109257126311269127191281283401409421131373314915157277431734433794494574613946346739487503475095216349952395416754755775635356958760181935936076179641976439764796536776599661996838389
132: 2233070101032270922932410725109257126311269127191281283401409421131373314915157277431734433794494574613946346739487503475095216349952395416754755775635356958760181935936076179641976439764796536776599661996838389
133: 223307010103227092293241072510925712631126912719128128340140942113137331443173449149457277433794613946346739487503475095215157516349952395416754755775635356958760181935936076179641976439764796536776599661996838389
134: 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727743379461394634673948750347509521515751634995239541675475575635356958757760181935936076179641976439764796536776599661996838389
135: 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727743379461394634673948750347509521515751634995239541675475575635356958757760181935936076179641976439764796536776599661996838389
136: 2233070101032270922932410725109257126311269127191281283401409421131373314431734491494572774337946139463467394875034750952151575163499523954167547557563535695875776018193593607617964197643976479653677696599661996838389
137: 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479653677696599661996838389
138: 2233070101032270922932410725109257126311269127191281283401409421131373314431734491494572773461394634673948743379503475095215157516349952395416754755756353569587577601819359360761796419764397647965367787696599661996838389
139: 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479765367787696599661996838389
140: 22330701010322709229324107251092571263112691271912812834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479765367787696599661996838389809
141: 223307010103227092293241072510925712631126912719128112834014094211313733144317344914945727734613946346739487433795034750952151575163499523954167547557563535695875776018193593607617964197643976479765367787696599661996838389809
142: 223307010103227092293241072510925712631126912719128112834014094211313733144317344914572773461394634673948743379503475095214952395415157516349954755756353569587577601676076179641935936439764797653677659966197876968383898098218199
143: 223070101032270922932410725109257126311269127191281128340140942113137331443173449145727734613946346739487433475034950952149952337954151575163535475575635695875776016760761796419359364396479765367765996619768383898098218199823978769
144: 223070101032270922932410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151575163535475575635695875773960167607617964193593643964797653677659966197683838980982181998239769827787
145: 223070101032270922924107251092571263112691271912811283401409421131373314431734491457274334613946346734748750349509521499523379541515751635354755756356958757739601676076179641935936439647976536599661976836776980982181998239782778782938389
146: 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587577396016760761796419359364396479765367765996619768383976980982181998239827787829389
147: 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587577396016760761796419359364396479765365996619768367769809821819982397827787829383985389
148: 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587576016760761796419359364396479765365996619768367739769809821819982398277829383985389857787
149: 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587576016760761796419359364396479765365966197683677397698098218199823982778293839853898577878599
150: 2230701010322709229241072510925712631126912719128112834014094211313733144317344914572743346139463467347487503495095214995233795415157516353547557563569587576016760761796419359364396479765365966197683677397698098218199823982778293839853857787859986389
151: 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151575163535475575635695875760167607617964193593643964797653659661976836773976980982181998239827782938398538577877859986389
152: 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383985385778778599863898818199
153: 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857787785998638988181998839
154: 22307010103227092292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857785998638988181998839887787
155: 2230701010322709072292410725109257126311269127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857785998638988181998839887787
156: 22307010103227090722924107251092571263112691127191281128340140942113137331443173449145727433461394634673474875034950952149952337954151547515755756353569587576016359360761796419364396479765365966197683676980982167739782398277829383853857785998638988181998839887787
157: 22307010103227090722924107251092571263112691127191281128340140942113137331443173449193457274334613946346734748750349509521499523379541515475155756353569587576015760761796419764396479765359365966199683676980982163823978277398293838538577859986389881816778778839887
158: 2230701010322709072292410725109257126311269112719128112834014092934211313733144317344919345727433461394634673474875034950952149952337954151547515575635356958757601576076179641976439647976535936596619968367698098216382397827739829853838577859986389881816778778839887
159: 22307010103227090722924107251092571263112691127191281128340140929342113137274314433173344919345746139463467347487503495095214995233735354151547515575635695875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
160: 2230701010322709072292410725109257126311269112719128112834014092934211313727431443317334491934574613941463467347487503495095214995233735354151547515575635695875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
161: 223070101032270907229241072510925712631126911271912811283401409293421131372743144331733449193457461394146346734748750349475095214995233735354151547515575635695875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
162: 22307010103227090722924107251092571263112691127191281128340140929342113137274314433173344919345746139414634673474875034947509521499523373535415154751557563569535875760157607617964197643964796535937976596619968367698098216382397827739829853838577859986389881816778778839887
163: 2230701010322709072292410725109257126311269112719128112834014092934211313727431443317334491934574613941463467347487503494750952149952337353541515475155756356953587576015760761796419764396479653593797659661996768367698098216382397827739829853838577859986389881816778778839887
164: 22307010103227090722924107251092571263112691127128112834014092934211313727431443317334491457461394146346734748750349475095214995233735354151547515575635695358757601576076179641919359379643964797197653659661996768367698098216382397827739829853838577859986389881816778778839887
165: 223070101032270907229241072510925712631126911271281128340140929342113137274314433173344914574613941463467347487503494750952149952337353541515475155756356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829853838577859986389881816778778839887
166: 22307010103227090722924107251092571263112691127128112834014092934211313727431443317334491457461394146346734748750349475095214995233735354151547515575635695358757601576076179641919359379643964797197653659661996768367698098216382397739827782983838538577859986389881816778778839887
167: 223070101032270907229241072510925712631126911271281128340140929342113137274314433173344914574613941463467347487503494750952149915152337353541547515575635695358757601576076179641919359379643964797197653659661996768367698098216382397739827782983838538577859986389881816778778839887
168: 2230701010322709072292410725109257126311269112712811283401409293421131372743144331733449145746139414634673474875034947509521499151523373535415475155756356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829838385385778599786389881816778778839887
169: 2230701009070922710103229241072510925712631126911272728112834014092934211313733144317344914574334613941463467347487503494750952149915152337515415475575635356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829838385385778599786389881816778778839887
170: 22307010090709227101310322924107251092571263112691127272811283401409293421134431373317344914574334613941463467347487503494750952149915152337515415475575635356953587576015760761796419193593796439647971976536596619967683676980982163823977398277829838385385778599786389881816778778839887
171: 22307010090709227101310191032292410725109257126311269112727281128340140929342113443137331734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
172: 22307010090709227101310191021032292410725109257126311269112727281128340140929342113443137331734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
173: 223070100907092271013101910210310722924109251257126311269112727281128340140929342113443137331734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
174: 223070100907092271013101910210310331107229241092512571263132691127272811283401409293421137334431734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
175: 223070100907092271013101910210310331103922924107251092571263132691127272811283401409293421137334431734491457433461394146346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
176: 223070100907092271013101910210310331103922924104910725109257126313269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
177: 223070100907092271013101910210310331103922924104910510725109257126313269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
178: 223070100907092271013101910210310331103922924104910510610725109257126313269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
179: 223070100907092271013101910210310331103922924104910510610631325107257109263269112727281128340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
180: 223070100907092271013101910210310331103922924104910510610631325106911072571092632692811272728340140929342113733443173449414574334613946346734748750349475095214991935233751515415475575635356953587576015760761796419643964796535937971976596619967683676980982163823977398277829838385385778599786389881816778778839887
181: 223070100907092271013101910210310331103922924104910510610631325106911072571087263269281092834012727409293421137334431734494145743346139463467347487503494750952149919352337515154154755756353569535875760157607617964196439647965359379719765966199676836769809821638239773982778298383853857785997863898811816778778839887
182: 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109112727283401409293421137334431734494145743346139463467347487503494750952149919352337515154154755756353569535875760157607617964196439647965359379719765966199676836769809821638239773982778298383853857785997863898811816778778839887
183: 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834012727409293421137334431734494145743346139463467347487503494750952149919352337515154154755756353569535875760157607617964196439647965359379719765966199676836769809821638239773982778298383853857785997863898811816778778839887
184: 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834010971929340941272742113733443173449457433461394634673474875034947509521499193523375151541547557563535695358757601576076179641976439647965359379765966199676836769809821638239773982778298383853857785997863898811816778778839887
185: 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834010971929340941272742113733443173449457433461394634673474875034947509521499193523375151541547557563535695358757601576076179641976439647965359379765966199676836769809821638239773982778298383853857785997863898811816778778839887
186: 2230701009070922710131019102103103311039229241049105106106313251069107257108726326928109110932834010971929340941272742113733443173449457433461394634673474875034947509521499193523375151541547557563535695358757601576076179641976439647965359379765966199676836769809821638239773982778298383853857785997863898811816778778839887
187: 223070100907092271013101910210310331103922924104910510610631325106910725710872632692810911093283401097192934094127274211173344317433449457461373463467347487503494750952149919352337515154157547557563535695358757601635937960761796419764396479765365966199676836769809821811397739823982778298383853857785997863898816778778839887
188: 223070100907092271013101910210310331103922924104910510610631325106910725710872632692810911093283401097192934094111727421123344317334494574337346137463467347487503494750952127514991935235354151575475575635695358757601635937960761796419764396479765365966199676836769809821811397739823982778298383853857785997863898816778778839887
189: 1009070101307092232271019102103103310491051061063110392292410691072510872571091109326326928109719283401117274092934211233443131733449411294574337346137463467347487503494750952127514991935235354151575475575635695358757601635937960761796419764396479765365966199676836769809821811397739823982778298383853857785997863898816778778839887
190: 10090701013070922322710191021031033104910510610631103922924106910725108725710911093263269281097192834011172740929342112334431317334494112945743373461374634673474875034947509521139523535412751499193547557563569535875760157607617964197643964796535937976596619967683676980982163823977398277829838385385778599786389881151816778778839887
191: 100907010130709101910210310331049105106106311039223227106910722924108725109110932571097192632692811172728340112334092934211294113137334431734494574337461394634673474875034947509521151153523535412751499193547557563569535875760157607617964197643964796535937976596619967683676980982163823977398277829838385385778599786389881816778778839887
192: 1009070101307091019102103103310491051061063110392232271069107229241087251091109325710971926326928111727283401123340929342112941131373344317344945743374613946346734748750349475095211511535235354116354751275575635695358757601499193593796076179641976439647976536596619967683676980982157739778239827782983838538578599786389881816778778839887
193: 1009070101307092232271019102103103310491051061063110392292410691072510872571091109326326928109711171928340112334092934211294113137274317334433734494574613946346734748750349475095211511535235354127514991935475575635695358757601576076179641976439647965359379765966199676836769809821811638239773982778298383853857785997863898816778778839887
194: 10090701013070922322710191021031033104910510610631103922924106910725108725710911093263269281097111719283401123340929342112941131372743173344337344945746139463467347487503494750952115115352353541163547512755756356953587576014991935937960761796419764396479765365966199676836769809821577397782398277829838385385785997863898811816778778839887
195: 100907010130709101910210310331049105106106311039223227106910722924108725109110932571097111719263269281123283401129293409411313727421151153443173344945743346139463467347487503494750952116352337353541181187512754755756356953587576014991935937960761796419764396479765365966199676836769809821577397782398277829838385385785997863898816778778839887
196: 100907010130709101910210310331049105106106310691072231103922710872292410911093251097111711232571926326928112928340113137274092934211511534431733449411634574334613946346734748750349475095211811875119352337353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
197: 100907010130709101910210310331049105106106310691072231103922710872292410911093251097111711232571926326928112928340113137274092934211511534431733449411634574334613946346734748750349475095211811875119352337353541201275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
198: 1009070101307091019102103103310491051061063106910710872231103922710911093229241097111711232511292571926326928113132834011511534092934211634431733449411811872743345746137346346734748750349475095211935233751201213953535412754755756356958757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
199: 10090701013070910191021031033104910510610631069107108710911039223110932271097111711232292411292511313257192632692811511532834011634092934211811872743173344334494119345746137346346734748750349475095212012139523375121754127547557563535695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
200: 100907010130709101910210310331049105106106310691071087109109311039110971117112322711292292411313251151153257192632692811632834011811872740929342119344317334494120121373457433461394634673474875034947509521217512233752353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
201: 1009070101307091019102103103310491051061063106910710871091093110391109711171123112922711313241151153251163257192632692811811872728340120121373340929342119344317344941217433457461394634673474875034947509521223375122952353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
202: 1009070101307091019102103103310491051061063106910710871091093110391109711171123112922711313241151153251163257192632692811811872728340120121373340929342119344317344941217433457461394634673474875034947509521223375122952353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
203: 10090701013070910191021031033104910510610631069107108710910931103911097111711231129113132271151153241163251181187257192632692812012137272834012173340929342119344317433449412233734574613946346734748750349475095212295235354123751275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
204: 100907010130709101910210310331049105106106310691071087109109311039110971117112311291131151153132271163241181187251201213725719263269281217272834012233409293421193443173344941229457433734613946346734748750349475095212375124952353541275475575635695358757601499196076179641976439647965359379765966199676836769809821577397782398277829838385385785997863898816778778839887
205: 1009070101307091019102103103310491051061063106910710871091093110391109711171123112911311511531163132271181187241201213725121725719263269281223283401229293409412372742119344317334494574334613946346734748750349475095212495233735354125937953547512755756356958757601499196076179641976439647976535965966199676836769809821577397782398277829838385385785997863898816778778839887
206: 1009070101307091019102103103310491051061063106910710871091093110391109711171123112911311511531163132271181187241201213725121725719263269281223283401229293409412372742119344317334494574334613946346734748750349475095212495233735354125937953547512773955756356958757601499196076179641976439647976535965966199676836769809821577823977827829838385385785997863898816778778839887
207: 10090701013070910191021031033104910510610631069107108710910931103911097111711231129113115115311631181187227120121313724121725122325719263269281229283401237274092934211934431733449412494574334613946346734748750349475095212593735233795353541277395475127955756356958757601499196076179641976439647976535965966199676836769809821577823977827829838385385785997863898816778778839887
208: 100907010130709101910210310331049105106106310691071087109109311039110971117112311291131151153116311811871201213137227121724122325122925719263269281237274012492934094125934211937334431734494574334613946346734748750349475095212773952337953535412795475128355756356958757601499196076179641976439647976535965966199676836769809821577823977827829838385385785997863898816778778839887
209: 1009070101307091019102103103310491051061063106910710871091093110391109711171123112911311511531163118118712012131217227122313724122925123725719263269281249293401259340941277274211937334431734494574334613946346734748750349475095212795233795353541283547512895575635695875760149919607617964197643964797653596596619967683676980982157739778239827829838385385785997863898816778778839887
210: 1009070101307091019102103103310491051061063106910710871091093110391109711171123112911311511531163118118712012131217227122313724122925123725719263269281249293401259340941277274211937334431734494574334613946346734748750349475095212795233795353541283547512895575635695875760149919607617964197643964797653596596619967683676980982157739778239827829838385385785997863898816778778839887
211: 10090701013070910191021031033104910510610631069107108710910931103911097111711231129113115115311631181187120121312171223137227122924123725124925719263269281259293401277274094127942119344317334494574334613946346734748750349475095212835233735354128953547512975575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
212: 100907010130101910210310330709104910510610631069107108710910931103911097111711231129113115115311631181187120121312171223227122924123725124925719263269281259293401277274094127942119344313733173449457433461394634673474875034947509521283523375128953535412975475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
213: 10090701013010191021031033070910491051061063106910710871091093110391109711171123112911303115115311631181187120121312171223227122924123725124925719263269281259293401277274094127942119344313733173449457433461394634673474875034947509521283523375128953535412975475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
214: 1009070101301019102103103310491051061063106910709108710910931103911097111711231129113031151153116311811871201213071217122312292271237241249251259257192632692812772740127929340941283421193443131733449457433461373463467347487503494750952128952337512975413953535475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
215: 100907010130101910210310331049105106106310691070910871091093110391109711171123112911303115115311631181187120121307121712231229227123724124925125925719263131926928127727401279293409412834211934431733449457433461373463467347487503494750952128952337512975413953535475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
216: 100907010130101910210310331049105106106310691070910871091093110391109711171123112911303115115311631181187120121307121712231229227123724124925125925719263131926928127727401279293409412834211934431733449457433461321289463467347487503494750952129751373523375413953535475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
217: 1009070101301019102103103310491051061063106910709108710910931103911097111711231129113031151153116311811871201213071217122312291237227124924125925127725719263131926928127929340128340941289421193443173344945727433461321297463467347487503494750952132751373523375413953535475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
218: 1009070101301019102103103310491051061063106910709108710910931103911097111711231129113031151153116311811871201213071217122312291237227124924125925127725719263131926928127929340128340941289421193443173344945727433461297463467347487503494750952132132751361373523375413953535475575635695875760149919607617964197643964796535937976596619967683676980982157739778239827829838385385785997863898816778778839887
219: 100907010130101910210310331049105106106310691070910871091093110391109711171123112911303115115311631181187120121307121712231229123712492271259241277251279257192631319269281283401289293409412972742119344317334494574334613213274634673474875034947509521361367513735233754139535354755756356958757601499196076179641976439647965359379765966199676838098215769823977398278298383853857785997863898816778778839887
220: 100907010130101910210310331049105106106310691070910871091093110391109711171123112911303115115311631181187120121307121712231229123712492271259241277251279257192631319269281283401289293409412972742119344317334494574334613213274634673474875034947509521361367513735233754139535354755756356958757601499196076179641976439647965359379765966199676838098215769823977398278298383853857785997863898816778778839887
221: 100907010130101910210310331049105106106310691070910871091093110391109711171123112911303115115311631181187120121307121712231229123712492271259241277251279257192631319269281283401289293409412972742119344317334494574334613213274634673474875034947509521361367513735233754138139535354755756356958757601499196076179641976439647965359379765966199676838098215769823977398278298383853857785997863898816778778839887
222: 1009070101301019102103103310491051061063106910709108710910931103911097111711231129113031151153116311811871201213071217122312291237124922712592412772512792571926313192692812834012892934094129727421193443173344945743346132132746346734748750349475095213613675137352337541381399195353547557563569587576014996076179641976439647965359379765966199676838098215769823977398278298383853857785997863898816778778839887

Sabe-se que o menor problema de superssequência comum é NP-completo , portanto, um algoritmo de tempo polinomial sem retorno pode não funcionar em todos os casos, a menos que sua correção dependa de alguma propriedade peculiar da distribuição de números primos (ou P = NP).
Anders Kaseorg

n>>0n=128

1
Dadas essas advertências como “na maioria das vezes” e “encontradas até agora”, você pode explicar por que devemos confiar que sua saída está correta? Como você pode ter certeza de que uma de suas simplificações locais não impedirá que você encontre o melhor global?
Anders Kaseorg

4
Por exemplo: se você substituir os três primeiros números primos com 1234, 3423,2345 , você gera 123453423em vez do ideal 12342345.
Anders Kaseorg

1
Além disso, aqui está um caso de problema de três dígitos: 457, 571, 757(todos os números primos). findSeqretornaria 7574571para isso, mas o menor comprimento é 457571. Portanto, sua abordagem está brincando com fogo. Promovido por pura audácia, no entanto.
japh
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.