{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# L'arithmétique" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quotient et reste\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "7\n" ] } ], "source": [ "import math\n", "\n", "dividende = 47\n", "diviseur = 8\n", "quotient = dividende // diviseur\n", "reste = dividende % diviseur\n", "print(quotient)\n", "print(reste)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Liste des diviseurs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def diviseurs(nombre):\n", " \"retourne la liste des diviseurs.\"\n", " limite=int(math.sqrt(nombre)+1)\n", " L_couples = [(x,nombre//x) for x in range(1,limite) if nombre%x==0]\n", " #print(L_couples)\n", " ensemble_diviseurs=set() # ensemble des diviseurs\n", " for (a,b) in L_couples :\n", " ensemble_diviseurs.add(a)\n", " ensemble_diviseurs.add(b)\n", " L_diviseurs=sorted(ensemble_diviseurs) #conversion en liste ordonnées.\n", " return L_diviseurs\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 6, 9, 18]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diviseurs(18)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tester si un nombre est premier" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def est_premier(nombre) :\n", " \"Un nombre est premier s'il a deux diviseurs\"\n", " if len(diviseurs(nombre))==2 :\n", " return True\n", " else :\n", " return False" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "est_premier(17)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Décomposition en facteurs premiers" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def decomposition(nombre):\n", " \"retourne la liste des facteurs premiers\"\n", " L=diviseurs(nombre)\n", " if len(L)==1 :\n", " return [1]\n", " Decomposition=[]\n", " while len(L)>1 :\n", " L.pop(0)\n", " p=L[0]\n", " Decomposition.append(p)\n", " L=[k//p for k in L if k%p==0]\n", " return Decomposition\n", " " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 3, 3607, 3803]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "decomposition(123456789)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }