首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >超程序设计: N+N,N×N,N^N

超程序设计: N+N,N×N,N^N
EN

Code Golf用户
提问于 2016-09-11 14:30:05
回答 2查看 17.6K关注 0票数 166

编写一个包含从1到9的N的程序。在其原生形式中,您的程序应该输出N+N。例如,如果N是1,则输出2;如果N是2,则输出4;如果N是3,则输出6,等等。

当程序中的每个字符都被复制到位时,它应该是一个接收N(仍然从1到9)并输出N×N的程序,例如,如果N是1,则输出1,如果N是2,则输出4,如果N是3,则输出9等等。

当程序中的每个字符都处于三重位时,它应该是一个程序,它接受N(仍然从1到9)并输出N^N。例如,输出1 (N为1 )、4 (N为2 )、27 (N为3 )、387420489 (N为9 )等。

不需要大于9的数字,因为10^10超出了许多语言通常的整数范围。

示例

如果你的最初计划是

代码语言:javascript
复制
My_Program!
Exit();

然后,它应该能够吸收N和输出N+N。

此外,该程序

代码语言:javascript
复制
MMyy__PPrrooggrraamm!!

EExxiitt(());;

应该吸收N和输出N×N。

最后,程序

代码语言:javascript
复制
MMMyyy___PPPrrrooogggrrraaammm!!!


EEExxxiiittt((()));;;

应该吸收N和输出N^N。

不需要四倍字符的程序和其他程序。

规则

  • 输入和输出应该是普通的,通常是格式化的十进制数。您可以使用不同的基础来展示您的代码,但您的答案是非竞争性的。
  • Windows用户可能会将\r\n视为一个字符,因为像\r\r\n\n这样的东西是没有意义的,甚至是行不通的。
  • 以字节为单位的最短的原生程序( N+N程序)获胜。
EN

回答 2

Code Golf用户

发布于 2016-09-11 20:13:31

果冻,12 字节数

N+N

代码语言:javascript
复制
“(ẹ+)‘FQṖṪỌv

在网上试试!

N×N

代码语言:javascript
复制
““((ẹẹ++))‘‘FFQQṖṖṪṪỌỌvv

在网上试试!

N^N

代码语言:javascript
复制
“““(((ẹẹẹ+++)))‘‘‘FFFQQQṖṖṖṪṪṪỌỌỌvvv

在网上试试!

是如何工作的

Jelly有几种不同类型的字符串;它们都是以开头的。如果文字包含多个,则返回一个字符串数组,将字符串彼此分离。

例如,“abc“def”生成['abc', 'def']

取决于文字的最后一个字符( ”«»‘’中的任何一个,其中的«目前还没有实现),人们可以在不同类型的文字之间进行选择。对于,我们得到果冻代码页中的代码点,而不是相应的Unicode字符。

例如,“abc“def‘生成[[97, 98, 99], [100, 101, 102]]

程序中的三个文本对应于以下代码点数组。

代码语言:javascript
复制
“(ẹ+)‘           -> [40, 214, 43, 41]
““((ẹẹ++))‘      -> [[], [40, 40, 214, 214, 43, 43, 41, 41]]
“““(((ẹẹẹ+++)))‘ -> [[], [], [40, 40, 40, 214, 214, 214, 43, 43, 43, 41, 41, 41]]

N+N

代码语言:javascript
复制
“(ẹ+)‘FQṖṪỌv                          Main link. Argument: n

“(ẹ+)‘                                As before.
      F                               Flatten the array. Yields an integer array.
       Q                              Unique; deduplicate the integers.
                                      This yields [40, 214, 43, 41].
        Ṗ                             Pop; remove the last element.
         Ṫ                            Tail; extract the last element. 
                                      This yields 43, the Unicode code point of +.
          Ọ                           Unordinal; cast to character.
           v                          Eval; execute the character as a Jelly
                                      program with argument n.

N×N

代码语言:javascript
复制
““((ẹẹ++))‘‘FFQQṖṖṪṪỌỌvv              Main link. Argument: n

““((ẹẹ++))‘                           As before.
           ‘                          Increment all integers.
            FF                        Flatten the array. Yields an integer array.
              QQ                      Unique; deduplicate the integers.
                                      This yields [41, 215, 44, 42].
                ṖṖ                    Pop twice; remove the last two elements.
                  ṪṪ                  Tail; extract the last element.
                                      This yields 215, the Unicode code point of ×.
                    ỌỌ                Unordinal; cast to character.
                      v               Eval; execute the character as a Jelly
                                      program with argument n.
                       v              Eval; convert the return value (n×n) to a
                                      string and execute that string as a Jelly
                                      program with argument n. Since the string
                                      consists of a single integer literal, that
                                      integer is returned, ignoring the argument.

注意,FQ不改变一维数组、没有重复项的数组、整数和字符(分别)。

N^N

代码语言:javascript
复制
“““(((ẹẹẹ+++)))‘‘‘FFFQQQṖṖṖṪṪṪỌỌỌvvv  Main link. Argument: n

“““(((ẹẹẹ+++)))‘                      As before.
                ‘‘                    Increment all integers twice.
                  FFF                 Flatten the array. Yields an integer array.
                     QQQ              Unique; deduplicate the integers.
                                      This yields [42, 216, 45, 43].
                        ṖṖṖ           Pop thrice; remove the last three elements.
                           ṪṪṪ        Tail; extract the last element.
                                      This yields 42, the Unicode code point of *.
                              ỌỌỌ     Unordinal; cast to character.
                                 v    Eval; execute the character as a Jelly
                                      program with argument n.
                                  vv  Eval twice. See N×N.
票数 221
EN

Code Golf用户

发布于 2016-09-12 06:39:54

><>,41字节

代码语言:javascript
复制
\<
1:: :
&&* +
i*n n
c&
%:
4l
0(
.i
n}
&?

在网上试试:N+NNN^N。假设STDIN输入正好是一个字符。

<>是一种2D语言,所以我们可以利用这样一个事实:如果我们向下执行指令,那么代码语义基本上是不变的--随之而来的额外空行只是没有操作。这方面的例外是条件蹦床?,它弹出一个值,如果值为非零,则跳过下一条指令--额外的换行符会因为插入的不操作而使?陷入混乱,但是我们可以通过将?放在列的末尾并利用包装来解决这个问题。

要决定执行哪个操作,关键是40.,它将IP传送到定位(4, 0)。由于代码的扩展,x = 4列对应于基本程序的+、加倍程序的*和三倍程序的^。不幸的是,><>没有内置指数运算,这使得它成为程序的主要部分。

代码语言:javascript
复制
[Setup]
\         Mirror: reflect IP direction to downwards
1&        Put 1 into the register
ic%       Push a code point of input, then take it mod 12. This maps the char '1' to the
          number 1, and so forth for '2' to '9'.
40.       Jump to (4, 0), still heading downwards

[N+N version]
:+        Duplicate then add
n         Output as number
          (Stack is now empty, so the program errors out trying to do the above again)

[N*N version]
:*        Duplicate then multiply
n         Output as number
          (Stack is now empty, so the program errors out trying to do the above again)

[N^N version]
:&*&      Multiply register by N
:l(       Push (N < length of stack + 1)
i         Push input, but since we're now at EOF this pushes -1 (stack length += 1)
}         Move -1 to the back
?<        If (N < length + 1) was 1, execute the < to move leftward. Otherwise, skip it.
          (Continue loop)

\         Mirror: reflect IP direction upwards
&n        Output register
.         Jump to (-1, N), which is invalid so the program errors out
票数 91
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/92944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档