From ab4840204cb466e7d0142f1c882a971904c0e4e7 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Wed, 19 Dec 2018 15:58:46 -0500 Subject: [PATCH] Solved problem 41. --- ipython/EulerProblem041.ipynb | 74 +++++++++++++++++++++++++++++-- ipython/html/EulerProblem041.html | 70 ++++++++++++++++++++++++++++- ipython/html/index.html | 8 +++- 3 files changed, 144 insertions(+), 8 deletions(-) diff --git a/ipython/EulerProblem041.ipynb b/ipython/EulerProblem041.ipynb index 042897c..20b4cf3 100644 --- a/ipython/EulerProblem041.ipynb +++ b/ipython/EulerProblem041.ipynb @@ -13,6 +13,8 @@ "collapsed": true }, "source": [ + "[https://projecteuler.net/problem=41]\n", + "\n", "We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.\n", "\n", "What is the largest n-digit pandigital prime that exists?" @@ -20,16 +22,78 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": { "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "def expmod(base, exp, m):\n", + " if exp == 0:\n", + " return 1\n", + " if (exp % 2 == 0):\n", + " return (expmod(base, exp // 2, m) ** 2 % m)\n", + " return (base * expmod(base, exp - 1, m) % m)\n", + " \n", + "def expmod_naiv(base, exp, m):\n", + " return base ** exp % m\n", + "\n", + "def fermat_test(n):\n", + " a = n - 3\n", + " return expmod(a, n, n) == a" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "assert(expmod(2, 100000, 2143) == expmod_naiv(2, 100000, 2143))" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "assert(fermat_test(2143))" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7652413\n" + ] + } + ], + "source": [ + "import itertools\n", + "\n", + "for pandigital in itertools.permutations(\"7654321\"):\n", + " p = int(\"\".join(pandigital))\n", + " if fermat_test(p):\n", + " s = p\n", + " break\n", + " \n", + "print(s)\n", + "assert(s == 7652413)" + ] } ], "metadata": { - "completion_date": "", + "completion_date": "Wed, 19 Dec 2018, 20:14", "kernelspec": { "display_name": "Python 3", "language": "python3.6", @@ -49,7 +113,9 @@ }, "tags": [ "pandigital", - "prime" + "prime", + "fermat", + "test" ] }, "nbformat": 4, diff --git a/ipython/html/EulerProblem041.html b/ipython/html/EulerProblem041.html index ac242d6..02b9178 100644 --- a/ipython/html/EulerProblem041.html +++ b/ipython/html/EulerProblem041.html @@ -11778,6 +11778,7 @@ div#notebook {
+

[https://projecteuler.net/problem=41]

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?

@@ -11785,15 +11786,80 @@ div#notebook {
-
In [ ]:
+
In [49]:
-
 
+
def expmod(base, exp, m):
+    if exp == 0:
+        return 1
+    if (exp % 2 == 0):
+        return (expmod(base, exp // 2, m) ** 2 % m)
+    return (base * expmod(base, exp - 1, m) % m)
+    
+def expmod_naiv(base, exp, m):
+    return base ** exp % m
+
+def fermat_test(n):
+    a = n - 3
+    return expmod(a, n, n) == a
 
+
+
+
In [50]:
+
+
+
assert(expmod(2, 100000, 2143) == expmod_naiv(2, 100000, 2143))
+
+
+
+
+
+
+
+
In [51]:
+
+
+
assert(fermat_test(2143))
+
+
+
+
+
+
+
+
In [54]:
+
+
+
import itertools
+
+for pandigital in itertools.permutations("7654321"):
+    p = int("".join(pandigital))
+    if fermat_test(p):
+        s = p
+        break
+        
+print(s)
+assert(s == 7652413)
+
+
+
+
+
+
+
+
+
+
7652413
+
+
+
+
+
+
diff --git a/ipython/html/index.html b/ipython/html/index.html index d1e2fc9..c1c95ca 100644 --- a/ipython/html/index.html +++ b/ipython/html/index.html @@ -645,16 +645,20 @@ - + Problem 041 - + Wed, 19 Dec 2018, 20:14 pandigital prime + fermat + + test +