这些矿山永远是实线


12

让我们创建一个N×N的空格和下划线网格,可用于直观地确定数字是否为质数。(N可以是任何正整数。)

该网格具有三个简单规则:

  1. 第n列包含n-1下划线的重复模式,后跟一个空格。此模式从第一行开始,并在第N行停止(可能在中间模式)。(行和列为1索引)。
  2. 第一列将替换为所有下划线而不是所有空格。
  3. 如果行索引等于列索引的某个位置出现空格,则将其替换为下划线。

示例:N = 10

           1
  1234567890 <-- column indices
 1__________
 2__________
 3__________
 4_ ________
 5__________
 6_  _______
 7__________
 8_ _ ______
 9__ _______
10_ __ _____
 ^ row indices

索引只是为了清楚起见。普通网格本身(程序必须输出的)是:

__________
__________
__________
_ ________
__________
_  _______
__________
_ _ ______
__ _______
_ __ _____

注意:

  • 第一列是所有下划线。
  • 除了第二行的下划线外,第二列转到下划线空间,下划线空间等。
  • 第三列为下划线下划线空间,下划线下划线空间等,除了第3行的下划线。
  • 等等

还要注意,除1外,只有质数行在每一列中都有下划线。

由于下划线跨越整个字体宽度,因此每个以撇号标记的行均形成连续的实线。因此从视觉上检查数字是否为质数非常容易;只需检查其行在所有列中是否均线。(实际上,查找行索引的平方根就足够了,但是输出该网格似乎不太优雅。)

程序

编写一个程序,将通过stdin(或最接近的替代方案)在给定N的情况下绘制这些网格。输出将输出到stdout(或最接近的替代输出),并且应仅包含空格,下划线和换行符,并带有可选的尾随换行符。

最短的代码获胜。


我不知道如何确定素数
Filip Bartuzi 2014年

5
@FilipBartuzi也许可以查看en.wikipedia.org/wiki/Sieve_of_Eratosthenes

“第n列包含n-1个下划线的重复模式,后跟一个空格。” 因此,列n = 1难道不是所有空格,不是所有下划线吗?
algorithmhark

6
我必须说我不太喜欢“无尾随换行符”的要求。在某些语言中,这是非常困难的,在其他语言中,这是完全不可能的,它只会影响打印到STDOUT的程序。
丹尼斯

4
该描述使我难以理解。这就是我的描述方式:从N行的N个下划线的网格开始。对于第nth行,k如果第一个字符k的除数m不是1或,则将第个字符设置为空格m
Casey Chu

Answers:


7

CJam,33 28 27字节

q~,:)_f{f{md\1=+'_S?}0'_tN}

在线尝试。

怎么运行的

q~        " Read one line from STDIN and evaluate it.                                     ";
,:)       " For “N”, push [ 1 ... N ].                                                    ";
_         " Push a copy.                                                                  ";
f{        " For each “I in [ 1 ... N ], push “I   [ 1 ... N ]”; then:                     ";
  f{      " For each “J in [ 1 ... N ], push “J   I”; then:                               ";
    md\   " Push “J % I   J / I”.                                                         ";
    1=+   " Calculate “J % I + (J / I == 1)”.                                             ";
    '_S?  " Push an underscore if the result is truthy and a space otherwise.             ";
  }       "                                                                               ";
  0'_t    " Replace the first character of the resulting array by an underscore.          ";
  N       " Push a newline.                                                               ";
}         "                                                                               ";

运行示例

$ cjam solid-primes.cjam <<< 10
__________
__________
__________
_ ________
__________
_  _______
__________
_ _ ______
__ _______
_ __ _____
$ cjam solid-primes.cjam <<< 100 | md5sum # trailing newline
e4396b316989813dada21141b569ccf9  -

打完球后我会发表解释。
丹尼斯

9
33个字节,您还没有打高尔夫球?

5

Ruby 77 73个字符

puts (1..n=gets.to_i).map{|a|?_+(2..n).map{|b|a!=b&&a%b==0? ' ':?_}.join}

我使用的一些技巧:

  • ..运营商几乎在Ruby中所有运营商的优先级最低,所以(1..n=gets.to_i)只是工作。

  • a!=1在检查字符是否应该是空格而不是下划线时(因为第一行都是下划线),所以没有添加额外的条件,而是从开始2并在其前面添加了extra ?_

  • A行可以变成B行:

    A | a%b==0&&a!=b ? ' ':?_
    B | a!=b&&a%b==0? ' ':?_
    

    因为我必须有之间的额外的空间b,并?在A线,但是这并不需要之间0?在B线b?是一个有效的Ruby的方法,但0?并非如此。

  • puts会自动为您添加带有换行符的数组,从而消除了额外的麻烦*"\n"

输出为n=100

____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
_ __________________________________________________________________________________________________
____________________________________________________________________________________________________
_  _________________________________________________________________________________________________
____________________________________________________________________________________________________
_ _ ________________________________________________________________________________________________
__ _________________________________________________________________________________________________
_ __ _______________________________________________________________________________________________
____________________________________________________________________________________________________
_   _ ______________________________________________________________________________________________
____________________________________________________________________________________________________
_ ____ _____________________________________________________________________________________________
__ _ _______________________________________________________________________________________________
_ _ ___ ____________________________________________________________________________________________
____________________________________________________________________________________________________
_  __ __ ___________________________________________________________________________________________
____________________________________________________________________________________________________
_ _  ____ __________________________________________________________________________________________
__ ___ _____________________________________________________________________________________________
_ ________ _________________________________________________________________________________________
____________________________________________________________________________________________________
_   _ _ ___ ________________________________________________________________________________________
____ _______________________________________________________________________________________________
_ __________ _______________________________________________________________________________________
__ _____ ___________________________________________________________________________________________
_ _ __ ______ ______________________________________________________________________________________
____________________________________________________________________________________________________
_  _  ___ ____ _____________________________________________________________________________________
____________________________________________________________________________________________________
_ _ ___ _______ ____________________________________________________________________________________
__ _______ _________________________________________________________________________________________
_ ______________ ___________________________________________________________________________________
____ _ _____________________________________________________________________________________________
_   _ __ __ _____ __________________________________________________________________________________
____________________________________________________________________________________________________
_ ________________ _________________________________________________________________________________
__ _________ _______________________________________________________________________________________
_ _  __ _ _________ ________________________________________________________________________________
____________________________________________________________________________________________________
_  __  ______ ______ _______________________________________________________________________________
____________________________________________________________________________________________________
_ _ ______ __________ ______________________________________________________________________________
__ _ ___ _____ _____________________________________________________________________________________
_ ____________________ _____________________________________________________________________________
____________________________________________________________________________________________________
_   _ _ ___ ___ _______ ____________________________________________________________________________
______ _____________________________________________________________________________________________
_ __ ____ ______________ ___________________________________________________________________________
__ _____________ ___________________________________________________________________________________
_ _ ________ ____________ __________________________________________________________________________
____________________________________________________________________________________________________
_  __ __ ________ ________ _________________________________________________________________________
____ _____ _________________________________________________________________________________________
_ _ __  _____ _____________ ________________________________________________________________________
__ _______________ _________________________________________________________________________________
_ __________________________ _______________________________________________________________________
____________________________________________________________________________________________________
_     ___ _ __ ____ _________ ______________________________________________________________________
____________________________________________________________________________________________________
_ ____________________________ _____________________________________________________________________
__ ___ _ ___________ _______________________________________________________________________________
_ _ ___ _______ _______________ ____________________________________________________________________
____ _______ _______________________________________________________________________________________
_  __ ____ __________ __________ ___________________________________________________________________
____________________________________________________________________________________________________
_ _ ____________ ________________ __________________________________________________________________
__ ___________________ _____________________________________________________________________________
_ __ _ __ ___ ____________________ _________________________________________________________________
____________________________________________________________________________________________________
_   _ _  __ _____ _____ ___________ ________________________________________________________________
____________________________________________________________________________________________________
_ __________________________________ _______________________________________________________________
__ _ _________ _________ ___________________________________________________________________________
_ _ ______________ __________________ ______________________________________________________________
______ ___ _________________________________________________________________________________________
_  __ ______ ____________ ____________ _____________________________________________________________
____________________________________________________________________________________________________
_ _  __ _ _____ ___ ___________________ ____________________________________________________________
__ _____ _________________ _________________________________________________________________________
_ ______________________________________ ___________________________________________________________
____________________________________________________________________________________________________
_   _  ____ _ ______ ______ _____________ __________________________________________________________
____ ___________ ___________________________________________________________________________________
_ ________________________________________ _________________________________________________________
__ _________________________ _______________________________________________________________________
_ _ ___ __ __________ _____________________ ________________________________________________________
____________________________________________________________________________________________________
_  _  __  ____ __ ___________ ______________ _______________________________________________________
______ _____ _______________________________________________________________________________________
_ _ __________________ ______________________ ______________________________________________________
__ ___________________________ _____________________________________________________________________
_ ____________________________________________ _____________________________________________________
____ _____________ _________________________________________________________________________________
_   _ _ ___ ___ _______ _______ _______________ ____________________________________________________
____________________________________________________________________________________________________
_ ____ ______ __________________________________ ___________________________________________________
__ _____ _ _____________________ ___________________________________________________________________
_ _  ____ _________ ____ ________________________ __________________________________________________

现在包括Extra-Special™Mega-Colorful Red©Highlight-Magic™®Extended Edition©:(单击缩略图可放大)

红宝石色,110个字符

puts (1..n=gets.to_i).map{|a|x=(2..n).map{|b|a!=b&&a%b==0? ' ':?_}.join;x.index(' ')??_+x :"\e[41m_#{x}\e[0m"}

扩展版


1
看起来应该是分形的……
Beta Decay

删除三元组内的空格' '。它可能会弄乱您的语法荧光笔,但仍然可以正常工作。此外,a%b<1
Value Ink

*''将与join相同,并且您可以检查a<b而不是,a!=b因为a的因数都不大于a。将a划分为两个字符的字符串,并在a和b上进行一些数学运算,而不使用三进制,这也可能会节省成本。
histocrat

5

J-28个字符

1('_ '{~0==+&|:1&=+|/~)@:+i.

爆炸解释:

1(                    )@:+i.  NB. on vector 1..n, do
                   |/~        NB. take the table "column modulo row"
               1&=+           NB. add 1 to column 1
          =                   NB. identity matrix of size 10
            &|:               NB. transpose both tables (now table is row mod col)
           +                  NB. add them
  '_ '{~0=                    NB. space if cell=0 else underscore

看起来像什么:

   1('_ '{~0==+&|:1&=+|/~)@:+i.25
_________________________
_________________________
_________________________
_ _______________________
_________________________
_  ______________________
_________________________
_ _ _____________________
__ ______________________
_ __ ____________________
_________________________
_   _ ___________________
_________________________
_ ____ __________________
__ _ ____________________
_ _ ___ _________________
_________________________
_  __ __ ________________
_________________________
_ _  ____ _______________
__ ___ __________________
_ ________ ______________
_________________________
_   _ _ ___ _____________
____ ____________________

4

蟒蛇2,76 71

r=range(1,input()+1)
for i in r:print''.join("_ "[i%j<1<j<i]for j in r)

不知道它是否可以比这短一些...这些关键字:rangeinput并且print花费很多。


3
您可以替换i>j>1and i%j<1i>j>1>i%j
seequ 2014年

@Sieg:太酷了!实际上,这是我第一次利用这些级联的比较。
Falko 2014年

我已经将这个确切的字符代码写成var名称,包括i%j<1<j<i:-P。因此,也许它真的不会变得更短。
xnor 2014年

实际上,我认为(未经测试)可以做到i%j<1。这意味着i>=j
seequ 2014年

@Sieg:是的,但我们需要的i>j,而不是i>=j为了避免在对角线上的空白。
Falko 2014年

3

APL(28)

'_ '[(1+(1≠⊢)∧≠∧0=|⍨)/¨⍳2⍴⎕]

说明:

  • ⍳2⍴⎕:读取数字N并制作一个N×N的坐标矩阵
  • (... )/¨:对于每对坐标,应用以下功能:
    • 在以下情况下,单元格必须有一个空格:
    • 0=|⍨:y modx = 0,并且
    • :x不等于y
    • 1≠⊢:x不是 1
    • 1+1由于APL数组从1开始,所以添加到结果位矩阵中。
  • '_ '[... ]:用1下划线和2空格代替。

显然28是这里的神奇高尔夫号码。

2

Perl,    69   61

更新版本(感谢丹尼斯!)

$n=<>;for$i(1..$n){say"_".join"",map{$i%$_|$i==$_?_:$"}2..$n}

原始版本:

$n=<>;for$i(1..$n){say"_".join("",map{$i%$_||$i==$_?"_":" "}(2..$n))}

1
1.无论是join也不2..$n需要括号。2.通过-n开关,您可以使用$_代替$n。3. _是有效的裸词,因此不需要引号。4.您可以使用$"代替" "。5.您可以使用| 代替||
丹尼斯

@丹尼斯-谢谢!我应用了其中一些。我之所以没有这样做,-n是因为我想将其保持为一个独立的程序,而不必说$^N=1_在情况下,使用as作为裸字有效,$i==_但在情况下则无效,$i%_因为解析器认为%_是哈希值。
托德·雷曼

1
1.您可以使用shebang(#!/bin/perl -n通常将其计为1个字节)来保持其独立性,但这显然取决于您。我不知道该怎么办$^N=1... 2. $i==_无法正常工作;它会测试是否$i == "_"。我的意思是使用_代替"_",即say _$i==$_?_:$"
丹尼斯

@丹尼斯-哦,胡扯,你是对的。我所做的编辑在矩阵中引入了一条对角线。没让我感到羞耻。固定。我明白您的意思是_= "_"现在。不幸的是,它在后一种情况下有效,但是在旁边却给我一个错误,say因为它似乎认为这是一个文件句柄。
托德·雷曼

2

CJam,27个字节

q~:I,{__I?'_*S+I*I<'_t}%zN*

在线尝试。

这种方法获得的字节数与我的其他答案相同,但是我仍然认为值得发布。与其在每一行中标记适当的倍数,不如规范中所说明的那样。

怎么运行的

q~:I                         " Read an integer I from STDIN.                              ";
    ,{                }%     " For each J ∊ {1,...,I}:                                    ";
          '_*S+              " Push J - 1 underscores followed by a space.                ";
               I*I<          " Repeat the pattern to complete I characters.               ";
       _I?                   " For the first row, replace all spaces with underscores.    ";
      _            '_t       " Replace the Jth character by an underscore.                ";
                        zN*  " Transpose rows and columns; separate with linefeeds.       ";

运行示例

$ cjam prime-lines.cjam <<< 10; echo
__________
__________
__________
_ ________
__________
_  _______
__________
_ _ ______
__ _______
_ __ _____
$ cjam prime-lines.cjam <<< 100 | md5sum # no trailing newline
83c7b974e1510f482322456a9dbb461f  -

1

C 143

对于这种情况,C显然不是正确的语言选择。但是为了完整起见,这是在C语言中完成此操作的一种可能方法。适用于n值(最大为1048575)。从标准输入中读取n

#include <stdio.h>
main(){int n,i=0,j;scanf("%d",&n);char x[1<<20];x[n+1]=0;
for(;++i<=n;){for(j=0;++j<=n;)x[j]=i%j|i==j|j==1?95:32;puts(x+1);}}

但是,它非常快。

  • 在我的系统上,n = 1,000,000(产生1,000,000,000,000元素的网格)的运行时间约为55分钟。

  • 运行时间Ñ = 1000(其produes一百万元网格)小于1/100秒。


4
1.某些编译器(例如,GCC)不需要包含,因此您可以将其删除。2.全局变量初始化为0,默认为int,因此可以使用char x[1<<20];n,i,j;main...。3.在上for(scanf("%d",&n);i++<n;)保存两个字节scanf("%d",&n);for(;++i<=n;)
丹尼斯
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.