{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# first I am going to import the needed libraries. Only numpy in this case since there is nothing to plot\n", "import numpy as np\n", "# here I write a function that takes an integer as input and returns an array with all the prime factors\n", "def primFact(n):\n", "# first I create the empty array where to store the prome factors\n", " result=np.array([])\n", "# then I create an array with all the number I want to check. \n", "# This is not very efficient since in principles I need to check only primes.\n", " numTest=np.arange(2,n,1)\n", "# now I cycle over al the numbers I am testing \n", " for nt in numTest:\n", "# I use a while loop here since the same prime number may appear multiple times.\n", "# the while loop runs as long as it finds that the remainder of the division is 0.\n", " while n%nt==0:\n", "# If the remainder of the division is zero, the number tested is added to the array of results\n", " result=np.append(result,nt)\n", "# the input number is now divided by the found divisor\n", " n=n/nt\n", "# when all numbers have been tested, the result is returned\n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter the first integer number 756\n", "The number you entered, rounded to integer, is: 756\n", "The prime decomposition of your input number is: [2. 2. 3. 3. 3. 7.]\n", "Please enter the second integer number 5040\n", "The number you entered, rounded to integer, is: 5040\n", "The prime decomposition of your input number is: [2. 2. 2. 2. 3. 3. 5. 7.]\n" ] } ], "source": [ "# This line requires the input number, turns it into a float, and then into an integer, \n", "# just in case it was entered as a real\n", "num1=int(float(input('Please enter the first integer number ')))\n", "# Then the number is printed back to ensure accuracy\n", "print('The number you entered, rounded to integer, is:',num1)\n", "# The routine above is called to obtain the factorization\n", "pf1=primFact(num1)\n", "# The result is printed out\n", "print('The prime decomposition of your input number is:',pf1)\n", "# SAme as above for the second number\n", "num2=int(float(input('Please enter the second integer number ')))\n", "# more of the same\n", "print('The number you entered, rounded to integer, is:',num2)\n", "# more of the same\n", "pf2=primFact(num2)\n", "# printing out the result\n", "print('The prime decomposition of your input number is:',pf2)\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The maximum common divisor is: 252.0\n" ] } ], "source": [ "# sets the initial guess of the MCD as 1. This is the smallest possible value that an MCD can be\n", "mcd=1\n", "# now the code cycles over all teh prime factors of the first number (could have been the second)\n", "for n in pf1:\n", "# this is a command that finds all locations where n (a factor of pf1) appears in pf2\n", " jj=np.where(pf2==n)\n", "# if it appears, then the MCD is multiplied by n\n", " if jj[0].size>0:\n", " mcd=mcd*n\n", "# that number is now removed from pf2 since it has been used.\n", " pf2[jj[0][0]]=-1\n", "# the final result is printed out\n", "print('The maximum common divisor is:',mcd)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }