Given a positive integer n, compute the nth Wilson number W(n) where

Wilson number formula

and e = 1 if n has a primitive root modulo n, otherwise e = -1. In other words, n has a primitive root if there does not exist an integer x where 1 < x < n-1 and x2 = 1 mod n.

  • This is so create the shortest code for a function or program that computes the nth Wilson number for an input integer n > 0.
  • You may use either 1-based or 0-based indexing. You may also choose to output the first n Wilson numbers.
  • This is the OEIS sequence A157249.

Test Cases

n  W(n)
1  2
2  1
3  1
4  1
5  5
6  1
7  103
8  13
9  249
10 19
11 329891
12 32
13 36846277
14 1379
15 59793
16 126689
17 1230752346353
18 4727
19 336967037143579
20 436486
21 2252263619
22 56815333
23 48869596859895986087
24 1549256
25 1654529071288638505
share|improve this question
    
Also, Oeis divides by n afterwards – H.PWiz 14 hours ago
    
@EriktheOutgolfer I added what is meant by having a primitive root. – miles 14 hours ago
1  
Are we supposed to divide by n? – Leaky Nun 14 hours ago
    
As far as I'm aware, if k = 1 and e = -1, the result of the product would be 0. (sorry asking many questions but I need clarifications for my answer :p) – Erik the Outgolfer 14 hours ago
    
1 has a primitive root according to the oeis – H.PWiz 14 hours ago

10 Answers 10

Jelly, 8 bytes

Rg=1TP‘:

Try it online!

You don't really have to compute e since you need to divide anyway.

share|improve this answer
    
gRỊT saves a byte. – Dennis 8 hours ago

Husk, 11 bytes

S÷ȯ→Π§foε⌋ḣ

Try it online!

Explanation

          ḣ   Range from 1 to input
     §foε⌋    Keep only those whose gcd with the input is 1
    Π         Product
  ȯ→          Plus 1
S÷            Integer division with input
share|improve this answer
    
Please add explanation? I think you've got a nifty algo there... – Erik the Outgolfer 13 hours ago

Mathematica, 91 bytes

If[(k=#)==1,2,(Times@@Select[Range@k,CoprimeQ[k,#]&]+If[IntegerQ@PrimitiveRoot@#,1,-1])/#]&
share|improve this answer
    
@BillSteihn please do not directly edit others' answers (relevant meta discussion). If you have a golfing suggestion, please leave a comment instead! – JungHwan Min 12 hours ago
    
@JungHwanMin Yes, I noticed that edit! thanks for helping new users with the rules – Jenny_mathy 12 hours ago

Pyth, 11 bytes

/h*Ff>2iTQS

Try it here!


How?

  • /h*Ff>2iTQS - Full program.

  • S - Generate the inclusive range [1, input]

  • f - Filter-keep those:

    • iTQ - whose GCD with the input.

    • >2 - Is less than two (can be replaced by either of the following: q1, !t)

  • *F - Apply multiplication repeatedly. In other words, the product of the list.

  • h - Increment the product by 1.

  • / - Floor division with the input.

TL;DR: Get all the coprimes to the input in the range [1, input], get their product, increment it and divide it by the input.

share|improve this answer

Python 2, 62 bytes

n=input();k=r=1
exec'r*=k/n*k%n!=1or k/n;k+=1;'*n*n
print-~r/n

Try it online!

share|improve this answer

J, 33 bytes

3 :'<.%&y>:*/(#~1&=@(+.&y))1+i.y'

This one is more of a request to see an improvement than anything else. I tried a tacit solution first, but it was longer than this.

explanation

This is fairly straightforward translation of Mr. Xcoder's solution into J.

Try it online!

share|improve this answer

R, 82 bytes

function(n)(prod((1:n)[g(n,1:n)<2])+1)%/%n
g=function(a,b)ifelse(o<-a%%b,g(b,o),b)

Uses integer division rather than figuring out e like many answers here, although I did work out that e=2*any((1:n)^2%%n==1%%n)-1

Try it online!

share|improve this answer

Jelly, 22 bytes

ḊṖṖ²%ċ1ȧ2’
gRỊTP+Ç÷_.Ċ

Try it online!

-1 thanks to Dennis (and Mr. Xcoder's comment).

share|improve this answer
    
kinda borrowed the g€=1T trick from leaky's answer – Erik the Outgolfer 13 hours ago
    
22 bytes – Mr. Xcoder 2 hours ago
    
@Mr.Xcoder and the credit goes there (I assume that's where you took it from) – Erik the Outgolfer 1 hour ago
    
Yes, obviously. Strange built-in BTW :P – Mr. Xcoder 1 hour ago
    
@Mr.Xcoder Yeah the builtin is kinda strange...but it has proven useful. – Erik the Outgolfer 1 hour ago

05AB1E, 8 bytes

Lʒ¿}P>I÷

Try it online!

share|improve this answer

f=(n,i=1,p=1,g=(a,b)=>b?g(b,a%b):a)=>i<n?f(n,i+1,g(n,i)-1?p:p*i):-~p/n|0
<input type=number min=1 oninput=o.textContent=f(+this.value)><pre id=o>

Integer division strikes again.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.