Permalink
Browse files

P381: Added Mathematica solution.

  • Loading branch information...
1 parent 31a5791 commit 3e071e55434fadd67becdc54a0d87edda6c3df67 @nayuki committed Apr 28, 2012
Showing with 15 additions and 0 deletions.
  1. +1 −0 Answers.txt
  2. +14 −0 p381.mat.txt
View
@@ -121,3 +121,4 @@ Problem 211: 1922364685
Problem 214: 1677366278943
Problem 250: 1425480602091519
Problem 323: 6.3551758451
+Problem 381: 139602943319822
View
@@ -0,0 +1,14 @@
+(*
+ * Solution to Project Euler problem 381
+ * By Nayuki Minase
+ *)
+
+
+(* Use this in Wolfram Mathematica *)
+
+(* By Wilson's theorem, (p-1)! = -1 mod p if and only if p is prime. *)
+S[p_] := Mod[Sum[-1 * Product[PowerMod[-j, -1, p], {j, 1, k - 1}], {k, 1, 5}], p]
+
+sum = 4; (* Special cased for 5 *)
+For[i = 4, Prime[i] <= 10^8, i++, sum += S[Prime[i]]]
+sum

0 comments on commit 3e071e5

Please sign in to comment.