Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Language agnostic 代码高尔夫:乌拉姆螺旋 挑战_Language Agnostic_Code Golf_Rosetta Stone - Fatal编程技术网

Language agnostic 代码高尔夫:乌拉姆螺旋 挑战

Language agnostic 代码高尔夫:乌拉姆螺旋 挑战,language-agnostic,code-golf,rosetta-stone,Language Agnostic,Code Golf,Rosetta Stone,按字符计数输出的最短代码,螺旋大小由用户输入给定 乌拉姆的螺旋是映射素数的一种方法。螺旋线从位于中心的数字1开始(1不是质数),并围绕它生成一个螺旋线,将所有质数标记为字符“*”。非素数将打印为空格“” 测试用例 代码计数包括输入/输出(即完整程序)。第一篇文章(哦,等等,这不是吗?) 我的Clojure团队作品,685528个字符 (defn ulam[n] (let [z (atom [1 0 0 {[0 0] " "}]) m [[0 1 1 0][2 -1 0 -1][2 0 -1 0

按字符计数输出的最短代码,螺旋大小由用户输入给定

乌拉姆的螺旋是映射素数的一种方法。螺旋线从位于中心的数字1开始(1不是质数),并围绕它生成一个螺旋线,将所有质数标记为字符“
*
”。非素数将打印为空格“

测试用例
代码计数包括输入/输出(即完整程序)。

第一篇文章(哦,等等,这不是吗?)

我的Clojure团队作品,685528个字符

(defn ulam[n] (let [z (atom [1 0 0 {[0 0] " "}])
m [[0 1 1 0][2 -1 0 -1][2 0 -1 0][2 0 0 1][2 0 1 0]]
p (fn [x] (if (some #(zero? (rem x %)) (range 2 x)) " " "*"))]
(doseq [r (range 1 (inc n)) q (range (count m)) [a b dx dy] [(m q)]
s (range (+ (* a r) b))]
(let [i (inc (first @z)) x (+ dx (@z 1)) y (+ dy (@z 2))]
(reset! z [i x y (assoc (last @z) [x y] (p i))])))
(doseq [y (range (- n) (inc n))] (doseq [x (range (- n) (inc n))]
(print ((last @z) [x y]))) (println))))
(ulam (dec (.nextInt (java.util.Scanner. System/in))))---

Input:
5
Output:
    * *  
 *     * 
* *   *  
   * * * 
  *  ** *
 * *     
*   *    
 *   *   
*     *  

我的第一个密码高尔夫

Ruby,309301283271265个字符 Ruby 1.8.7194个字符 出于某种原因,ruby1.9希望第4行上有另一个空间:

r.times{|i|l[c]=i+1;c=i==0||l[c-n]&&!l[c+1]?c+1:l[c-1]&&!l[c-n]?c-n :l[c+n]?c-1:c+n}

Python,299个字符:

from sys import *
def t(n):
 if n==1:return ' '
 for i in range(2,n):
  if n%i==0:return ' '
 return '*'
i=int(stdin.readline())
s=i*2-1
o=['\n']*(s+1)*s
c=1
g=2
d=0
p=(s+2)*(i-1)
for n in range(s**2):
 o[p]=t(n+1);p+=[1,-s-1,-1,s+1][d%4];g-=1
 if g==c:d+=1
 if g==0:d+=1;c+=1;g=2*c
print ''.join(o)
>> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
2
* *
  *
*  
>> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
3
*   *
 * * 
*  **
 *   
  *  
>> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
5
    * *  
 *     * 
* *   *  
   * * * 
  *  ** *
 * *     
*   *    
 *   *   
*     *  
MATLAB:182 167 156个字符

p[_]='*'
p _=' '
main=interact$unlines.u.read
i n=p[x|x<-[2..n],n`mod`x==0]
u(n+1)=map(map(i.f.o).zip[-n..n].replicate((n+1)*2-1))[n,n-1..(-n)]
f(x,y,z)=4*y*y-x-y+1+if z then 2*(x-y)else 0
o(u,v)=if(v> -u)==(v<u)then(v,u,v<u)else(u,v,v<u)
脚本
ulam.m

A=1;b=ones(1,4);for i=0:(input('')-2),c=b(4);b=b+i*8+(2:2:8);A=[b(2):-1:b(1);(b(2)+1:b(3)-1)' A (b(1)-1:-1:c+1)';b(3):b(4)];end;disp(char(isprime(A)*10+32))
格式稍微好一点:

A = 1;
b = ones(1,4);
for i = 0:(input('')-2),
  c = b(4);
  b = b+i*8+(2:2:8);
  A = [b(2):-1:b(1); (b(2)+1:b(3)-1)' A (b(1)-1:-1:c+1)'; b(3):b(4)];
end;
disp(char(isprime(A)*10+32))
测试用例:

>> ulam
2
* *
  *
*  
>> ulam
3
*   *
 * * 
*  **
 *   
  *  
>> ulam
5
    * *  
 *     * 
* *   *  
   * * * 
  *  ** *
 * *     
*   *    
 *   *   
*     *  
Lua,302个字符 lua ulam.lua 6的输出:

* * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * Haskell-224个字符
这是一个不同的算法(类似于@drhirsch),不幸的是,我似乎无法在239个字符以下找到它

p[_]='*'
p _=' '
main=interact$unlines.u.read
i n=p[x|x<-[2..n],n`mod`x==0]
u(n+1)=map(map(i.f.o).zip[-n..n].replicate((n+1)*2-1))[n,n-1..(-n)]
f(x,y,z)=4*y*y-x-y+1+if z then 2*(x-y)else 0
o(u,v)=if(v> -u)==(v<u)then(v,u,v<u)else(u,v,v<u)
p[]='*'
p u=“”
main=交互$unlines.u.read
i n=p[x | x-u)==(vPython 2.x,220C 213C 207C 204C 201C 198C 196C 188C
特别感谢您在
#stackoverflow
on中提供了一些提示。输出包括前导和尾随换行符

import math
v=input()*2
w=v-1
a=['\n']*w*v
p=w*v/2
for c in range(1,w*w):a[p]=' *'[(c>1)*all(c%d for d in range(2,c))];x=int(math.sqrt(c-1));p+=(-1)**x*((x*x<c<=x*x+x)*w+1)
print''.join(a)
导入数学
v=输入()*2
w=v-1
a=['\n']*w*v
p=w*v/2
对于范围(1,w*w)中的c:a[p]='*'[(c>1)*全部(c%d表示范围(2,c))中的d);x=int(math.sqrt(c-1));p+=(-1)**x*((x*xPython-203个字符)
它的工作原理
这个想法是用需要打印为“*”的x,y坐标填充A
该算法从对应于2的单元格开始,因此避免了测试1的素性的特殊情况。
x、 y是感兴趣的单元格
j、 k跟踪是否需要inc或dec x或y才能到达下一个单元格
s是下一个拐角处i的值
t跟踪到s的增量

all(R(2,i)中d的i%d)进行素性检查

最后一行相当笨拙,它遍历所有单元格并决定是放置空格还是星号

C,208 206 201 200 199 196 194 193 193 188 185 183 180 176字节 (如果删除了换行符):

警告。此程序运行缓慢,因为is会进行2^31的试除法。但is会生成所需的输出:

    * *
 *     *
* *   *
   * * *
  *  ** *
 * *
*   *
 *   *
*     *
在格式良好的C语言中,带有冗余#包括:

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

int main(int argc, char** argv) {

    int u,v,x,y,d,S = atoi(argv[1]);

    /* v is the y coordinate of grid */
    for (v=S; v>=-S; --v)

        /* u is the x coordinate. The second operand (!putchar...) of the boolean or
         * is only ececuted a a end of a x line and it prints a newline (10) */
        for (u=-S; u<=S || !putchar(10); ++u) {

            /* x,y are u,v after "normalizing" the coordintes to quadrant 0
               normalizing is done with the two comparisions, swapping and and
               an additional term later */
            d = v<u;
            x=u;
            y=v;

            if (v<=-u ^ d) {
                x=v;
                y=u;
            }

            /* reuse x, x is now the number at grid (u,v) */
            x = 4*y*y -x-y+1 +2*d*(x-y);   

           /* primality test, y resused as loop variable, won't win a speed contest */
            for (y=2; y<x && x%y; ++y)
                 ;

            putchar(y!=x?' ':'*');
        }
}
#包括
#包括
int main(int argc,字符**argv){
intu,v,x,y,d,S=atoi(argv[1]);
/*v是网格的y坐标*/
对于(v=S;v>=-S;--v)
/*u是x坐标。布尔or的第二个操作数(!putchar…)
*仅在x行的一端剪切,并打印换行符(10)*/
对于(u=-S;upython284266 256 243242 240个字符)
我想尝试递归,我相信它可能会大大缩短:

r=range
def f(n):
 if n<2:return[[4]]
 s=2*n-1;z=s*s;c=[r(z-2*s+2,z-3*s+2,-1)];e=1
 for i in f(n-1):c+=[[c[0][0]+e]+i+[c[0][-1]-e]];e+=1
 c+=[r(z-s+1,z+1)];return c
for l in f(input()):print''.join(' *'[all(x%f for f in r(2,x))]for x in l)
r=范围
def f(n):
如果是nPython-171
Hirsch博士的C移植到python

S=input();R=range(-S+1,S)
for w in R:
 p="";v=-w
 for u in R:d=v<u;x,y=[(u,v),(v,u)][(w>=u)^d];x=4*y*y-x-y+1+2*d*(x-y);p+=" *"[(x>1)*all(x%f for f in range(2,x))]
 print p
S=input();R=range(-S+1,S)
对于R中的w:
p=“”;v=-w
对于R中的u:d=v=u)^d];x=4*y*y-x-y+1+2*d*(x-y);p+=“*”[(x>1)*全部(x%f表示范围(2,x)中的f)]
打印p
echo 20| python ulam.py * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * J解决方案:197173 165 161字节(到目前为止)
这不使用OP评论中提到的方法

p=:j./<.-:$g=:1$~(,])n=:<:+:".1!:1]3
d=:j.r=:1
(m=:3 :'if.y<*:n do.if.0=t=:<:t do.d=:d*0j1[t=:<.r=:r+0.5 end.m>:y[g=:y(<+.p=:p+d)}g end.')t=:2
1!:2&2(1 p:g){' *'

p=:j./不如前一个C条目漂亮,但这是我的

注意:我之所以发帖是因为它采取了与前一个不同的方法,主要是

  • 没有坐标重新映射
  • 它给出了与测试相同的结果
  • 它在输入大于9的情况下工作(两位数-否
    -47
    技巧)

    以下是一些输出:

        $ ./ulam_compr 3
    *   *
     * * 
    * **
     *   
      *  
    
        $ ./ulam_compr 5
        * *  
     *     * 
    * *   *  
       * * * 
      * ** *
     * *     
    *   *    
     *   *   
    *     *  
    
    Golfscript-92个字符 ~.(:S+,:R{S\:|;R{S-:$|>'*'1/[|$.|]2/@:d | ~)$ 97个字符
    ~.(:S+,:R{S\-:|;R{S-:$|>'*'1/[|$.|]2/@:d | ~${$\%!},!=}%n}%

    99个字符
    ~.(:S+,{S-}%:R{~):{:R{:$}>'*'1/[\$.]2/@:d}~${$\%!},!=}%n}%

    100个字符
    ~:S.(+,{S(-}%:R{~):{:$}>'*'1/[\$.]2/@:d}~)${$\%!},!=}%n}%

    101个字符
    ~:S.(+,{S(-}%:R{~):v;R{:$v>:d;'*'1/[v$.v]2/v~)${$\%!},!=}%n}%

    Python-176 这一个以一长串的换行符和重播开始
    main(int u,char**b){
    for(int v,x,y,S=v=**++b-48;--v>-S;putchar(10))
    for(u=-S;++u<S;){
    x=u;y=v;v>-u^v<u?:(x=v,y=u);
    x=4*y*y-x-y+1+2*(v<u)*(x-y);
    for(y=1;x%++y;);
    putchar(y^x?32:42);}}
    
    > gcc -std=c99 -o ulam ulam.c
    
        * *
     *     *
    * *   *
       * * *
      *  ** *
     * *
    *   *
     *   *
    *     *
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv) {
    
        int u,v,x,y,d,S = atoi(argv[1]);
    
        /* v is the y coordinate of grid */
        for (v=S; v>=-S; --v)
    
            /* u is the x coordinate. The second operand (!putchar...) of the boolean or
             * is only ececuted a a end of a x line and it prints a newline (10) */
            for (u=-S; u<=S || !putchar(10); ++u) {
    
                /* x,y are u,v after "normalizing" the coordintes to quadrant 0
                   normalizing is done with the two comparisions, swapping and and
                   an additional term later */
                d = v<u;
                x=u;
                y=v;
    
                if (v<=-u ^ d) {
                    x=v;
                    y=u;
                }
    
                /* reuse x, x is now the number at grid (u,v) */
                x = 4*y*y -x-y+1 +2*d*(x-y);   
    
               /* primality test, y resused as loop variable, won't win a speed contest */
                for (y=2; y<x && x%y; ++y)
                     ;
    
                putchar(y!=x?' ':'*');
            }
    }
    
    r=range
    def f(n):
     if n<2:return[[4]]
     s=2*n-1;z=s*s;c=[r(z-2*s+2,z-3*s+2,-1)];e=1
     for i in f(n-1):c+=[[c[0][0]+e]+i+[c[0][-1]-e]];e+=1
     c+=[r(z-s+1,z+1)];return c
    for l in f(input()):print''.join(' *'[all(x%f for f in r(2,x))]for x in l)
    
    S=input();R=range(-S+1,S)
    for w in R:
     p="";v=-w
     for u in R:d=v<u;x,y=[(u,v),(v,u)][(w>=u)^d];x=4*y*y-x-y+1+2*d*(x-y);p+=" *"[(x>1)*all(x%f for f in range(2,x))]
     print p
    
    echo 20 |python ulam.py * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    p=:j./<.-:$g=:1$~(,])n=:<:+:".1!:1]3
    d=:j.r=:1
    (m=:3 :'if.y<*:n do.if.0=t=:<:t do.d=:d*0j1[t=:<.r=:r+0.5 end.m>:y[g=:y(<+.p=:p+d)}g end.')t=:2
    1!:2&2(1 p:g){' *'
    
    enum directions_e { dx, up, sx, dn } direction;
    
    int main (int argc, char **argv) {
        int len = atoi(argv[1]);
        int offset = 2*len-1;
        int size = offset*offset;
        char *matrix = malloc(size);
        int startfrom = 2*len*(len-1);
        matrix[startfrom] = 1;
        int next = startfrom;
        int count = 1;
        int i, step = 1;
        direction = dx ;
    
        for (;; step++ )
            do { 
                for ( i = 0 ; i < step ; i++ ) {
                    switch ( direction ) {
                        case dx:
                            next++;
                            break;
                        case up:
                            next = next - offset;
                            break;
                        case sx:
                            next--;
                            break;
                        case dn:
                            next = next + offset;
                    }
                    int div = ++count;
                    do {
                        div--;
                    } while ( count % div );
                    if ( div > 1 ) {
                        matrix[next] = ' ';
                    }
                    else { 
                        matrix[next] = '*';
                    }
                    if (count >= size) goto dontusegoto;
                }
                direction = ++direction % 4;
            } while ( direction %2);
    dontusegoto:
        for ( i = 0 ; i < size ; i++ ) {
            putchar(matrix[i]);
            if ( !((i+1) % offset) ) putchar('\n'); 
        }
        return 0;
    }
    
    main(int a,char**v){
        int l=atoi(v[1]),o=2*l-1,z=o*o,n=2*l*(l-1),c=1,i,s=1,d;
        char*m=malloc(z);
        m[n]=1;
        for(;;s++)do{
                for(i=0;i<s;i++){
                    if(d==0)n++;
                    else if(d==1)n-=o;
                    else if(d==2)n--;
                    else n+=o;
                    int j=++c;
                    while(c%--j);
                    if(j>1)m[n]=' ';else m[n]='*';
                    if(c>=z)goto g;
                }d=++d%4;}while(d%2);
    g:for(i=0;i<z;i++){
            putchar(m[i]);
            if(!((i+1)%o))putchar('\n');
        }
    }
    
        $ ./ulam_compr 3
    *   *
     * * 
    * **
     *   
      *  
    
        $ ./ulam_compr 5
        * *  
     *     * 
    * *   *  
       * * * 
      * ** *
     * *     
    *   *    
     *   *   
    *     *  
    
    w=input()*2;v=w-1;a=['\n']*v*w;p=w/2*v-1;d=0;z=[w,-1,-w,1]
    for i in range(v*v):a[p]=' *'[i and all((i+1)%f for f in range(2,i))];d=d%4-(a[p+z[d-1]]<' ');p+=z[d]
    print"".join(a)
    
    w=input()*2;v=w-1;a='\n'*v*w;p=w/2*v-1;d=0;z=[w,-1,-w,1]
    for i in range(v*v):a=a[:p]+' *'[i and all((i+1)%f for f in range(2,i))]+a[p+1:];d=d%4-(a[p+z[d-1]]<' ');p+=z[d]
    print a
    
    p=(v=(w=gets.to_i*2)-1)*w/2-1
    a='
    '*v*w
    d=0
    (v*v).times{|i|a[p]="1"*(i+1)!~/^1?$|^(11+?)\1+$/?42:32;d=(a[p+(z=[w,-1,-w,1])[d-1]]<32)?(d-1):d%4;p+=z[d]}
    puts a
    
    disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
    
    >> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
    2
    * *
      *
    *  
    >> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
    3
    *   *
     * * 
    *  **
     *   
      *  
    >> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
    5
        * *  
     *     * 
    * *   *  
       * * * 
      *  ** *
     * *     
    *   *    
     *   *   
    *     *  
    
    l = Length; t = Table; f = Flatten;
    h@m_ := With[{x = l@m[[1]], y = l@m}, f[{{Reverse@t[w + y + (x y), {w, x + 2}]}, 
      t[f[{(x y) + x + y + 2 + w, m[[w]], (x y) + y - w + 1}], {w, y}], 
      {t[2 + y + x + y + w + (x y), {w, x + 2}]}}, 1]];
    m_~g~z_ := Nest[h, m, z] /. {_?PrimeQ -> "\[Bullet]", _Integer -> ""};
      Grid[{{1}}~g~#, Frame -> All] &
    
    Grid[{{1}}~g~#, Frame -> All] &[13]