Factoriales Z Prime Modificados
Déjame explicarte uno por uno los términos anteriores ...
Llamaremos \$\text{Z-Factorial}(n)\$de un entero positivo \$n\$, \$n!\$(es decir, \$n\$factorial) sin ceros finales. Entonces, \$\text{Z-Factorial}(30)\$es \$26525285981219105863630848\$porque \$30!=265252859812191058636308480000000\$
Llamaremos Modified Z-Factorial
de \$n\$, el \$\text{Z-Factorial}(n) \mod n\$.
Entonces, Modified Z-Factorial
de \$30\$, es \$\text{Z-Factorial}(30) \mod 30\$que es \$26525285981219105863630848 \mod 30 = 18\$
Estamos interesados en esos \$n\$para el que Modified Z-Factorial of n
es un número primo
Ejemplo
El número \$545\$es PMZ porque \$\text{Z-Factorial}(545) \mod 545 = 109\$ que es primordial
Aquí hay una lista de los primeros valores de \$n\$ que producen 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...
Tarea
La lista anterior continúa y su tarea es encontrar el \$k\$th PMZ
Entrada
Un entero positivo \$k\$
Salida
El \$kth\$ PMZ
Casos de prueba
aquí hay algunos casos de prueba indexados 1 .
Indique qué sistema de indexación utiliza en su respuesta para evitar confusiones.
Sus soluciones solo deben funcionar dentro de los límites del tamaño entero nativo de su idioma.
input -> output
1 5
10 1355
21 2615
42 5465
55 7265
100 15935
500 84815
Esto es código de golf , por lo que gana la puntuación más baja en bytes.
Respuestas
05AB1E , 16 bytes
[N!0ÜN%pi®>©¹Q#N
La entrada se basa en 1 k.
Emite el k-ésimo PMZ.
Explicación:
[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
¡Pruébelo en línea!
Gelatina , 13 11 bytes
!Dt0Ḍ%⁸Ẓµ#Ṫ
Una lectura de programa completa de STDIN que imprime el resultado en STDOUT.
¡Pruébelo en línea!
¿Cómo?
!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
Agregar ++ , 58 bytes
D,f,@,Rb*BDBGbUdb*!!*BFJiA%P
x:?
Wx,`y,+1,`z,$f>y,`x,-z
Oy
¡Pruébelo en línea!
Tiempos de espera para \ $ k \ ge 30 \ $ en TIO
Cómo 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
Japonés , 13 bytes
0-indexado. Sólo funciona, en la práctica, para 0
y 1
ya que una vez que vayamos más 21!
excedemos JavaScript de MAX_SAFE_INTEGER
.
ÈÊsÔsÔuX j}iU
Intentalo
ÈÊ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 (y -4 bytes de la versión de precisión arbitraria) gracias 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
¡Pruébelo en línea!
Utiliza el enfoque sencillo, siguiendo los pasos de la explicación. Desafortunadamente, se sale de los límites de la precisión numérica de R en factorial (21), por lo que falla para cualquier k> 2.
Una versión de precisión arbitraria (que no se limita a k pequeña, pero es menos competitiva en el golf) es:
R + gmp, 115 bytes
Cáscara , 11 bytes
!foṗS%ȯ↔↔ΠN
¡Pruébelo en línea!
Explicació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 ... 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)
¡Pruébelo en línea!
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
¡Pruébelo en línea!
Python 2 , 126125 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
¡Pruébelo en línea!
Explicación: Siga dividiendo por 10 siempre que el factorial actual sea divisible por 10, y luego verifique el número actual del módulo factorial para ver si es primordial.
¡Gracias a caird coinheringaahing por -20 bytes y a Dominic van Essen por -9 bytes!
Haskell , 129111 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]]
¡Pruébelo en línea!
g
elimina 0
s del número.
f
toma el k
elemento de una comprensión de lista infinita donde:
[x|x<-[2..p],mod p x==0]==[p]
es prime
condición (compara la lista de divisores de p
y una lista de solo p).
Y p
es mod(g$foldr(*)1[1..n])n
el módulo de factorial traspasado g
.
18 guardados gracias al usuario