{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function, division" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def somme(A, B):\n", " C = []\n", " for i in range(4):\n", " Ai = A[i]\n", " Bi = B[i]\n", " row = [Ai[j]+Bi[j] for j in range(4)]\n", " C.append(row)\n", " return C" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "X = [[56, 39, 3, 41],\n", " [23, 78, 11, 62],\n", " [61, 26, 65, 51],\n", " [80, 98, 9, 68]]\n", "Y = [[51, 52, 53, 15],\n", " [ 1, 71, 46, 31],\n", " [99, 7, 92, 12],\n", " [15, 43, 36, 51]]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[107, 91, 56, 56], [24, 149, 57, 93], [160, 33, 157, 63], [95, 141, 45, 119]]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "somme(X, Y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On vérifie en utilisant sympy que le calcul est correct:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[107, 91, 56, 56],\n", "[ 24, 149, 57, 93],\n", "[160, 33, 157, 63],\n", "[ 95, 141, 45, 119]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sympy import Matrix\n", "Mx = Matrix(X)\n", "My = Matrix(Y)\n", "Mx + My" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def produit(A, B):\n", " C = []\n", " for i in range(4):\n", " row = []\n", " for j in range(4):\n", " row.append(sum(A[i][k]*B[k][j] for k in range(4)))\n", " C.append(row)\n", " return C" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[3807, 7465, 6514, 4176],\n", " [3270, 9477, 8051, 6057],\n", " [10337, 7666, 12245, 5102],\n", " [6089, 14105, 12024, 7814]]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "produit(X,Y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On vérifie en utilisant sympy que ce calcul est correct:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Matrix([\n", "[ 3807, 7465, 6514, 4176],\n", "[ 3270, 9477, 8051, 6057],\n", "[10337, 7666, 12245, 5102],\n", "[ 6089, 14105, 12024, 7814]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Mx * My" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 3" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import sqrt\n", "def est_premier(n):\n", " if n == 0 or n == 1:\n", " return False\n", " for i in range(2, int(sqrt(n))+1):\n", " if n % i == 0:\n", " return False\n", " return True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On vérifie que la fonction `est_premier` fonctionne bien:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, " ] } ], "source": [ "for i in range(100):\n", " if est_premier(i):\n", " print(i, end=', ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "D'abord, on remarque qu'il n'existe pas de quadruplet de nombre premiers $p$, $p+2$, $p+4$, $p+6$, car l'un d'eux doit être divisible par trois (en effet, leurs valeurs modulo 3 sont $p$, $p+2$, $p+1$). Comme 3 est le seul nombre premier divisible par trois, il faudrait que le quadruplet contienne le nombre 3. Or un tel quadruplet n'existe pas." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def triplets_nombre_premier(n):\n", " L = []\n", " p = 3\n", " while len(L) < n:\n", " if est_premier(p) and est_premier(p+6):\n", " if est_premier(p+2):\n", " L.append((p, p+2, p+6))\n", " elif est_premier(p+4):\n", " L.append((p, p+4, p+6))\n", " p += 2\n", " return L" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(5, 7, 11),\n", " (7, 11, 13),\n", " (11, 13, 17),\n", " (13, 17, 19),\n", " (17, 19, 23),\n", " (37, 41, 43),\n", " (41, 43, 47),\n", " (67, 71, 73),\n", " (97, 101, 103),\n", " (101, 103, 107)]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "triplets_nombre_premier(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 4" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def triplets_pythagore(n):\n", " L = []\n", " for c in range(1, n+1):\n", " for b in range(1, c+1):\n", " for a in range(1, b+1):\n", " if a**2+b**2 == c**2:\n", " L.append((a,b,c))\n", " return L" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(3, 4, 5),\n", " (6, 8, 10),\n", " (5, 12, 13),\n", " (9, 12, 15),\n", " (8, 15, 17),\n", " (12, 16, 20),\n", " (15, 20, 25),\n", " (7, 24, 25),\n", " (10, 24, 26),\n", " (20, 21, 29),\n", " (18, 24, 30)]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "triplets_pythagore(30)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }