n=input()
s=p=i=1
while i<n:p*=i*i;i+=1;s+=p%i
print~-s
Try it online!
A programification of the below. The Python 3 equivalent is 62 bytes.
Note that although xnor's recursive answer outgolfs this at 45 bytes, it cannot handle an input of 999
or greater, due to Python's recursion limit: Try it online! - this limit can be increased, though: Try it online!
This 55 byte solution can go well beyond that. For example, all inputs up to 3000
: Try it online!
lambda n,p=1:sum([(p:=p*i*i)%-~i for i in range(1,n)])
Try it online! / Attempt This Online!
A lambdafication of the below, taking advantage of the :=
operator.
Although xnor's recursive answer outgolfs this at 46 bytes when ported to Python 3, it still can't handle an input of 999
or greater: Try it online! - this limit can be increased: Attempt This Online!
def P(n):
s=p=i=1
while i<n:p*=i*i;i+=1;s+=p%i
return~-s
Try it online! - Python 2
Try it online! - Python 3
Based on miles's answer to "Is it a super-prime?".
This exploits Wilson's theorem and Python's arbitrary precision integers to count primes. At each iteration of the loop, with \$i\in[2,n]\$, it calculates \${(i-1)^2}!\pmod i\$. This \$\equiv 1\$ when \$i\$ is prime and \$\equiv 0\$ otherwise. The squaring is done to compensate for the fact that \$4\$ is the only exception, with \$(4-1)!\equiv 2\pmod 4\$, but \$(4-1)^2!\equiv 0\pmod 4\$. Note that squaring also changes the modulus from \$-1\$ to \$1\$ for prime \$i\$.
If not for the exception at i=4
, we could have done
while i<n:p*=i;i+=1;s+=-p%i
For 1 byte less, because Python returns a positive result when taking a negative number modulo a positive number.