从原动力中恢复动力


16

似乎很多人都想拥有此功能,因此现在是此挑战的续集!

定义素数幂是自然数,可以以p n的形式表示,其中p是素数,n是自然数。

任务:给定素数幂p n > 1,返回幂n。

测试用例

input output
9     2
16    4
343   3
2687  1
59049 10

计分:这是。以字节为单位的最短答案将获胜。


2
注意:此挑战在某些高尔夫语言中可能并不重要,但对于某些主流语言以及2018年6月的QBasic语言而言却并非如此。
暴民埃里克(Erik the Outgolfer)

我们可以输出True而不是1吗?或者,使用浮点数而不是整数?
乔金

1
@JoKing是的,是的。
Leaky Nun

@EriktheOutgolfer 挑战接受了:D
DLosc

Answers:



5

Python 3,49个字节

f=lambda n,x=2:n%x and f(n,x+1)or n/x<2or-~f(n/x)

在线尝试!

输出True而不是1(OP允许)。递归函数,它反复查找最低因子,然后以下一个最低幂再次调用该函数,直到达到1。这是对上一个问题的回答的扩展。





3

dc50 41字节

1si[dli1+dsi%0<X]dsXx[dli/dli<Y]sYdli<Yzp

在线尝试!

从堆栈顶部获取输入(在TIO中,将输入放在标头中以在执行之前将其加载到堆栈上)。输出到标准输出。

说明

使用的寄存器:

i:当前的试验除数,X正在运行。后来,我们找到了除数。

X:宏dli1+dsi%0<X,其作用是“递增i,然后使用堆栈上的值(将是原始输入)检查模数。如果它不为零,请重复”。

Y:宏dli/dli<Y,其作用是“将当前栈顶的副本添加到栈中,然后除以i。重复直到i达到。”

完整程序:

1si                 Initialize i
[dli1+dsi%0<X]dsXx  Define and run X
[dli/dli<Y]sY       Define Y
dli<Y               Run Y, but only if needed (if the input wasn't just i)
z                   The stack is i^n, i^(n-1), ... ,i, so print the stack depth

我找到了一个更好的解决方案!正在编辑...
Sophia Lechner,

3

,86字节

(%d@)\$*,c'$,io>Av"[""mN*c?*m1*mp*m%*s1"$pN1p:~+p1p%%Np?%~:=/NNp+?1?-%N1?%=p%'$i?w1'%>

万岁,比Java长!

在线尝试!

我特别喜欢使用的返回值的技巧sscanf。通常,返回值将被丢弃,但是在这里它将始终为1,因为我们总是读取单个数字作为输入。我们可以通过将其返回值分配给变量来利用这一点1,从而节省2个字节,否则需要1显式地将其分配为1 个字节。

(%d@)

\$*,c'$,io>  ( setup - assign $ to "%d", * to a number, o to stdout )
Av"[""mN*    ( set " to input and allocate space for N for int conversion )
c?*          ( calloc ?, starting it at zero - this will be the output )
m1*          ( allocate variable "1", which gets the value 1 eventually )
mp*m%*       ( p is the prime, % will be used to store N mod p )

s1"$pN       ( scan " into N with $ as format; also assigns 1 to 1 )

1p:~         ( begin loop, starting p at 1 )
  +p1p       ( increment p )
  %%Np       ( set % to N mod p )
?%~          ( repeat if the result is nonzero, so that we reach the factor )

:=           ( another loop to repeatedly divide N by p )
  /NNp       ( divide N by p in-place )
  +?1?       ( increment the counter )
  -%N1       ( reuse % as a temp variable to store N-1 )
?%=          ( repeat while N-1 is not 0 -- i.e. break when N = 1 )

p%'$i?       ( sprintf ? into ', reusing the input format string )
w1'%>        ( write to stdout )








2

空格,141个字节

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_number][T    T   T   _Retrieve][S S S T  N
_Push_1][N
S S N
_Create_Label_LOOP_1][S S S T   N
_Push_1][T  S S S _Add][S N
S _Duplicate][S T   S S T   S N
_Copy_2nd_input][S N
T   _Swap_top_two][T    S T T   _Modulo][N
T   S S N
_If_0_Jump_to_Label_BREAK_1][N
S N
N
_Jump_to_Label_LOOP_1][N
S S S N
_Create_Label_BREAK_1][S S S N
_Push_0][S T    S S T   S N
_Copy_2nd_input][N
S S T   N
_Create_Label_LOOP_2][S N
S _Duplicate_input][S S S T N
_Push_1][T  S S T   _Subtract][N
T   S S S N
_If_0_Jump_to_Label_BREAK_2][S N
T   _Swap_top_two][S S S T  N
_Push_1][T  S S S _Add][S N
T   _Swap_top_two][S T  S S T   S N
Copy_2nd_factor][T  S T S _Integer_divide][N
S N
T   N
_Jump_to_Label_LOOP_2][N
S S S S N
_Create_Label_BREAK_2][S N
N
_Discard_top][T N
S T _Print_as_number]

字母S(空格),T(制表符)和N(换行符)仅作为突出显示而添加。
[..._some_action]仅作为说明添加。

在线尝试(仅使用空格,制表符和换行符)。

伪代码中的解释:

Integer n = STDIN as input
Integer f = 1
Start LOOP_1:
  f = f + 1
  if(n modulo-f == 0)
    Call function BREAK_1
  Go to next iteration of LOOP_1

function BREAK_1:
  Integer r = 0
  Start LOOP_2:
    if(n == 1)
      Call function BREAK_2
    r = r + 1
    n = n integer-divided by f
    Go to next iteration of LOOP_2

function BREAK_2:
  Print r as number to STDOUT
  Program stops with an error: Exit not defined

示例运行: input = 9

Command    Explanation                    Stack           Heap    STDIN   STDOUT   STDERR

SSSN       Push 0                         [0]
SNS        Duplicate top (0)              [0,0]
TNTT       Read STDIN as number           [0]             {0:9}   9
TTT        Retrieve                       [9]             {0:9}
SSSTN      Push 1                         [9,1]           {0:9}
NSSN       Create Label_LOOP_1            [9,1]           {0:9}
 SSSTN     Push 1                         [9,1,1]         {0:9}
 TSSS      Add top two (1+1)              [9,2]           {0:9}
 SNS       Duplicate top (2)              [9,2,2]         {0:9}
 STSSTSN   Copy 2nd from top              [9,2,2,9]       {0:9}
 SNT       Swap top two                   [9,2,9,2]       {0:9}
 TSTT      Modulo top two (9%2)           [9,2,1]         {0:9}
 NTSSN     If 0: Jump to Label_BREAK_1    [9,2]           {0:9}
 NSNN      Jump to Label_LOOP_1           [9,2]           {0:9}

 SSSTN     Push 1                         [9,2,1]         {0:9}
 TSSS      Add top two (2+1)              [9,3]           {0:9}
 SNS       Duplicate top (3)              [9,3,3]         {0:9}
 STSSTSN   Copy 2nd                       [9,3,3,9]       {0:9}
 SNT       Swap top two                   [9,3,9,3]       {0:9}
 TSTT      Modulo top two (9%3)           [9,3,0]         {0:9}
 NTSSN     If 0: Jump to Label_BREAK_1    [9,3]           {0:9}
NSSSN      Create Label_BREAK_1           [9,3]           {0:9}
SSSN       Push 0                         [9,3,0]         {0:9}
STSSTSN    Copy 2nd from top              [9,3,0,9]       {0:9}
NSSTN      Create Label_LOOP_2            [9,3,0,9]       {0:9}
 SNS       Duplicate top (9)              [9,3,0,9,9]     {0:9}
 SSSTN     Push 1                         [9,3,0,9,9,1]   {0:9}
 TSST      Subtract top two (9-1)         [9,3,0,9,8]     {0:9}
 NTSSSN    If 0: Jump to Label_BREAK_2    [9,3,0,9]       {0:9}
 SNT       Swap top two                   [9,3,9,0]       {0:9}
 SSSTN     Push 1                         [9,3,9,0,1]     {0:9}
 TSSS      Add top two (0+1)              [9,3,9,1]       {0:9}
 SNT       Swap top two                   [9,3,1,9]       {0:9}
 STSSTSN   Copy 2nd from top              [9,3,1,9,3]     {0:9}
 TSTS      Integer-divide top two (9/3)   [9,3,1,3]       {0:9}
 NSNTN     Jump to Label_LOOP_2           [9,3,1,3]       {0:9}

 SNS       Duplicate top (3)              [9,3,1,3,3]     {0:9}
 SSSTN     Push 1                         [9,3,1,3,3,1]   {0:9}
 TSST      Subtract top two (3-1)         [9,3,1,3,2]     {0:9}
 NTSSSN    If 0: Jump to Label_BREAK_2    [9,3,1,3]       {0:9}
 SNT       Swap top two                   [9,3,3,1]       {0:9}
 SSSTN     Push 1                         [9,3,3,1,1]     {0:9}
 TSSS      Add top two (1+1)              [9,3,3,2]       {0:9}
 SNT       Swap top two                   [9,3,2,3]       {0:9}
 STSSTSN   Copy 2nd from top              [9,3,2,3,3]     {0:9}
 TSTS      Integer-divide top two (3/3)   [9,3,2,1]       {0:9}
 NSNTN     Jump to Label_LOOP_2           [9,3,2,1]       {0:9}

 SNS       Duplicate top (1)              [9,3,2,1,1]     {0:9}
 SSSTN     Push 1                         [9,3,2,1,1,1]   {0:9}
 TSST      Subtract top two (1-1)         [9,3,2,1,0]     {0:9}
 NTSSSN    If 0: Jump to Label_BREAK_2    [9,3,2,1]       {0:9}
NSSSSN     Create Label_BREAK_2           [9,3,2,1]       {0:9}
 SNN       Discard top                    [9,3,2]         {0:9}
 TNST      Print as integer               [9,3]           {0:9}           2
                                                                                    error

程序因错误而停止:未找到出口。








1

Cjam,5个字节

rimf,

在线尝试!

说明:

ri      take the input and convert it to an int
  mf    factors the input
    ,   take the length of the list

内置功能很棒!


默认情况下,提交的内容必须是程序或函数,我们不将其视为函数。这两个rimf,(全部程序)和{mf,}(功能)将是有效的。
丹尼斯

@丹尼斯是的,我想我对此感到困惑。我之前也看过允许的stardard io,想知道我应该提交什么...我实际上想在meta上问一个问题。但是您确认了,所以谢谢!
Chromium

1

QBasic,51个字节

INPUT x
p=2
WHILE x/p>x\p
p=p+1
WEND
?LOG(x)/LOG(p)

使用与“恢复素数”解决方案相同的算法来找到底数,然后使用对数规则来获取指数:ØGpñ=ñØGp




0

Perl 6,36个字节

{round .log/log (2..*).first: $_%%*}

寻找第一个因数(2..*).first: $_%%*,然后从中计算出近似值(对数无法准确得出)并进行四舍五入。

在线尝试!





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.