打印/输出所有正数,其中十进制表示形式的每个多位数字子字符串也为质数。


15

任务

您的任务是打印或输出所有数,其中十进制表示形式的每个多位数字子字符串也都是质数。如果该数字至少包含2位数字,则意味着该数字本身也需要为质数。

  • 6197是序列中,因为每一个多位数的子串6197是素数,即:6119976191976197(本身)。
  • 请注意,这6不是质数,但6197仍处于序列中,因为6它不是的多位数子串6197
  • 8也处于序列中,因为其中的每个多位数字子串8都是质数。里面没有多位数的子串8,所以这是虚无的事实

眼镜

  • 除了允许您对输出进行硬编码或将与输出有关的信息存储在程序中之外,存在标准漏洞
  • 输出中的数字可以任意顺序
  • 输出中的数字允许重复。
  • 如果选择打印而不是输出,则可以使用任何分隔符
  • 你是允许的,如果你选择要打印的,而不是输出到前缀和/或后缀输出。
  • 分隔符,前缀和后缀不得包含任何数字(U + 0030至U + 0039)。

全部列表(58个项目)

1
2
3
4
5
6
7
8
9
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
113
131
137
173
179
197
311
313
317
373
379
419
431
479
613
617
619
673
719
797
971
1373
3137
3797
6131
6173
6197
9719

参考


与往常一样,请随时在评论中提及我需要澄清的任何内容。


2
我将给@Fatalize以外的任何人+300悬赏,只要他在Brachylogwiki链接)(TIO链接)(聊天室)中提交对此挑战的最小答案。
Leaky Nun

2
可怜的@Fatalize。那就是您创造语言的
乐趣

3
我有50个字节的答案:(
Fatalize 16'Aug

1
程序必须终止吗?
致命

2
@LeakyNun看起来有人会得到赏金!
约旦

Answers:



7

05AB1E15 13字节

码:

4°GN§ŒD9›ÏpP–

说明:

  G            # For N in range 1,
4°             #   10000
   N           # Push N
    §          # Convert that to string
     Œ         # Get all substrings
      D9›Ï     # Keep all substrings that are greater than 9
          p    # Check each of them if they are prime
           P   # Product
            –  # If 1, print N

使用CP-1252编码。在线尝试!(可能需要几秒钟)。


5

Brachylog18 17 15 16 15字节

ℕ₁<l4&≜sᶠ{Ḋ|ṗ}ᵐ

在线尝试!

与Fatalize讨论后的-1个字节启发了我,看看我交换 l<周围。

只要输出变量不受约束,此谓词就通过输入变量生成输出。由于允许重复,因此每个数字的乘数等于其素数数字的幂次幂。

ℕ₁                 The input variable is a natural number
  <                less than
   l4              some number with length 4 (maximized as 9999).
     &≜            Assign a number to the input, and assert that
       sᶠ          every substring of it
         { | }ᵐ    is either
            ṗ      a prime number
          Ḋ        or a single digit.

旧版本:

{≜ℕsᶠ{Ḋ!|ṗ}ᵐ&}ᶠ⁵⁹b
7^₅⟦₁{sᶠ{Ḋ|ṗ}ᵐ&}ˢ
8ḟ⟦₁{sᶠ{Ḋ|ṗ}ᵐ&}ˢ
∧8ḟ>?ℕ₁≜sᶠ{Ḋ|ṗ}ᵐ

这是16个字节,但未经测试,因为检查所有不超过40320的内容并不十分快速:8ḟ⟦₁{sᶠ{Ḋ|ṗ}ᵐ&}ˢ
不相关的字符串

但是,如果上限为10000,则终止会很好:tio.run/##SypKTM6ozMlPN/r/39AACB7NX/…
不相关的字符串

4

Brachylog,18个字节

另一个Brachylog解决方案。我比Erik The Outgolfer的Brachylog解决方案要。它的长度完全相同,但是从相反的方向接近生成物。

{≜ℕ{sℕ₁₀}ᶠṗᵐ&}ᶠ⁵⁹b

似乎无关字符串已被许多角色击败,我对此表示祝贺。

说明:

{≜ℕ                Brute force all nonnegative integers to find any that match the constraints
   {s               Create a predicate that finds all subsequences of digits of said integer
     ℕ₁₀            Constrains those subsequences to be >= 10
        }ᶠ          Finds all possible values of that predicate: all multi-digit subsequences
          ṗᵐ        Apply a primality constraint to all of those subsequences
            &       Make the predicate output the input integer rather than a prime subsequence
             }ᶠ⁵⁹   Find the first 59 results (all of the puzzle's solutions, and zero)
                 b  Remove the first element of the list, i.e. 0

在线尝试!


3

果冻,17 个字节

DẆṖÐfḌÆP€Ạ
³²RÇÐf

我的第一个果冻答案!感谢@Leaky Nun,节省了3个字节!

在线尝试

说明:

DẆṖÐfḌÆP€Ạ      The helper link, which checks if a given number satisfy the conditions.
DẆ              Convert the argument to a list of its digits and get all its substrings.
  ṖÐf           Remove all lists of length 1.
     ḌÆP€Ạ      Convert back each element to an integer and check if all of them are prime.

³²RÇÐf          Main link.
³²              Create a 100 and square it, which gives 10000.
  R             Create a list from 1 to it.
   ÇÐf          Filter out all the elements where the helper link gives false.

恭喜,您的第一个果冻答案!
Leaky Nun

2
RÇÐf可以替换为Ç€TṖÐfḌÆP€可以替换为ḌḟDÆP
丹尼斯

3

Java 8,182字节

v->{for(int n=0;++n<1e4;)if(P(n)>0)System.out.println(n);}int p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}int P(int n){return n>99?p(n)*p(n%100)*p(n%1000)*P(n/10):n<10?n:p(n);}

Gastropner的C端口(gcc)答案,因此请确保对他的答案进行投票!

在线尝试。

说明:

// Loop in range [1,10000), and print any primes corresponding to the challenge description
v->{for(int n=0;++n<1e4;)if(P(n)>0)System.out.println(n);}

// Checks if the given integer is a prime (return unchanged input if prime, 0 if not)
int p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

// Recursive method that checks if every part of length 2+ is a prime, or is below 10
int P(int n){return n>99?p(n)*p(n%100)*p(n%1000)*P(n/10):n<10?n:p(n);}

2

PowerShell v2 +,107104字节

1..10+(11..1e4|?{($x=11..($i=$_)|?{"$i"-match$_}).count-eq($x|?{'1'*$_-match'^(?!(..+)\1+$)..'}).count})

警告:有点慢

从循环111e4(即10000),并使用Where-Object选择器(|?{...})提取数字。该子句有两个组成部分-第一个从11最高编号循环到当前编号,并用于Where-Object提取构成当前编号子字符串的那些编号(通过-matchregex运算符)。我们将这些子字符串存储在中$x。第二部分循环遍历$x并用于使用Where-Object数正则表达式拉出所有素数。然后,我们将.count两者都进行,然后检查实际上是否是-equal。例如,971将具有$x = (71,97,971)与各个的那些是素数,所以3-eq3$TRUE且因此971将被选中。

该结果与range数组连接1..10。结果数组保留在管道上,并且输出是隐式的,默认情况下在元素之间带有换行符。



2

C(GCC) 144个 142 140 136 134 132字节

-2感谢Kevin Cruijssen。-2多亏了ceilingcat

...并且受此启发,我们可以从for循环中再获得2个字节。

凯文·克鲁伊森(Kevin Cruijssen)的答案又是-4,这也毫不掩饰地刻画了更好的首选项。

p(n,i){for(i=2;i<n;)n*=n%i++||n<10;i=n;}P(n){n=p(n)*(n<99||p(n%100)*p(n%1000)*P(n/10));}f(n){for(n=1e4;--n;)P(n)&&printf("%d\n",n);}

在线尝试!


||n<10可以|n<10并且for(n=1;n<1e4;n++)可以是for(n=0;++n<1e4;)-2个字节。
凯文·克鲁伊森

@KevinCruijssen干杯!
gastropner

2

Malbolge Unshackled(20次旋转的变体形式),2,5254e7字节或1,9809e7字节

该答案的大小超过了可发布的最大程序大小(eh),因此代码位于我的GitHub存储库中(注意:请勿使用CTRL + A和CTRL + C复制该代码,只需右键单击并单击“将目标元素另存为。 ..”)。

如何运行呢?

这可能是一个棘手的部分,因为幼稚的Haskell解释器将不时地运行它。TIO有不错的Malbogle Unshackled解释器,但可惜我无法使用它(限制)。

我能找到的最好的是固定的20-trit旋转宽度变体,效果非常好。

为了使口译员更快一些,我从Matthias Lutter的Malbolge Unshackled口译员中删除了所有支票。

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* translation = "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72Fh"
        "OA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";

typedef struct Word {
    unsigned int area;
    unsigned int high;
    unsigned int low;
} Word;

void word2string(Word w, char* s, int min_length) {
    if (!s) return;
    if (min_length < 1) min_length = 1;
    if (min_length > 20) min_length = 20;
    s[0] = (w.area%3) + '0';
    s[1] = 't';
    char tmp[20];
    int i;
    for (i=0;i<10;i++) {
        tmp[19-i] = (w.low % 3) + '0';
        w.low /= 3;
    }
    for (i=0;i<10;i++) {
        tmp[9-i] = (w.high % 3) + '0';
        w.high /= 3;
    }
    i = 0;
    while (tmp[i] == s[0] && i < 20 - min_length) i++;
    int j = 2;
    while (i < 20) {
        s[j] = tmp[i];
        i++;
        j++;
    }
    s[j] = 0;
}

unsigned int crazy_low(unsigned int a, unsigned int d){
    unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
    int position = 0;
    unsigned int output = 0;
    while (position < 10){
        unsigned int i = a%3;
        unsigned int j = d%3;
        unsigned int out = crz[i+3*j];
        unsigned int multiple = 1;
        int k;
        for (k=0;k<position;k++)
            multiple *= 3;
        output += multiple*out;
        a /= 3;
        d /= 3;
        position++;
    }
    return output;
}

Word zero() {
    Word result = {0, 0, 0};
    return result;
}

Word increment(Word d) {
    d.low++;
    if (d.low >= 59049) {
        d.low = 0;
        d.high++;
        if (d.high >= 59049) {
            fprintf(stderr,"error: overflow\n");
            exit(1);
        }
    }
    return d;
}

Word decrement(Word d) {
    if (d.low == 0) {
        d.low = 59048;
        d.high--;
    }else{
        d.low--;
    }
    return d;
}

Word crazy(Word a, Word d){
    Word output;
    unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
    output.area = crz[a.area+3*d.area];
    output.high = crazy_low(a.high, d.high);
    output.low = crazy_low(a.low, d.low);
    return output;
}

Word rotate_r(Word d){
    unsigned int carry_h = d.high%3;
    unsigned int carry_l = d.low%3;
    d.high = 19683 * carry_l + d.high / 3;
    d.low = 19683 * carry_h + d.low / 3;
    return d;
}

// last_initialized: if set, use to fill newly generated memory with preinitial values...
Word* ptr_to(Word** mem[], Word d, unsigned int last_initialized) {
    if ((mem[d.area])[d.high]) {
        return &(((mem[d.area])[d.high])[d.low]);
    }
    (mem[d.area])[d.high] = (Word*)malloc(59049 * sizeof(Word));
    if (!(mem[d.area])[d.high]) {
        fprintf(stderr,"error: out of memory.\n");
        exit(1);
    }
    if (last_initialized) {
        Word repitition[6];
        repitition[(last_initialized-1) % 6] =
                ((mem[0])[(last_initialized-1) / 59049])
                    [(last_initialized-1) % 59049];
        repitition[(last_initialized) % 6] =
                ((mem[0])[last_initialized / 59049])
                    [last_initialized % 59049];
        unsigned int i;
        for (i=0;i<6;i++) {
            repitition[(last_initialized+1+i) % 6] =
                    crazy(repitition[(last_initialized+i) % 6],
                        repitition[(last_initialized-1+i) % 6]);
        }
        unsigned int offset = (59049*d.high) % 6;
        i = 0;
        while (1){
            ((mem[d.area])[d.high])[i] = repitition[(i+offset)%6];
            if (i == 59048) {
                break;
            }
            i++;
        }
    }
    return &(((mem[d.area])[d.high])[d.low]);
}

unsigned int get_instruction(Word** mem[], Word c,
        unsigned int last_initialized,
        int ignore_invalid) {
    Word* instr = ptr_to(mem, c, last_initialized);
    unsigned int instruction = instr->low;
    instruction = (instruction+c.low + 59049 * c.high
            + (c.area==1?52:(c.area==2?10:0)))%94;
    return instruction;
}

int main(int argc, char* argv[]) {
    Word** memory[3];
    int i,j;
    for (i=0; i<3; i++) {
        memory[i] = (Word**)malloc(59049 * sizeof(Word*));
        if (!memory) {
            fprintf(stderr,"not enough memory.\n");
            return 1;
        }
        for (j=0; j<59049; j++) {
            (memory[i])[j] = 0;
        }
    }
    Word a, c, d;
    unsigned int result;
    FILE* file;
    if (argc < 2) {
        // read program code from STDIN
        file = stdin;
    }else{
        file = fopen(argv[1],"rb");
    }
    if (file == NULL) {
        fprintf(stderr, "File not found: %s\n",argv[1]);
        return 1;
    }
    a = zero();
    c = zero();
    d = zero();
    result = 0;
    while (!feof(file)){
        unsigned int instr;
        Word* cell = ptr_to(memory, d, 0);
        (*cell) = zero();
        result = fread(&cell->low,1,1,file);
        if (result > 1)
            return 1;
        if (result == 0 || cell->low == 0x1a || cell->low == 0x04)
            break;
        instr = (cell->low + d.low + 59049*d.high)%94;
        if (cell->low == ' ' || cell->low == '\t' || cell->low == '\r'
                || cell->low == '\n');
        else if (cell->low >= 33 && cell->low < 127 &&
                (instr == 4 || instr == 5 || instr == 23 || instr == 39
                    || instr == 40 || instr == 62 || instr == 68
                    || instr == 81)) {
            d = increment(d);
        }
    }
    if (file != stdin) {
        fclose(file);
    }
    unsigned int last_initialized = 0;
    while (1){
        *ptr_to(memory, d, 0) = crazy(*ptr_to(memory, decrement(d), 0),
                *ptr_to(memory, decrement(decrement(d)), 0));
        last_initialized = d.low + 59049*d.high;
        if (d.low == 59048) {
            break;
        }
        d = increment(d);
    }
    d = zero();

    unsigned int step = 0;
    while (1) {
        unsigned int instruction = get_instruction(memory, c,
                last_initialized, 0);
        step++;
        switch (instruction){
            case 4:
                c = *ptr_to(memory,d,last_initialized);
                break;
            case 5:
                if (!a.area) {
                    printf("%c",(char)(a.low + 59049*a.high));
                }else if (a.area == 2 && a.low == 59047
                        && a.high == 59048) {
                    printf("\n");
                }
                break;
            case 23:
                a = zero();
                a.low = getchar();
                if (a.low == EOF) {
                    a.low = 59048;
                    a.high = 59048;
                    a.area = 2;
                }else if (a.low == '\n'){
                    a.low = 59047;
                    a.high = 59048;
                    a.area = 2;
                }
                break;
            case 39:
                a = (*ptr_to(memory,d,last_initialized)
                        = rotate_r(*ptr_to(memory,d,last_initialized)));
                break;
            case 40:
                d = *ptr_to(memory,d,last_initialized);
                break;
            case 62:
                a = (*ptr_to(memory,d,last_initialized)
                        = crazy(a, *ptr_to(memory,d,last_initialized)));
                break;
            case 81:
                return 0;
            case 68:
            default:
                break;
        }

        Word* mem_c = ptr_to(memory, c, last_initialized);
        mem_c->low = translation[mem_c->low - 33];

        c = increment(c);
        d = increment(d);
    }
    return 0;
}

性能说明

该应用程序在我的计算机上运行了大约40分钟,产生了序列的十六进制数字。我在大约一个小时的计算后将其停止,并以0x11完成。

请注意,这个答案与我的另一个答案有所不同,因为这个答案实际上是计算数字,并且可以将其无限期地计算出来。

应用程序分配了大约7 GB的旋转缓冲区,因此请更好地准备可用的RAM。

替代变体

替代变体使用的内存减少了大约2 GB,但是以ASCII字符(0 = ASCII(0x0),10 =换行等)的形式产生输出,可在此处获得。由于挑战要求,它没有竞争


高尔夫代码就是要给出简短的答案。
Alfe

2
@Alfe Malbolge是一种设计成很难编程的语言(维基百科链接);甚至有可能做到这一点的事实令人印象深刻。
朱塞佩

4
因此,实际上,这一个简短的答案。只是标准发生了变化。略。
Alfe

3
@Alfe,欢迎您尝试剃光一些字节!;-)
朱塞佩

2

Python 3,118字节

r=range(9720)
for n in r[1:]:all(all(l%k+9//l for k in r[2:l])for l in(n%10**(i%5)//10**(i//5)for i in r))and print(n)

在线尝试!

说明

警告:此解决方案中没有实际的字符串。

r=range(9720)
for n in r[1:]:                                        # For each positive integer up to 9720
 all( ... for l in(n%10**(i%5)//10**(i//5)for i in r)) # Check for all its substrings
  all(l%k ... for k in r[2:l])                         # If it is either prime
   +9//l                                               # Or smaller than 10
and print(n)                                           # Then print

1

Ruby,81 + 8 = 89字节

的+8个字节-rprime

puts (?1..?9*4).select{|m|(r=2..m.size).all?{|i|r.all?{|j|m[i-2,j].to_i.prime?}}}

在repl.it上查看:https ://repl.it/CniR/2


1

Perl 6的 47 44  43个字节

for 1..9719 {all(m:ex/..+/).Int.is-prime&&.say}
put grep {is-prime +all(m:ex/..+/):},1..9719
put grep {is-prime +all m:ex/..+/:},1..9719

说明:

# print the values space separated, with trailing newline
put

# that match
grep -> $_ {

  # call the method 「.is-prime」 ( that is what 「:」 is for )
  # (autothreaded)
  is-prime

  # convert following to numeric (autothreaded)
  +
  # a junction of
  all(
    # all substrings 2 characters or greater
    $_ ~~ m :exhaustive / . .+ /
  )

  # needed to indicate that 「is-prime」 is a method call
  :

},

# in this Range
1..9719

1

C#,261个 249 247字节

感谢Leaky Nun节省了12个字节

()=>{Action<int>w=System.Console.WriteLine;int i=0,n,j,k,p,m,b;for(;++i<10001;){n=(i+"").Length;if(n<2)w(i);else{b=1;for(j=1;++j<=n;)for(k=0;k+j<=n;){p=int.Parse((i+"").Substring(k++,j));if(p%2<1)b=0;for(m=3;m<p;m+=2)if(p%m<1)b=0;}if(b>0)w(i);}}};

这将编译为Func<List<int>>

格式化的版本如下所示:

() =>
{
    Action<int> w = System.Console.WriteLine;

    int i = 0, n, j, k, p, m, b;

    for (; ++i < 10001;)
    {
        n = (i + "").Length;

        if (n < 2)
            w(i);

        else
        {
            b = 1;
            for (j = 1; ++j <= n; )
                for (k = 0; k + j <= n; )
                {
                    p = int.Parse((i + "").Substring(k++, j));

                    if (p % 2 < 1)
                        b = 0;

                    for (m = 3; m < p; m += 2)
                        if (p % m < 1)
                            b = 0;
                }

            if (b > 0)
                w(i);
        }
    }
};

无需使用列表即可直接打印
Leaky Nun

代替falsetrue,使用0>10<1
Leaky Nun

您可以参考以获取其他高尔夫技巧。
Leaky Nun

@LeakyNun感谢您的提示,我通常喜欢发布一种高尔夫版本,然后从那里转移。
TheLethalCoder

1

Swift 4,144字节

let p={n in !(2..<n).contains{n%$0<1}}
print((1...971).filter{$0<10||p($0)&&($0<100||p($0/10)&&p($0%100))}+[1373,3137,3797,6131,6173,6197,9719])

在线尝试!

说明

let p={n in !(2..<n).contains{n%$0<1}} // Helper function p, tests if a number is prime
print((1...971).filter{                // Print every number n in the range 1 to 971
 $0<10                                 //  that is less than 10
 ||p($0)&&                             //  or a prime and
 ($0<100                               //   is less than 100 or
  ||p($0/10)&&p($0%100))}              //   n/10 and n%100 are primes
+[1373,3137,3797,6131,6173,6197,9719]) // Print the four digit numbers

1

JavaScript(Node.js),130字节

如果我可以假设无限堆栈i*i<=n&&可被移除并i*i>n轮流i>=n由9个字节这降低了代码和可能的主要功能转换为递归:https://tio.run/##LYpBDoIwEEX33AMyAxVbXUmccgX2xkWDRYeQaSPqyrvXkrj5ef/lze7j1vHJ8bWTcPMpTQRMWjm6XJFs0/DZ@EM/ASunBmCsKtfG9/rIiJ0rIoEoJpNbKXPdx@1jx5akGEiytqdNYp2nNFr / wR @ xHkD2Rn81dpLGIGtYfLuEO0yAmH4(119字节)

_=>eval(`for(a=[i=1];++i<1e4;)P(i)&&a.push(i)`)||a
p=(n,i=1)=>i*i<=n&&n%++i?p(n,i):n%i
P=n=>n>9?p(n)*p(n%100)*p(n%1e3)*P(n/10|0):n

在线尝试!


1

Malbolge,1361字节

简单而无聊的版本。从最高的数字开始显示。

D'`r^"!=[YG3yUCvA-csNqp-nJ$HYFgDC#AbQ,|*)\rwvutm3kSonmlkdihg`&dc\aZ_X|V[ZYXQPt7SRQPOHGkKJIHG@d>=<A:98\}|:981U5.-2+0/.'K%$#G!E}e#z!~}v<]yxwpun4rkj0nmfN+ihaf_^$\a`_XW{>=YXWVONrLKPINGkE-IBAe(>=<;_?>=}|:3W1w543,P0).-&J*)(!E}|B"!~}|{zyr8potml2jongfkjibg`&d]\"`_XW{>=YXWVONr54JIHMFj-,HGF?>b%A@?87[;:981w543,P0).-&J*j(!EfeB"!~}_u;yrqpun4rqpihmlkjihg`&d]\"`_X|\[ZYXQuUNMLQJnH0LKJIBAe(>=<`#"8\<5Y9270T43,Pqp.-&J$)"!~D|#"y~}|u;s9qvotsrk1inglkdihg`&d]\"Z~XWVUZYXQu87SLKo2NGFjDIHGF?>bBA#"8\6;:981Uv.32+*)Mnm%$)('~D|{A!xwv{zyr8vXnsrkjoh.fNdchg`ed]#aC_^WVz=YXQPt7SRQPOHGkK-IHGF?>bBA#"8\6;:981Uv.32+*)Mnm%*#"F&%$#cy?}v<]\xwpun4rqSonmf,diha'eG]#a`_X|V[ZYXWPt76LKoIHGLEiCHGFED=aA:?>7[;:981w/4-,PO)o'&J*j(!E%edz@~}_u;yxqpo5mrqpoh.f,jibgf_%]\[!_XW{[ZYXQu87SLKo2NGFjJIHAF?c=BA@?>=<5Y38765.-Q10)o'&J*j(!E%e{z@~}|{ts9qpotsrk1oQglkd*)gIed]#DZ_^]VzZYRQuONMRKJnNGLEJCgG)(D=aA:?>=<;4X816/43,P0).-&+$H('gf|Bcb~w|u;yxwYutmrqj0nmleMib(fH%cba`_X|VUZYXWPt7SRQPOHGkEDIHG@dDC<;@?8\6|:32V0T43,+O)o'&J*)('&}C{"yxwv<tyr8vun4Ukpoh.fN+c)gIed]#DZ_^]VzTSRWPtTSLQJnH0LKJIBAe(>=BA@987[;:381Uv.32+*)Mnm%$)('~D${"y?}_uzyxqpo5srqSonmf,jihgfeG]#a`_X|V[ZYXWPt76LKo2NGFjJIH*)ED=a;@?>76;4X816/43,P*).',%I)('~Ded"y~}|u;srqvo5mlqpih.fN+cba`&d]\aZ~^]VUZSwWPUTSLpJ2NGLEiCHGFED=a;:?>7<5YX876v43,+O).-,+$H('&feBz!x}v{zsr8punsrk1inglkdihg`&d]\"Z~X]V[ZSwQVUTMRKo2NGFjDIHGF?>b%A@?87[;{921U5.3210)M-,%k#(!EfeB"y~}v{zyr8%

在线尝试!


0

TI-83 / 84 BASIC,124字节

For(A,1,E4
DelVar Nint(log(A→P
Ans→Q
While Ans
For(I,0,Q-Ans
10^(P+1
AnsfPart(iPart(A/10^I)/Ans→S
min(Ans={2,3,5
If S≥7 and fPart(.5S
min(remainder(S,3+2cumSum(not(binompdf(int(.5√(S)),0
N+not(Ans→N
End
P-1→P
End
If not(N
Disp A
End

循环访问前10k个整数。在N中设置一个计数器以检查每个子串素数,然后int(log(A检索比当前数字中的位数少一个的数字。然后我们将该数字留在第二个变量中,这样我们就可以将P逐步减小每个长度至少2位数字的子串。10 ^ ...和AnsfPart(iPart(,,)生成当前子串以检查素数,然后以下3行在Ans中将素数检查为1或0。如果子串不是素数,我们将N递增,然后在检查所有子串后,如果N仍为0,我们将输出当前数字。

可能可以做一些调整以提高针对该测试的素数检查的效率?我很高兴能找到比以TI-83格式直接存储输出更少的字节数的算法!


0

Python 3.8(预发布),194字节

r=range
s=str
l=lambda _:len(s(_))
[*map(print,[t for t in r(1,10**4) if all(all(int(x)%b for b in r(2,int(x))) for x in [s(t)[i:j+1] for i in r(l(t)) for j in r(i,l(t)) if l(s(t)[i:j+1])>1])])]

在线尝试!


0

PHP,135字节

for(;++$n<1e4;$p||print"$n
")for($p=$i=0;$i<$l=strlen($n);$i++)for($j=1;$j++<$l-$i;$p|=$k)for($k=($m=substr($n,$i,$j))-1;$k&&$m%$k--;);

在线尝试!

for(;                         // level 1 loop on
  ++$n<1e4;                   // all numbers from 1 to 10,000, $n is current number
  $p||print"$n\n"             // at the end of loop for each number, print $n if all multi digit sub strings were prime ($p=0)
)
  for(                        // level 2 loop on each digit of $n
    $p=                       // $p is a flag for all sub string primes and is set to 0 for each new $n
      $i=0;                   // $i is position of current digit (and sub string start position)
    $i<$l=strlen($n);         // $l is the total digits count in $n
    $i++                      // increment $i by one
  )
    for(                      // level 3 loop to create sub strings
      $j=1;                   // $j is length of sub string, we only care about multi digit sub strings so it starts from 1
      $j++<$l-$i;             // continue the loop as long as $j has not reached last digit and increment it by one
      $p|=$k                  // THIS IS RUN AFTER LOOP LEVEL 4: update $p flag based on value of $k
                              //     $p will be left at 0 only if all of the sub strings are prime (if $k is always 0)
    )
      for(                    // level 4 loop to check each sub string to be prime
        $k=(                  // $k is set to current sub string minus 1
          $m=substr($n,$i,$j) // $m is current sub string
        )-1;                  // 
        $k && $m%$k--;        // as long as $k is more than 0 and $m%$k is not zero, decrement $k by one and continue
                              //     a prime number will only get a 0 remainder, when $k gets to 1
                              //     so $k will be 0 for primes and more than 0 for non-primes
      );
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.