プライム修正Z階乗
上記の用語を1つずつ説明させてください...
\と呼びます$\text{Z-Factorial}(n)\$正の整数の\$n\$、\$n!\$(すなわち\$n\$階乗)末尾のゼロなし。だから、\$\text{Z-Factorial}(30)\$は\$26525285981219105863630848\$なぜなら\$30!=265252859812191058636308480000000\$
我々は呼ぶModified Z-Factorial
の\$n\$、\$\text{Z-Factorial}(n) \mod n\$。
だから、Modified Z-Factorial
の\$30\$、は\$\text{Z-Factorial}(30) \mod 30\$これは\$26525285981219105863630848 \mod 30 = 18\$
私たちはそれらに興味があります\$n\$そのためのModified Z-Factorial of n
素数
例
数\$545\$はPMZなので\$\text{Z-Factorial}(545) \mod 545 = 109\$ 素数です
これが\の最初の値のリストです$n\$ その Prime Modified Z-Factorial (PMZ)
5,15,35,85,545,755,815,1135,1165,1355,1535,1585,1745,1895,1985,2005,2195,2495,2525,2545,2615,2705,2825,2855,3035,3085,3155,3205,3265,3545,3595,3695,3985,4135,4315,4385,4415,4685,4705,4985,5105,5465,5965,6085,6155,6185,6385,6415,6595...
仕事
上記のリストは続き、あなたの仕事は\を見つけることです$k\$th PMZ
入力
正の整数\$k\$
出力
\$kth\$ PMZ
テストケース
ここにいくつかの1インデックスのテストケースがあります。
混乱を避けるために、回答に使用するインデックスシステムを明記してください。
ソリューションは、言語のネイティブ整数サイズの範囲内でのみ機能する必要があります。
input -> output
1 5
10 1355
21 2615
42 5465
55 7265
100 15935
500 84815
これはコードゴルフなので、バイト単位で最も低いスコアが優先されます。
回答
05AB1E、16バイト
[N!0ÜN%pi®>©¹Q#N
入力は1ベースのkです。
k番目のPMZを出力します。
説明:
[N!0ÜN%pi®>©¹Q#N
[ Start infinite loop
N! Factorial of the index
0Ü Remove trailing zeros
N% Mod index
p Is prime?
i If it is:
®>© Increment the value stored in register c (initially -1)
¹Q Is the value equals the input?
#N If it does, push the index (which is the PMZ) and break
オンラインでお試しください!
ゼリー、 13の 11バイト
!Dt0Ḍ%⁸Ẓµ#Ṫ
結果をSTDOUTに出力するSTDINからの完全なプログラム読み取り。
オンラインでお試しください!
どうやって?
!Dt0Ḍ%⁸Ẓµ#Ṫ - Main Link: no arguments
# - set n=0 (implicit left arg) and increment getting the first
(implicit input) values of n which are truthy under:
µ - the monadic chain (f(n)):
! - factorial -> n!
D - convert from integer to decimal digits
t0 - trim zeros
Ḍ - convert from decimal digits to integer
⁸ - chain's left argument, n
% - modulo
Ẓ - is prime?
Ṫ - tail
- implicit print
Add ++、58バイト
D,f,@,Rb*BDBGbUdb*!!*BFJiA%P
x:?
Wx,`y,+1,`z,$f>y,`x,-z
Oy
オンラインでお試しください!
TIOで\ $ k \ ge 30 \ $がタイムアウトしました
使い方
D,f,@, ; Define a function, f, taking 1 argument, n
; Example: STACK = [30]
Rb* ; Factorial STACK = [265252859812191058636308480000000]
BD ; Convert to digits STACK = [2 6 5 ... 0 0 0]
BGbU ; Group adjacents STACK = [[2] [6] [5] ... [8] [4] [8] [0 0 0 0 0 0 0]]
db*!! ; If last is all 0s
*BF ; remove it STACK = [[2] [6] [5] ... [8] [4] [8]]
Ji ; Join to make integer STACK = [26525285981219105863630848]
A% ; Mod n STACK = [18]
P ; Is prime? STACK = [0]
; Return top value 0
x:? ; Set x to the input
Wx, ; While x > 0
`y,+1, ; y = y + 1
`z,$f>y, ; z = f(y)
`x,-z ; x = x - z
; We count up with y
; If y is PMZ, set z to 1 else 0
; Subtract z from x, to get x PMZs
Oy ; Output y
Japt、13バイト
0-インデックス付き。以下のためにのみ、実際には、作品0
&1
かつてのように、私たちが終わって行く21!
我々にはJavaScriptの超過MAX_SAFE_INTEGER
。
ÈÊsÔsÔuX j}iU
それを試してみてください
ÈÊsÔsÔuX j}iU :Implicit input of integer U
È :Function taking an integer X as argument
Ê : Factorial
s : String representation
Ô : Reverse
sÔ : Repeat (There has to be a shorter way to remove the trailing 0s!)
uX : Modulo X
j : Is prime?
} :End function
iU :Pass all integers through that function, returning the Uth one that returns true
R、99の93バイト
編集:Giuseppeのおかげで-6バイト(および任意精度バージョンから-4バイト)
k=scan();while(k){F=F+1;z=gamma(F+1);while(!z%%5)z=z/10;x=z%%F;k=k-(x==2|all(x%%(2:x^.5)))};F
オンラインでお試しください!
説明の手順に従って、簡単なアプローチを使用します。残念ながら、factorial(21)でRの数値精度の限界を超えているため、k> 2の場合は失敗します。
任意精度のバージョン(小さいkに限定されませんが、ゴルフの競争力は低くなります)は次のとおりです
。R + gmp、115バイト
ハスク、11バイト
!foṗS%ȯ↔↔ΠN
オンラインでお試しください!
説明
!foṗS%ȯ↔↔ΠN
f N filter list of natural numbers by:
Π take factorial
↔↔ reverse twice, remove trailing zeros
S% mod itself
ṗ is prime?
! get element at index n
JavaScript(Node.js)、 89 ... 7977 バイト
n=>(g=y=>y%10n?(p=k=>y%--k?p(k):~-k||--n?g(x*=++i):i)(y%=i):g(y/10n))(x=i=2n)
オンラインでお試しください!
Pythonの3、145の 140 138 129バイト
def f(n,r=0):
c=d=2
while r<n:
c+=1;d*=c
while 1>d%10:d//=10
i=d%c;r+=i==2or i and min(i%j for j in range(2,i))
return c
オンラインでお試しください!
パイソン2、126の125バイト
def f(n,r=0):
c=d=2
while r<n:
c+=1;d*=c
while d%10<1:d/=10
i=d%c
r+=i==2or i and min(i%j for j in range(2,i))
print c
オンラインでお試しください!
説明:現在の階乗が10で割り切れる限り、10で除算し続けてから、階乗のモジュロ電流数で素数性を確認します。
おかげcairdはcoinheringaahing -20バイトのためにとドミニクバンエッセンのため-9バイトを!
Haskell、129111バイト
g n
|n`mod`10>0=n
|0<1=g$div n 10 f=(!!)[n|n<-[1..],let p=mod(g$product[1..n])n,[x|x<-[2..p],mod p x<1]==[p]]
オンラインでお試しください!
g
0
番号からsを削除します。
f
とりk
:無限リスト内包から番目の要素を
[x|x<-[2..p],mod p x==0]==[p]
あるprime
条件(の約数のリストを比較p
し、ちょうどPのリスト)。
そして、p
はmod(g$foldr(*)1[1..n])n
通過しg
た階乗のモジュロです。
ユーザーのおかげで18を節約