Fatoriais Z modificados principais
Deixe-me explicar um por um os termos acima ...
Vamos ligar para \$\text{Z-Factorial}(n)\$de um inteiro positivo \$n\$, \$n!\$(ie \$n\$fatorial) sem zeros à direita. Então, \$\text{Z-Factorial}(30)\$é \$26525285981219105863630848\$porque \$30!=265252859812191058636308480000000\$
Ligaremos Modified Z-Factorial
de \$n\$, o \$\text{Z-Factorial}(n) \mod n\$.
Então, Modified Z-Factorial
de \$30\$, é \$\text{Z-Factorial}(30) \mod 30\$que é \$26525285981219105863630848 \mod 30 = 18\$
Estamos interessados neles \$n\$para o qual Modified Z-Factorial of n
é um número primo
Exemplo
O número \$545\$é PMZ porque \$\text{Z-Factorial}(545) \mod 545 = 109\$ qual é o principal
Aqui está uma lista dos primeiros valores de \$n\$ aquele produto 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...
Tarefa
A lista acima continua e sua tarefa é encontrar o \$k\$th PMZ
Entrada
Um inteiro positivo \$k\$
Resultado
O \$kth\$ PMZ
Casos de teste
aqui estão alguns casos de teste indexados em 1 .
Indique qual sistema de indexação você usa em sua resposta para evitar confusão.
Suas soluções só precisam funcionar dentro dos limites do tamanho inteiro nativo do seu idioma.
input -> output
1 5
10 1355
21 2615
42 5465
55 7265
100 15935
500 84815
Este é um código de golfe , então a pontuação mais baixa em bytes vence.
Respostas
05AB1E , 16 bytes
[N!0ÜN%pi®>©¹Q#N
A entrada é baseada em 1 k.
Produz o k-th PMZ.
Explicação:
[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
Experimente online!
Jelly , 13 11 bytes
!Dt0Ḍ%⁸Ẓµ#Ṫ
Um programa completo de leitura de STDIN que imprime o resultado em STDOUT.
Experimente online!
Quão?
!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
Adicione ++ , 58 bytes
D,f,@,Rb*BDBGbUdb*!!*BFJiA%P
x:?
Wx,`y,+1,`z,$f>y,`x,-z
Oy
Experimente online!
Tempo esgotado para \ $ k \ ge 30 \ $ em TIO
Como funciona
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 bytes
Indexado a 0. Só funciona, na prática, para 0
& 1
, uma vez que ultrapassamos o 21!
valor do JavaScript MAX_SAFE_INTEGER
.
ÈÊsÔsÔuX j}iU
Tente
ÈÊ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 bytes
Editar: -6 bytes (e -4 bytes da versão de precisão arbitrária) graças a Giuseppe
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
Experimente online!
Usa a abordagem direta, seguindo as etapas da explicação. Infelizmente sai dos limites da precisão numérica de R no fatorial (21), então falha para qualquer k> 2.
Uma versão de precisão arbitrária (que não está limitada a k pequeno, mas é menos competitiva no golfe) é:
R + gmp, 115 bytes
Husk , 11 bytes
!foṗS%ȯ↔↔ΠN
Experimente online!
Explicação
!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 ... 79 77 bytes
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)
Experimente online!
Python 3 , 145 140 138 129 bytes
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
Experimente online!
Python 2 , 126 125 bytes
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
Experimente online!
Explicação: Continue dividindo por 10, desde que o fatorial atual seja divisível por 10 e, em seguida, verifique o número atual do módulo fatorial para primalidade.
Graças a caird coinheringaahing por -20 bytes e Dominic van Essen por -9 bytes!
Haskell , 129 111 bytes
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]]
Experimente online!
g
remove 0
s do número.
f
pega o k
elemento de uma compreensão de lista infinita onde:
[x|x<-[2..p],mod p x==0]==[p]
é prime
condição (compara lista de divisores de p
e uma lista de apenas p).
E p
é mod(g$foldr(*)1[1..n])n
o módulo do fatorial passado g
.
Salvo 18 graças ao usuário