似乎很多人都想拥有此功能,因此现在是此挑战的续集!
定义:素数幂是自然数,可以以p n的形式表示,其中p是素数,n是自然数。
任务:给定素数幂p n > 1,返回幂n。
测试用例:
input output
9 2
16 4
343 3
2687 1
59049 10
计分:这是代码高尔夫球。以字节为单位的最短答案将获胜。
似乎很多人都想拥有此功能,因此现在是此挑战的续集!
定义:素数幂是自然数,可以以p n的形式表示,其中p是素数,n是自然数。
任务:给定素数幂p n > 1,返回幂n。
测试用例:
input output
9 2
16 4
343 3
2687 1
59049 10
计分:这是代码高尔夫球。以字节为单位的最短答案将获胜。
Answers:
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
(%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 )
PrimeOmega
只需一个内置函数即可计算N具有的素数。
由于N = p k,所以Ω(N)=Ω(p k)= k,因此是理想的结果。
[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
程序因错误而停止:未找到出口。
INPUT x
p=2
WHILE x/p>x\p
p=p+1
WEND
?LOG(x)/LOG(p)
使用与“恢复素数”解决方案相同的算法来找到底数,然后使用对数规则来获取指数:。