1
|
{"nbformat_minor": 0, "cells": [{"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 239", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#To find the quotient and remainder \ndividend=101\ndivisor=11\nquotient=dividend/divisor #To find quotient\nremainder=dividend%divisor #To find remainder\ndividend=(divisor*quotient)+remainder\nprint \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The quotient when 101 is divided by 11 is 9 = 101 div 11 and the remainder is 2 = 101 mod 11\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 240", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To find the quotient and remainder\ndividend=-11\ndivisor=3\nquotient=dividend/divisor\nremainder=dividend%divisor\ndividend=(divisor*quotient)+remainder\nprint \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The quotient when -11 is divided by 3 is -4 = -11 div 3 and the remainder is 1 = -11 mod 3\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 246", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "#To convert binary to decimal equivalent\nbinary_num= raw_input('enter a number: ')\ndecimal = 0\nfor digit in binary_num:\n decimal = decimal*2 + int(digit)\n\nprint decimal\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter a number: 101011111\n351\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 247", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "#To convert decimal to hexadecimal\ndec= raw_input('enter a number: ')\n\nprint \"The conversion of\",dec,\"to hexadeimal is\",int(dec,16)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter a number: 2AE0B\nThe conversion of 2AE0B to hexadeimal is 175627\n"}], "metadata": {"scrolled": true, "collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 247", "cell_type": "markdown", "metadata": {}}, {"execution_count": 6, "cell_type": "code", "source": "#To compute decimal to octal\nnumbers= []\ndec=input(\"Enter a number\");\nnum=dec\nwhile dec!=0:\n \n rem=dec%8\n dec=dec/8\n numbers.append(rem)\nprint \"The decimal number\",num,\"is converted to its octal equivalent : \",\nfor i in reversed(numbers):\n print i,\n \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a number12345\nThe decimal number 12345 is converted to its octal equivalent : 3 0 0 7 1\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 248", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "#To convert Decimal to hexadecimal\nnum=[]\ndef ChangeHex(n): #function to convert\n if (n < 0):\n num.append(\"\")\n elif (n<=1):\n num.append(n)\n else: #for numbers greater than 9\n x =(n%16)\n if (x < 10):\n num.append(x) \n if (x == 10):\n num.append(\"A\")\n if (x == 11):\n num.append(\"B\")\n if (x == 12):\n num.append(\"C\")\n if (x == 13):\n num.append(\"D\")\n if (x == 14):\n num.append(\"E\")\n if (x == 15):\n num.append(\"F\")\n ChangeHex( n / 16 )\ndec_num=input(\"Enter the decimal number which is to be converted to hexadecimal\");\nChangeHex(dec_num)\nprint \"The hexadecimal equivalent of decimal\",dec_num,\"is\",\nfor i in reversed(num):\n print i,\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the decimal number which is to be converted to hexadecimal177130\nThe hexadecimal equivalent of decimal 177130 is 0 2 B 3 E A\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 249", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "#Compute Decimal to Binary\narray=[]\ndef conv(n):\n if n==0:\n print ''\n else:\n array.append(str(n%2)) #to compute remainder and append it to the result\n return conv(n/2) \ndec_num=input(\"Enter a decimal number\")\nconv(dec_num)\nprint \"The binary equivalent of decimal\",dec_num,\"is\",\nfor i in reversed(array):\n print i,\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a decimal number241\n\nThe binary equivalent of decimal 241 is 1 1 1 1 0 0 0 1\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 249", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "#To compute the binary addition\ndef binAdd(bin1, bin2): #function to add two binary numbers\n if not bin1 or not bin2:#checks if both the numbers are binary\n return '' \n\n maxlen = max(len(bin1), len(bin2))\n\n bin1 = bin1.zfill(maxlen) #zfill fills with zero to fill the entire width\n bin2 = bin2.zfill(maxlen)\n\n result = ''\n carry = 0\n\n i = maxlen - 1\n while(i >= 0):\n s = int(bin1[i]) + int(bin2[i])#adding bit by bit\n if s == 2: #1+1\n if carry == 0:\n carry = 1\n result = \"%s%s\" % (result, '0')\n else:\n result = \"%s%s\" % (result, '1')\n elif s == 1: # 1+0\n if carry == 1:\n result = \"%s%s\" % (result, '0')\n else:\n result = \"%s%s\" % (result, '1')\n else: # 0+0\n if carry == 1:\n result = \"%s%s\" % (result, '1')\n carry = 0 \n else:\n result = \"%s%s\" % (result, '0') \n\n i = i - 1;\n\n if carry>0:\n result = \"%s%s\" % (result, '1')\n return result[::-1]\nbin1 = raw_input('enter the first number: ')\nbin2 = raw_input('enter the second number: ')\nprint \"The sum of binary numbers\",bin1,\"and\",bin2,\"is\",binAdd(bin1,bin2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter the first number: 1110\nenter the second number: 1011\nThe sum of binary numbers 1110 and 1011 is 11001\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 258", "cell_type": "markdown", "metadata": {}}, {"execution_count": 21, "cell_type": "code", "source": "#to find the prime factors\n\ndef prime_factors(n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i: #modulp division to check of the number is prime or not\n i += 1\n else:\n n //= i\n factors.append(i) #append those numbers which readily divides the given number\n if n > 1:\n factors.append(n)\n return factors\nnumber=input(\"Enter the number for which the prime factors have to be found\");\na=prime_factors(number)\nprint a\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number for which the prime factors have to be found100\n[2, 2, 5, 5]\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 258", "cell_type": "markdown", "metadata": {}}, {"execution_count": 22, "cell_type": "code", "source": "#To say if a number is prime or not\nglobals() ['count']=0\nn=input(\"Enter the number\");\nfor i in range(2,n):#number thats not divisible by other than one and itself. so from 2 to n (n-1 in python for loop)\n if n%i==0:\n count=count+1\n num=i\nif count==0:\n print n,\"is prime\" \nelse:\n print n,\"is not prime because its divisible by\",num\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number101\n101 is prime\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 259", "cell_type": "markdown", "metadata": {}}, {"execution_count": 23, "cell_type": "code", "source": "#to find the prime factors\n\ndef prime_factors(n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i: #modulp division to check of the number is prime or not\n i += 1\n else:\n n //= i\n factors.append(i) #append those numbers which readily divides the given number\n if n > 1:\n factors.append(n)\n return factors\nnumber=input(\"Enter the number for which the prime factors have to be found\");\na=prime_factors(number)\nprint a\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number for which the prime factors have to be found7007\n[7, 7, 11, 13]\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 10: Page 263", "cell_type": "markdown", "metadata": {}}, {"execution_count": 24, "cell_type": "code", "source": "#To compute GCD\ndef gcd(a,b):#fuction computes gcd\n if b > a:\n return gcd(b,a)\n r = a%b\n if r == 0:\n return b\n return gcd(r,b)\nn1=input(\"Enter the first number\");\nn2=input(\"Enter the second number\");\nprint \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number24\nEnter the second number36\nGCD( 24 , 36 ) is 12\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 11: Page 263", "cell_type": "markdown", "metadata": {}}, {"execution_count": 25, "cell_type": "code", "source": "#To compute GCD\ndef gcd(a,b):#fuction computes gcd\n if b > a:\n return gcd(b,a)\n r = a%b\n if r == 0:\n return b\n return gcd(r,b)\nn1=input(\"Enter the first number\");\nn2=input(\"Enter the second number\");\nprint \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number17\nEnter the second number22\nGCD( 17 , 22 ) is 1\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 16: Page 268", "cell_type": "markdown", "metadata": {}}, {"execution_count": 26, "cell_type": "code", "source": "#to find gcd using euclidean algorithm\ndef gcd(a,b):#euclidean algithm definition\n x=a\n y=b\n while y!=0:\n r=x%y\n x=y\n y=r\n print \"gcd(\",a,\",\",b,\")is\",x\nnum1=input(\"Enter the first number\");\nnum2=input(\"Enter the second number\");\ngcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number414\nEnter the second number662\ngcd( 414 , 662 )is 2\n"}], "metadata": {"collapsed": false, "trusted": false}}, {"source": "# 04 Number Theory and Cryptography", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"source": "## Example 17: Page 270", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#to find gcd using euclidean algorithm\ndef gcd(a,b):#euclidean algithm definition\n x=a\n y=b\n while y!=0:\n r=x%y\n x=y\n y=r\n print \"gcd(\",a,\",\",b,\")is\",x\nnum1=input(\"Enter the first number\");\nnum2=input(\"Enter the second number\");\ngcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number252\nEnter the second number198\ngcd( 252 , 198 )is 18\n"}], "metadata": {"collapsed": false, "trusted": false}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}
|