多くの人がこれを持ちたいと思っているようですので、今ではこの挑戦の続編です!
定義:素数はp nの形式で表現できる自然数です。pは素数で、nは自然数です。
タスク:プライムパワーp n > 1の場合、パワーnを返します。
テストケース:
input output
9 2
16 4
343 3
2687 1
59049 10
得点:これはcode-golfです。バイト単位の最短回答が優先されます。
多くの人がこれを持ちたいと思っているようですので、今ではこの挑戦の続編です!
定義:素数はp nの形式で表現できる自然数です。pは素数で、nは自然数です。
タスク:プライムパワーp n > 1の場合、パワーnを返します。
テストケース:
input output
9 2
16 4
343 3
2687 1
59049 10
得点:これはcode-golfです。バイト単位の最短回答が優先されます。
回答:
f=lambda n,x=2:n%x and f(n,x+1)or n/x<2or-~f(n/x)
True
1の代わりに出力します(OPで許可されています)。繰り返し最低の要因を見つけ、1に達するまで次の最低の電力で関数を再度呼び出す再帰関数。これは、前の質問に対する私の答えの延長です。
f=lambda n,i=2:i/n or(n%i<1)+f(n,i+1)
要因をカウントします。どうやら2015年に同じゴルフを書いたようです。
再帰的でないものをわずかに打ち負かす
Python 2、38バイト
lambda n:sum(n%i<1for i in range(1,n))
1si[dli1+dsi%0<X]dsXx[dli/dli<Y]sYdli<Yzp
スタックの最上部から入力を取得します(TIOでは、入力をヘッダーに入れて、実行前にスタックにロードします)。stdoutへの出力。
使用されるレジスタ:
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つの数値を読み取るためです。これを利用するには、戻り値を変数1
に割り当て1
、1に明示的に割り当てるために必要な2バイトを保存します。
(%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
Attacheをオンラインでお試しください! オンラインでMathematicaをお試しください!
Nが持つ素因数の数を計算するための組み込み関数です。
以来、N = PのK、Ω(N)=Ω(PのK)= K、所望の結果。
sum(x|1)
ほぼ常に短いlength(x)
Yfz
% Implicit input: 59049
Yf % Factorize input [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
z % Number of non-zero elements: 10
% Implicit output
[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
プログラムがエラーで停止します:出口が見つかりません。
rimf,
説明:
ri take the input and convert it to an int
mf factors the input
, take the length of the list
組み込みは素晴らしいです!
{round .log/log (2..*).first: $_%%*}
最初の要因を探し(2..*).first: $_%%*
、そこからおおよその値を計算し(ログは正確になりません)、丸めます。