找到酋长国!


20

一个反质数是一个非回文素这,逆转的时候,也是素数。

可以在OEIS上找到基于10个emirps的列表。前六个是:

13, 17, 31, 37, 71, 73

但是,由于有逆转规则,每个基地的延期都不相同。例如,前六个二进制事件是:

Bin  | 1011, 1101, 10111, 11101, 101001, 100101
Dec  | (11 , 13  , 23   , 29   , 37    , 41   ) 

...以十六进制表示,它们是:

Hex |  17, 1F, 35, 3B, 3D, 53
Dec | (23, 31, 53, 59, 61, 83)

有趣的事实:一元没有小事,因为每个数字都是回文。


挑战

您的任务是创建一个接受两个参数和的函数(或完整程序) ,并在基生成前 emirps 的列表。nbnb

规则/细节:

  • n和都是大于正整数。b0
  • 您可以假设:也就是说,基数将在二进制和十六进制之间。2b16
  • 您应该能够计算值,最高可达。n 100
  • 生成的列表可以以或您语言的标准整数为基础,只要您在答案中指定即可。b
  • 不允许内置的emirp检查(内置的素性测试很好)
  • 您不能对emirps进行硬编码,也不能从任何外部文件中读取。
  • 一如既往,禁止出现标准漏洞。
  • 这是,因此最短的答案(以字节为单位)获胜。

测试用例

对于每个测试用例,我都将该列表包括在基数中,b并以10为基数等效。

B = 2, N = 10

BIN: [1011, 1101, 10111, 11101, 100101, 101001, 101011, 101111, 110101, 111101]
DEC: [11, 13, 23, 29, 37, 41, 43, 47, 53, 61] 


B = 3, N = 5

BASE3: [12, 21, 102, 201, 1011]
DEC:   [5, 7, 11, 19, 31]


B = 12, N = 7

BASE12: [15, 51, 57, 5B, 75, B5, 107]
DEC: [17, 61, 67, 71, 89, 137, 151]


B = 16, N = 4

HEX: [17, 1F, 35, 3B]
DEC: [23, 31, 53, 59] 

您可以进一步测试程序对我的(ungolfed)Python的例子repl.it

Answers:


6

果冻,16 字节

bµU,ḅ⁹QÆPḄ=3
⁸ç#

TryItOnline!

怎么样?

bµU,ḅ⁹QÆPḄ=3 - Link 1, in-sequence test: n, b
b            - convert n to base b - a list
 µ           - monadic chain separation
  U          - reverse the list
   ,         - pair with the list
     ⁹       - link's right argument, b
    ḅ        - convert each of the two lists from base b
      Q      - get unique values (if palindromic a list of only one item)
       ÆP    - test if prime(s) - 1 if prime, 0 if not
         Ḅ   - convert to binary
          =3 - equal to 3? (i.e. [reverse is prime, forward is prime]=[1,1])

⁸ç# - Main link: b, N
  # - count up from b *see note, and find the first N matches (n=b, n=b+1, ...) for:
 ç  - last link (1) as a dyad with left argument n and right argument
⁸   - left argument, b

* b在base中的注释b[1,0],当反转时[0,1]1,它不是素数;小于b一位数的位数b,因此回文。


恭喜您获胜!
FlipTack

8

05AB1E,17个字节

使用CP-1252编码。

输入顺序为n, b
输出以10为底。

µN²BÂD²öpŠÊNpPD–½

在线尝试!

说明

                    # implicit input a,b
µ                   # loop until counter is a
 N²B                # convert current iteration number to base b
    ÂD              # create 2 reversed copies
      ²ö            # convert one reversed copy to base 10
        p           # check for primality
         ŠÊ         # compare the normal and reversed number in base b for inequality
           Np       # check current iteration number for primality
             P      # product of all
              D     # duplicate
               –    # if 1, print current iteration number
                ½   # if 1, increase counter

4

Mathematica,70个字节

Cases[Prime@Range@437,p_/;(r=p~IntegerReverse~#2)!=p&&PrimeQ@r]~Take~#&

适用于0 <= n <= 1002 <= b <= 16。从列表中Prime@Range@437的第一个437素数,找到Cases p其中IntegerReverse rp碱基#2不等于p,也是素数,则采取先#这样p

这是一个95字节的解决方案,适用于任意n>=0b>=2

(For[i=1;a={},Length@a<#,If[(r=IntegerReverse[p=Prime@i,#2])!=p&&PrimeQ@r,a~AppendTo~p],i++];a)&

+1 IntegerReverse。当然!真好
DavidC

79个字节用于任意nb解决方案;如果在页脚中允许ing,则为77个字节ReapFor[i=j=0,j<#,If[(r=IntegerReverse[p=Prime@++i,#2])!=p&&PrimeQ@r,j++;Sow@p]]&
Roman

3

Perl,262个字节

($b,$n)=@ARGV;$,=',';sub c{my$z;for($_=pop;$_;$z=(0..9,a..z)[$_%$b].$z,$_=($_-$_%$b)/$b){};$z}sub d{my$z;for(;c(++$z)ne@_[0];){}$z}for($p=2;@a<$n;$p++){$r=qr/^1?$|^(11+?)\1+$/;(c($p)eq reverse c$p)||((1x$p)=~$r)||(1x d($x=reverse c($p)))=~$r?1:push@a,c($p);}say@a

可读性:

($b,$n)=@ARGV;
$,=',';
sub c{
    my$z;
    for($_=pop;$_;$z=(0..9,a..z)[$_%$b].$z,$_=($_-$_%$b)/$b){};
    $z
}
sub d{
    my$z;
    for(;c(++$z)ne@_[0];){}
    $z
}
for($p=2;@a<$n;$p++){
    $r=qr/^1?$|^(11+?)\1+$/;
    (c($p)eq reverse c$p)||((1x$p)=~$r)||(1x d($x=reverse c($p)))=~$r?1:push@a,c($p)
}
say@a

c将给定的数字转换为基数$b,并通过找到传递给的返回d基数$b的第一个数字,将给定的数字从基数转换回十进制。然后,for循环使用复合正则表达式检查它是否是回文式,以及两个数字是否都是素数。$bc


3

Mathematica 112字节

Cases[Table[Prime@n~IntegerDigits~#2,{n,500}],x_/;x!=(z=Reverse@x)&&PrimeQ[z~(f=FromDigits)~#2]:>x~f~#2]~Take~#&

查找十六进制的前10个Emips;以十进制返回它们。

Cases[Table[Prime@n~IntegerDigits~#2, {n, 500}], 
x_ /; x != (z = Reverse@x) && PrimeQ[z~(f = FromDigits)~#2] :> x~f~#2]~Take~# &[10, 16]


{23, 31, 53, 59, 61, 83, 89, 113, 149, 179}

不打高尔夫球

Take[Cases[                                             (* take #1 cases; #1 is the first input argument *)
   Table[IntegerDigits[Prime[n], #2], {n, 500}],        (* from a list of the first 500 primes, each displayed as a list of digits in base #2 [second argument] *) 
   x_ /;                                                (* x, a list of digits, such that *)
   x != (z = Reverse[x]) && PrimeQ[FromDigits[z, #2]]   (* the reverse of the digits is not the same as the list of digits; and the reverse list, when composed, also constitutes a prime *)
   :> FromDigits[x, #2]],                               (* and return the prime *)
   #1] &                                                (* [this is where #1 goes, stating how many cases to Take] *)



2

C,293个 286 261字节

@ceilingcat改进,261个字节:

v,t,i,j,c,g,s[9],r[9],b;main(n,a)int**a;{for(b=n=atoi(a[1]);g^atoi(a[2]);t|v|!wcscmp(s,r)||printf("%u ",n,++g)){i=j=0;for(c=++n;s[i]=c;c/=b)s[i++]=c%b+1;for(;r[j]=i;)r[j++]=s[--i];p(n);for(t=v;r[i];)c+=~-r[i]*pow(b,i++);p(c);}}p(n){for(j=1,v=0;++j<n;n%j||v++);}

在线尝试!

(此人就像不断关注我在PPCG上的内容,并在评论中改进我的内容,当我对他表示感谢时,他只是删除评论并消失了,哈哈。谢谢,再次感谢!)


@movatica改进,286个字节:

u,v,t,i,j,c,n,g;main(int x,char**a){char s[9],r[9],b=n=atoi(a[1]);x=atoi(a[2]);for(;g^x;){i=j=0;for(c=++n;c;c/=b)s[i++]=c%b+1;s[i]=c=0;for(;i;r[j++]=s[--i]);r[j]=0;p(n);t=v;for(;r[i];)c+=(r[i]-1)*pow(b,i++);p(c);t|v|!strcmp(s,r)?:printf("%u ",n,++g);}}p(n){for(u=1,v=0;++u<n;n%u?:v++);}

在线尝试!


我的原始答案是293个字节:

u,v,t,i,j,c,n,g;main(int x,char**a){char s[9],r[9],b=n=atoi(a[1]);x=atoi(a[2]);for(++n;g^x;++n){i=j=0;for(c=n;c;c/=b)s[i++]=c%b+1;s[i]=c=0;for(;i;r[j++]=s[--i]);r[j]=0;p(n);t=v;for(--i;r[++i];)c+=(r[i]-1)*pow(b,i);p(c);t|v|!strcmp(s,r)?:printf("%u ",n,++g);}}p(n){for(u=1,v=0;++u<n;n%u?:v++);}

用编译gcc emirp.c -o emirp -lm并运行./emirp <b> <n>。在以10为基数的位置打印以空格分隔的emirps。


@FlipTack你是对的。我明天必须修理。
超频Sanic

@FlipTack已修复并经过测试,以确保其通过您的测试。这个好吗?
超频Sanic

当然可以!欢迎您编码高尔夫。
FlipTack

1
干得好!我移动了一些增量运算符使您降至286
movatica

1
@movatica太棒了!我在您的答案中添加了您的改进。谢谢!
超频Sanic

1

的JavaScript(ES6),149 148 141 140个字节

返回基数为b的emirps的空格列表。(可以通过返回一个十进制列表来缩短2个字节。)

f=(b,n,i=2)=>n?((p=(n,k=n)=>--k<2?k:n%k&&p(n,k))(i)&p(k=parseInt([...j=i.toString(b)].reverse().join``,b))&&k-i&&n--?j+' ':'')+f(b,n,i+1):''

测试用例


1

Python 2,133字节

p=lambda n:all(n%i for i in range(2,n))
b,n=input()
i=b
while n:
 j=i=i+1;r=0
 while j:r=r*b+j%b;j/=b
 if(i-r)*p(i)*p(r):print i;n-=1

在线尝试!

在以10为底的新行上输出每个数字


0

APL(NARS),87个字符,174个字节

r←a f w;i
i←1⋄r←⍬
→2×⍳∼{∼0π⍵:0⋄k≡v←⌽k←{(a⍴⍨⌊1+a⍟⍵)⊤⍵}⍵:0⋄0πa⊥v:1⋄0}i+←1⋄r←r,i⋄→2×⍳w>≢r

结果将以10为底。测试和结果:

  3 f 1
5 
  2 f 10
11 13 23 29 37 41 43 47 53 61 
  3 f 5
5 7 11 19 31 
  12 f 7
17 61 67 71 89 137 151 
  16 f 4
23 31 53 59 

{(⍺⍴⍨⌊1+⍺⍟⍵)⊤⍵}在base ,array整数结果中进行转换;0π⍵如果为质数则返回true [1],否则返回0。

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.