做一些Prime Square!


17

什么是总理广场?

素数平方是所有四个边都是不同素数的平方。
但是哪个呢?
我们如何构造它们?

这是4x4 Prime Square的示例

1009  
0  0     
3  0   
1021    

首先,我们从左上角开始。我们正在顺时针工作。
我们选择4数字为1009的最小素数。

然后,我们需要具有4数字的最小素数,该素数以开头9。这是9001

第三个(4位数字)素数必须1以最后一位数字(因为9001以结尾1
,并且必须是具有此属性的最小的4位素数,此属性以前没有用作edge
这个素数是1021

第四个质数必须具备4数字,开始1(因为1009点开始有1),并结束1(因为1021点开始有1
与此属性的最小的4位质数尚未为边缘之前使用的1031

你的任务

您将获得一个整数n3 to 100
该整数将是该n x n正方形的尺寸。
然后,您必须按照以下测试用例的形式准确输出该正方形

测试用例

n=3  
Output    

101
3 0
113     

n=5    
Output     

10007
0   0
0   0    
9   0    
10061     

n=7     
Output    

1000003    
0     0     
0     0     
0     0     
0     0     
8     1     
1000037      

n=10      
Output     

1000000007      
0        0      
0        0     
0        0      
0        0       
0        0       
0        0      
1        0      
8        0       
1000000021      

n=20       
Output     

10000000000000000051     
0                  0          
0                  0           
0                  0           
0                  0          
0                  0           
0                  0          
0                  0           
0                  0           
0                  0          
0                  0          
0                  0          
0                  0           
0                  0           
0                  0          
0                  0            
0                  0          
0                  0              
9                  8      
10000000000000000097
  • 输入和输出可以通过任何方便的方法给出。
  • 您可以将其打印到STDOUT或将其作为功能结果返回。
  • 完整的程序或功能都是可以接受的。
  • 只要数字适当排列,任何数量的外部空格都是可以接受的
  • 禁止出现标准漏洞
  • 这是因此所有常见的高​​尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

编辑
这对所有人来说n
都是可能的n=100

1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000289        
9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000091            
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000711             
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002191     



对于那些您认为不可能实现的人,这里是所有测试用例


如果n可以增加到100,则最好有一些大于n = 10的
测试用例。– gastropner

4
是否有可能对所有n:P 都可行?挑战不是问题,只是好奇。
魔术章鱼缸

2
@MagicOctopusUrn绝对不可能对所有对象都是不可能的n:对于n= 1,我们不能满足四个边缘是素数的约束,而对于n= 2,我们被迫选择11,13,23,此时最终边缘是12是复合的。我没有证据表明所有n> 2 都是可能的,但是如果不这样做,我会感到震惊:非正式地,数字越多,满足约束的“摆动空间”越多。
丹尼尔·瓦格纳

pķ+1个pķķ463ñ4

2
@MagicOctopusUrn 算术级数素数定理表明,以 1、3、7 和9结尾的素数的密度有相当强的意义(用n = 10,a = 1/3/7/9 表示);对于足够大n的长度,至少有两个素数,其长度n以1开始,并以每个数字结尾(因此我们可以选择底边),并且至少有三个素数以1开始并以1结尾(因此我们可以选择a左边缘)。
丹尼尔·瓦格纳

Answers:


4

05AB1E64 63 56 53 48 46 字节

°ÅPIùćÐ4FˆθUKD.ΔθyXÅ?yXÅ¿)¯gè}ÐNĀiR}¦}I¯JŽ9¦SΛ

-1感谢字节@ Mr.Xcoder
-5字节感谢@Grimy

ñ>4ñ>7

说明:

°                 # Raise the (implicit) input to the power 10
 ÅP               # Get a list of primes within the range [2, n^10]
   Iù             # Only keep those of a length equal to the input
ć                 # Extract the head; push the remainder-list and first prime separately
 Ð                # Triplicate this first prime
4F                # Loop 4 times:
  ˆ               #  Add the (modified) prime at the top of the stack to the global array
  θU              #  Pop and store the last digit of the prime in variable `X`
  K               #  Remove this prime from the prime-list
  D               #  Duplicate the prime-list
                #  Find the first prime `y` in the prime list which is truthy for:
     θ            #   Get the last digit of prime `y`
     yXÅ?         #   Check if prime `y` starts with variable `X`
     yXÅ¿         #   Check if prime `y` ends with variable `X`
     )            #   Wrap the three results above into a list
      ¯g          #   Get the amount of items in the global array
        è         #   And use it to index into these three checks
                  #   (Note that only 1 is truthy for 05AB1E, so the `θ` basically checks
                  #    if the last digit of prime `y` is 1)
                #  Triplicate the found prime
      NĀi }       #  If the loop index is 1, 2, or 3:
         R        #   Reverse the found prime
      ¦           #  And then remove the first digit of the (potentially reversed) prime
}                 # After the loop:
 I                # Push the input as length
 ¯J               # Push the global array joined together to a single string
 Ž9¦S             # Push compressed integer 2460 converted to a list of digits: [2,4,6,0]
 Λ                # Draw the joined string in the directions [2,4,6,0] (aka [→,↓,←,↑])
                  # of a length equal to the input
                  # (which is output immediately and implicitly as result)

看到这个05AB1E尖矿(部分如何压缩大整数?理解为什么Ž9¦2460。并查看我的05AB1E技巧,以了解如何使用Λ内置的Canvas 输出正方形。

NĀiR}¦I¯JŽ9¦SΛn=4[1009,9001,1021,1031][1009,"001","201","301"]Λ
一种I
b¯J"1009001201301"n=4
CŽ9¦S[2,4,6,0][→,↓,←,↑]


1
50:4F°ÅP¯KIù.Δ1sЮθÅ¿Š®θÅ?Šθ)¯gè}©ˆ}ð¯2ô`€R«€¦J«Ž9¦SΛ 49:°ÅPIùć4FÐN1›iR}¦ˆθUKD.ΔÐθsXÅ?‚sXÅ¿ª¯gè]Ið¯J«Ž9¦SΛ 48:°ÅPIùćÐ4FˆθUKD.ΔÐθsXÅ?‚sXÅ¿ª¯gè}ÐNĀiR}¦}I¯JŽ9¦SΛ
肮脏的

@肮脏的谢谢!非常好的高尔夫。通过更改ÐθsXÅ?‚sXÅ¿ª为,我可以根据您的48字节版本再保存2个字节θyXÅ?yXÅ¿))但是,我不确定是否可以在循环范围内工作,因为我希望它在第一次迭代中也将质数表也包装到其表中。但是即使没有,使用yy代替代替Ðss仍然会节省1个字节。:)
凯文·克鲁伊森

4

05AB1E35 33 32 31字节

-1字节感谢Kevin Cruijssen

°ÅPIùΔÐXθÅ?Ïн©KX®¦«UNií]IXŽ9¦SΛ

在线尝试!

说明:

°                 # 10 to the power of the input
 ÅP               # list of primes up to that
   Iù             # keep only those with the same length as the input

Δ                 # repeat until the list doesn't change
# This ends up doing a ton of unneeded work. 4F (to loop 4 times) would be
# enough, but Δ is shorter and the extra iterations don’t cause issues.
# At the start of each iteration, the stack only contains the list of primes,
# and the variable X contains the current list of digits we’ll want to print.
# Conveniently, X defaults to 1, which is our first digit.

 Ð    Ï           # push a filtered copy of the list, keeping only…
    Å?            # numbers that start with…
  Xθ              # the last character of X
       н          # get the first element: this is our next prime

 ©                # save this number to the register
  K               # remove it from the list of candidate primes
   X              # push X
    ®             # restore the number from the register
     ¦            # remove its first character
      «           # concatenate it to X
       U          # save the result to X

 Ni               # if N == 1 (second time through the loop)
   í              # reverse all elements in the list of candidate primes
    ]             # closes both this if and the main loop

      Λ           # Draw on a canvas…
I                 # using the input as length…
 X                # using X as the string to draw…
  Ž9¦S            # using [2,4,6,0] (aka [→,↓,←,↑]) as the directions to draw in

这部分地基于Kevin的答案,但在这一点上已经足够不同,以至于我认为它应该得到自己的答案,而不是发表评论。
Grimmy

1
我现在才看到这个答案。非常好!除了一般方法(因此是第一部分和最后一部分)之外,确定四个素数和构建字符串的方法也不同,因此我可以理解分开的答案。向我+1。顺便说一句,您可以保存一个字节,删除Θat 105AB1E中只有真理,所以if Nif N == 1相同。
凯文·克鲁伊森

1
@KevinCruijssen谢谢!我当然知道,但是我忘了使用它。回想起来,Θi05AB1E等于if (cond == true)...
Grimmy

是的,是的。:) Θ如果你想转换的一切,除了可以仍然是有用10。但是对于if语句i,就像使用的伪代码一样,它并不是真正必需的== true
凯文·克鲁伊森

2

JavaScript(ES8), 205 ...  185177173字节

ñ>8

n=>([a,b,c]=[0,-1,--n,0].map(p=o=i=>o[(g=n=>{for(k=n++;n%k--;);k|o[n]|p[i]-n%10?g(n):p=n+''})((~i?1:p%10)*10**n)|p]=p),[...p].map((d,i)=>i?i<n?d.padEnd(n)+b[i]:c:a).join`
`)

在线尝试!

怎么样?

步骤#1:计算4个素数

[a, b, c] =               // save the 3 first primes into a, b and c
                          // (the 4th prime will be saved in p)
  [ 0, -1, --n, 0 ]       // decrement n and iterate over [ 0, -1, n, 0 ]
  .map(p =                // initialize p (previous prime) to a non-numeric value
       o =                // use o as a lookup table
  i =>                    // for each value i in the list defined above:
    o[                    //   update o:
      (g = n => {         //     g = recursive function taking n
        for(k = n++;      //       set k = n and increment n
            n % k--;);    //       decrement k until it's a divisor of n
                          //       (notice that k is decremented *after* the test)
        k |               //       if k is not equal to 0 (i.e. n is not prime)
        o[n] |            //       or n was already used
        p[i] - n % 10 ?   //       or the last digit of n does not match the connected
                          //       digit (if any) with the previous prime:
          g(n)            //         do a recursive call
        :                 //       else:
          p = n + ''      //         stop recursion and save n coerced to a string into p
      })(                 //     initial call to g with:
        (~i ? 1 : p % 10) //       either 10 ** n if i is not equal to -1
        * 10 ** n         //       or (p % 10) * 10 ** n if i = -1
      ) | p               //     yield p
    ] = p                 //   set o[p] = p
  )                       // end of map()

步骤#2:格式化输出

[...p].map((d, i) =>      // for each digit d at position i in the last prime:
  i ?                     //   if this is not the first digit:
    i < n ?               //     if this is not the last digit:
      d.padEnd(n)         //       append d, followed by n - 1 spaces
      + b[i]              //       append the corresponding digit in the 2nd prime
    :                     //     else (last digit):
      c                   //       append the 3rd prime
  :                       //   else (first digit):
    a                     //     append the first prime
).join`\n`                // end of map(); join with carriage returns

2

果冻89 82字节

1ịÆn⁺f®$¿
’⁵*;Æn$©µDṪṪ×ḢÇ©;@©µ;Ç⁺;0ị®¤%⁵n/Ɗ¿$$©;Ç⁺%⁵’$¿$$µŒœṪDZUḊṖj€⁶x³¤ḊḊ¤;@Ḣ;2ị$

在线尝试!

可以肯定是高尔夫球手,但可以有效地处理大量数字。



1

JavaScript,484字节

i=a=>a?(l=a=>a[(L=a=>a.length-1)(a)])(a)==9?i(r(a))+0:(r=a=>a.substr(0,L(a)))(a)+(+l(a)+1)%10:"1";s=(a,b)=>b?a==b?"":s(l(a)<l(b)?s(r(a),1):r(a),r(b))+Math.abs(l(a)-l(b)):a;m=(a,b)=>!a||!((c=L(a)-L(b))<0||!c&&a<b)&&m(s(a,b),b);p=(a,b="2")=>a/2<b||!(m(a,b)||!p(a,i(b)));a=>{for(M=1+(R=a=>"0".repeat(b))(z=a-1);!p(M=i(M)););for(N=M[z]+R(z);!p(N=i(N)););for(O=1+R(x=a-2);!p(O+n[z]);O=i(O));for(P=R(x);!p(m[0]+P+O[0]);P=i(P));for(S="\n",j=0;j<x;)S+=P[i]+R(x)+N[++i]+"\n";return M+S+O+N[z]}

最后一个未命名的函数返回ASCII码。

原始码

function inc(a){
  if (!a) return "1";
  if (a[a.length-1]=="9") return inc(a.substr(0,a.length-1))+"0";
  return a.substr(0,a.length-1)+(+a[a.length-1]+1)%10;
}
function sub(a,b){
  if (!b) return a;
  if (a==b) return "";
  var v=a.substr(0,a.length-1);
  if (a[a.length-1]<b[b.length-1]) v=sub(v,1);
  return sub(v,b.substr(0,b.length-1))+Math.abs(a[a.length-1]-b[b.length-1])
}
function multof(a,b){
  if (!a) return true;
  if (a.length<b.length||a.length==b.length&&a<b) return false;
  return multof(sub(a,b),b);
}
function isprime(a){
  for (var i="2";a/2>i;i=inc(i)){
    if (multof(a,i)) return false;
  }
  return true;
}
function square(a){
  for (var m="1"+"0".repeat(a-1);!isprime(m);m=inc(m)){}
  for (var n=m[a-1]+"0".repeat(a-1);!isprime(n);n=inc(n)){}
  for (var o="1"+"0".repeat(a-2);!isprime(o+n[a-1]);o=inc(o)){}
  for (var p="0".repeat(a-2);!isprime(m[0]+p+o[0]);p=inc(p)){}
  var s="";
  for (var i=0;i<a-2;i++) s+=p[i]+"0".repeat(a-2)+n[i+1]+"\n";
  return m+"\n"+s+o+n[a-1];
}

最佳的平均时间复杂度:Ω(100 ñ N)在Knuth的大欧米茄符号(N减去ñ位数字,10个步骤ñ每整除检查substractions,10 ñ整除检查主要检查,并做了Ω(1)主要检查)。

最差的时间复杂度:Knuth的大Ω表示法中的Ω(1000 n n)(n步用于减去n个数字,每个可除性检查减10 n个减法,用于质数检查的10 n个可除性检查,以及10 n个质数检查已完成)。

我怀疑n=100需要进行10 203次计算。

旁注:我使用UglifyJS 3验证了语法,并且比我做得更好,节省了47.13%,并获得了282个字节。但是,我决定不做我的成绩,因为我觉得它很欺骗。

i=(s=>s?9==(l=(l=>l[(L=(l=>l.length-1))(l)]))(s)?i(r(s))+0:(r=(l=>l.substr(0,L(l))))(s)+(+l(s)+1)%10:"1"),s=((L,i)=>i?L==i?"":s(l(L)<l(i)?s(r(L),1):r(L),r(i))+Math.abs(l(L)-l(i)):L),m=((l,r)=>!l||!((c=L(l)-L(r))<0||!c&&l<r)&&m(s(l,r),r)),p=((l,s="2")=>l/2<s||!(m(l,s)||!p(l,i(s))));

它只是删除了最后一个函数,因为它们从未使用过。如果分配而不删除它,实际上变得更糟,包括我添加的其他代码。


3
这似乎不完整?不打高尔夫球吗?
connectyourcharger

是。完成/打高尔夫球。
鸣子
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.