将整数x提高到x的幂,无需内置幂运算


16

任务-标题几乎总结了一下:将一个整数x乘x,其中0<x

限制条件:

  • 使用幂,exp()ln(),和任何其他权力相关的语言内置插件,比如pow()x^xx**x是被禁止的。
  • 您可以假定给定的整数符合您选择的编程语言的限制。

测试用例:

Input | Output
---------------
2     | 4
3     | 27
5     | 3125
6     | 46656
10    | 10000000000

这是,因此以字节为单位的最短程序获胜。


我们可以接受输入作为字符串吗?
毛茸茸的

我对此进行了编辑,希望它将重新打开。我删除了第3条,而是说,它应该是一个完整的程序,因为OP可能是打算
Xcoder先生

@ Mr.Xcoder会更好,但我建议删除(或改写)第二个限制。“不是功能”是否将JS排除在外?我也建议,对于面临的挑战而言,我们应该有手柄0和预期输出指定(01或两者)。最后,必须处理负整数将是对挑战的很好补充。
毛茸茸的

@Shaggy加js在...上苹果计算器计算出0 ^ 0回来了,它返回1,也许1应该是选择的值,因为Python也回报10^0。但是,Foundation+ Swift返回0
Xcoder先生17年

1
@ Mr.Xcoder,我删除了我们不需要处理的“限制”,0而是0<x在导入中进行了指定。我还消除了代码不应引发错误的限制;那应该不用说。如有必要,请随意回滚。
毛茸茸的

Answers:




9

Mathematica,16个字节

在此字节数下,我有两种解决方案:

1##&@@#~Table~#&

在此处,#~Table~#创建的n副本列表n。然后List替换为head,将1##&其所有参数相乘在一起。

Nest[n#&,1,n=#]&

这只是存储在输入n,然后乘1nn倍。


1
#~Product~{#}&
alephalpha

1
@alephalpha啊,好点。您可以将其作为单独的答案发布。
马丁·恩德

5

JavaScript(ES6),33 28 25 24字节

n=>g=(x=n)=>--x?n*g(x):n

尝试一下

f=
n=>g=(x=n)=>--x?n*g(x):n
o.innerText=f(i.value=3)()
i.oninput=_=>o.innerText=f(+i.value)()
<input id=i min=1 type=number><pre id=o>


历史

25字节

f=(n,x=n)=>--x?n*f(n,x):n

28字节

n=>eval(1+("*"+n).repeat(n))

33字节

n=>eval(Array(n).fill(n).join`*`)

4

纯扑43

echo $[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]

在线尝试

不知道这是否会使规则过度弯曲-我没有使用任何列出的禁止的内置函数,但是我正在使用基本转换。

  • printf 1%0$1d输出a 1后跟n 0s
  • $[b#a] 是算术扩展 a用作基数b,可以得出所需的结果。不幸的是,基数<2不起作用,因此额外的?:位处理输入n = 1。

最大输入为15,因为bash使用带符号的64位整数(最多2 31 -1)。


与我遇到的问题相同,这不适用于x = 1。尽管如此,还是非常有趣的方法。
Maxim Mikhaylov

@MaxLawnboy感谢您指出这一点-可悲地使我的回答肿。也许我可以找出另一个更短的版本……
数字创伤

酷的东西。一直希望学习bash,但总是懒惰=)

4

爱丽丝,13字节

/o
\i@/.&.t&*

在线尝试!

说明

/o
\i@/...

这是一个用于读取和写入十进制整数并完全在Cardinal模式下运行的程序的框架(因此,该程序适用于大多数算术问题)。

.    Duplicate n.
&.   Make n copies of n.
t    Decrement the top copy to n-1.
&*   Multiply the top two values on the stack n-1 times, computing n^n.


3

果冻,3 个字节

ẋ⁸P

在线尝试!

怎么样?

ẋ⁸P - Main link: x             e.g. 4
 ⁸  - link's left argument, x       4
ẋ   - repeat left right times       [4,4,4,4]
  P - product                       256

达恩,我想这样做。:P
HyperNeutrino

@Jonathan Allan是3个字节还是3个宽字符?让我们查看源代码十六进制转储,以对实际代码字节大小做出正确的决定。;-)并进行更正

1
@ xakepp35 Jelly使用SBCS,并且标头中的字节链接指向它。使用hexdump的程序F7 88 50按预期工作。
丹尼斯

@丹尼斯感谢您的回复!我永远无法想象这样的语言=)

3

库比克斯,19字节

..@OI:1*s;pu!vqW|($

在线尝试!

一步步

扩展到边长为2的立方体上

    . .
    @ O
I : 1 * s ; p u
! v q W | ( $ .
    . .
    . .
  • I:1 取得输入,将其复制并压入1。这将使用计数器,乘法器和结果来设置堆栈。
  • *s; TOS的倍数,将结果与上一个交换并删除上一个。
  • pu将柜台物品带到服务条款。掉头。这曾经是更改车道的方法,但需要剃除一个字节。
  • |($这样做是为了节省一个字节。命中时,它会跳过减量。反映,减少计数器,并跳过围绕立方体的无操作包装。
  • !vqW测试计数器。如果确实跳过重定向,则将计数器放在BOS上,将通道改回乘数。否则重定向。
  • |sO@这是从计数器测试重定向到的最终序列。经过水平反射,将TOS带给TOS带来结果,输出和停止。

3

R,22个字节

x从标准输入读取。

prod(rep(x<-scan(),x))

生成的x副本列表x,然后计算该列表元素的乘积。当时x=0rep返回numeric(0),它是length的数字向量0,但其中prod的是1,因此0^0=1通过此方法,这与R的内置指数是一致的,因此非常简洁。

在线尝试!


3

适用于Linux的x86_64机器语言,14 11 10字节

0:   6a 01                   pushq  $0x1
2:   58                      pop    %rax
3:   89 f9                   mov    %edi,%ecx
5:   f7 ef                   imul   %edi
7:   e2 fc                   loop   5
9:   c3                      retq

网上试试吧!,编译并运行以下C程序。

const char h[]="\x6a\1\x58\x89\xf9\xf7\xef\xe2\xfc\xc3";

int main(){
  for( int i = 1; i < 4; i++ ) {
    printf( "%d %d\n", i, ((int(*)())h)(i) );
  }
}




2

05AB1E,3个字节

.DP

在线尝试!尝试所有示例

.D  # pop a,b    push b copies of a 
    # 05AB1E implicitly takes from input if there aren't enough values on the stack
    # For input 5, this gives us the array: [5,5,5,5,5]
  P # Take the product of that array
    # Implicit print

看起来你喜欢.D。第一次看到它使用了。
魔术章鱼缸

啊,我不明白这里发生了什么..似乎太奇怪了,没有任何解释。=(

@ xakepp35有帮助吗?
莱利



2

x86机器代码(Linux),18字节

31 c0 ff c0 31 db 39 df 74 07 0f af c7 ff c3 eb f5 c3

它期望C声明如下 extern int XpowX(int)

拆解

XpowX:
  # edi : input register
  # ebx : counter
  # eax : result register
  xor  %eax, %eax    # result  = 0
  inc  %eax          # result += 1
  xor  %ebx, %ebx    # counter = 0
  loop:
    cmp  %ebx, %edi  # if (counter == input)
    je   done        #   return result
    imul %edi, %eax  # result  *= input
    inc        %ebx  # counter += 1
    jmp   loop
  done:
    ret

1

Brachylog,6个字节

g;?j₎×

在线尝试!

说明

          Example input: 5
g         Group: [5]
 ;?       Pair with the Input: [[5], 5]
   j₎     Juxtapose [5] 5 times: [5, 5, 5, 5, 5]
     ×    Multiply

1

CJam,7个字节

ri_a*:*

在线尝试!

说明

ri       e# Read an int from input
  _      e# Duplicate it
   a*    e# Put the copy in the array and repeat it that many times
     :*  e# Take the product of the array

1

Perl 6、13个字节

{[*] $_ xx$_}

$_ xx $_计算为的$_副本列表$_$_是匿名函数的参数),然后[*]通过乘法减少该列表。




1

Röda,17个字节

{product([_]*_1)}

在线尝试!

这是一个匿名函数,它从流中获取输入。

说明:

{product([_]*_1)}
{               } /* An anonymous function */
         [_]      /* An array containing the input value */
            *_1   /* repeated times the input value */
 product(      )  /* Product of all values in the array */

1

dc, 24 23 26 22 bytes

This is my first attempt writing a recursive macro in dc. I am sure it is a sub-optimal solution which can be improved a lot.

dsr1+[lrr1-d1<F*]dsFxp

Try it online!

Edit: Thanks eush77! -4 bytes.


1
Does not work for x=1.
eush77

You can shave off two bytes by replacing lr sequences at the end with two ds at the beginning.
eush77

Actually, you don't need that. Just increment the top of the stack before calling for the first time. This way you will end up with x copies of x on the stack (and 1 of course), and x multiplications thereafter. So the ending can just be plain dsFxp.
eush77

@eush77 I was about to say that removing second lr wouldn't work here. It's my first time golfing in a stack-based language, so it feels very unusual. Thanks for your help!
Maxim Mikhaylov

1

Batch, 58 bytes

@set n=1
@for /l %%i in (1,1,%1)do @set/an*=%1
@echo %n%

Only works for single-digit inputs due to 32-bit arithmetic.


1

brainf*ck, 148 bytes

,[->+>+<<]>>[-<<+>>]++++++++[<------<------>>-]<[->>+>>+<<<<]>>[-<<+>>]>>-[-<<<<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>[-<<+>>]>>>]<<<++++++++[-<<++++++>>]<<.

Try it online!

No built-ins ;)

How it works

,                                       - get ascii input
[->+>+<<]                               - duplicate input
>>[-<<+>>]                              - shift inputs left to start
++++++++[<------<------>>-]             - convert ascii into input numbers
<[->>+>>+<<<<]                          - get loop intervals (same as input #)
>>[-<<+>>]                              - shift input back again
>>-[-<<<<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>  - iterated addition (multiplication)
[-<<+>>]>>>                             - Shift output back into input
]<<<++++++++[-<<++++++>>]<<.            - convert final output to ascii

In a nutshell, this works by multiplying x (the input) by itself x times. (a.k.a. iterating iterated addition). The net result is x^x.

I/O

The program takes a single ASCII input, and processes it as it's ASCII index minus 48. The minus 48 is to normalize inputs of actual numbers (4 becomes 52 -> 52-48 -> 4). To input a number higher than 9, use the next corrosponging ASCII character (: -> 58-48 -> 10). The program ouputs in a similar fashion.

Test I/O

INPUT > PROCESSED INPUT >> OUTPUT > TRANSLATED OUTPUT
1 > 1 >> 1 > 1
2 > 2 >> 4 > 4
3 > 3 >> K > 27

Since there are no printable ASCII characters after an input of 3, it can only print numbers in theory. Though, you can check all inputs do in fact work on visualizers such as this.



1

Python, 32 bytes

f=lambda g,z=1:z>g or g*f(g,z+1)

Try it online!


Welcome to PPCG! You don't need to count the f= part, so you can shorten your submission to 30 bytes.
Steadybox

@Steadybox The f= part does need to be counted, because it's recursive, so it relies upon the function being named f in order to work properly
musicman523

@musicman523 Yes, you are right.
Steadybox

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.