1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 03: Page 239"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The quotient when 101 is divided by 11 is 9 = 101 div 11 and the remainder is 2 = 101 mod 11\n"
]
}
],
"source": [
"#To find the quotient and remainder \n",
"dividend=101\n",
"divisor=11\n",
"quotient=dividend/divisor #To find quotient\n",
"remainder=dividend%divisor #To find remainder\n",
"dividend=(divisor*quotient)+remainder\n",
"print \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 04: Page 240"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The quotient when -11 is divided by 3 is -4 = -11 div 3 and the remainder is 1 = -11 mod 3\n"
]
}
],
"source": [
"#To find the quotient and remainder\n",
"dividend=-11\n",
"divisor=3\n",
"quotient=dividend/divisor\n",
"remainder=dividend%divisor\n",
"dividend=(divisor*quotient)+remainder\n",
"print \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 01: Page 246"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"enter a number: 101011111\n",
"351\n"
]
}
],
"source": [
"#To convert binary to decimal equivalent\n",
"binary_num= raw_input('enter a number: ')\n",
"decimal = 0\n",
"for digit in binary_num:\n",
" decimal = decimal*2 + int(digit)\n",
"\n",
"print decimal\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 03: Page 247"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"enter a number: 2AE0B\n",
"The conversion of 2AE0B to hexadeimal is 175627\n"
]
}
],
"source": [
"#To convert decimal to hexadecimal\n",
"dec= raw_input('enter a number: ')\n",
"\n",
"print \"The conversion of\",dec,\"to hexadeimal is\",int(dec,16)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 04: Page 247"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a number12345\n",
"The decimal number 12345 is converted to its octal equivalent : 3 0 0 7 1\n"
]
}
],
"source": [
"#To compute decimal to octal\n",
"numbers= []\n",
"dec=input(\"Enter a number\");\n",
"num=dec\n",
"while dec!=0:\n",
" \n",
" rem=dec%8\n",
" dec=dec/8\n",
" numbers.append(rem)\n",
"print \"The decimal number\",num,\"is converted to its octal equivalent : \",\n",
"for i in reversed(numbers):\n",
" print i,\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 05: Page 248"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the decimal number which is to be converted to hexadecimal177130\n",
"The hexadecimal equivalent of decimal 177130 is 0 2 B 3 E A\n"
]
}
],
"source": [
"#To convert Decimal to hexadecimal\n",
"num=[]\n",
"def 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 )\n",
"dec_num=input(\"Enter the decimal number which is to be converted to hexadecimal\");\n",
"ChangeHex(dec_num)\n",
"print \"The hexadecimal equivalent of decimal\",dec_num,\"is\",\n",
"for i in reversed(num):\n",
" print i,\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 06: Page 249"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a decimal number241\n",
"\n",
"The binary equivalent of decimal 241 is 1 1 1 1 0 0 0 1\n"
]
}
],
"source": [
"#Compute Decimal to Binary\n",
"array=[]\n",
"def 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) \n",
"dec_num=input(\"Enter a decimal number\")\n",
"conv(dec_num)\n",
"print \"The binary equivalent of decimal\",dec_num,\"is\",\n",
"for i in reversed(array):\n",
" print i,\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 06: Page 249"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"enter the first number: 1110\n",
"enter the second number: 1011\n",
"The sum of binary numbers 1110 and 1011 is 11001\n"
]
}
],
"source": [
"#To compute the binary addition\n",
"def 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]\n",
"bin1 = raw_input('enter the first number: ')\n",
"bin2 = raw_input('enter the second number: ')\n",
"print \"The sum of binary numbers\",bin1,\"and\",bin2,\"is\",binAdd(bin1,bin2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 02: Page 258"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number for which the prime factors have to be found100\n",
"[2, 2, 5, 5]\n"
]
}
],
"source": [
"#to find the prime factors\n",
"\n",
"def 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\n",
"number=input(\"Enter the number for which the prime factors have to be found\");\n",
"a=prime_factors(number)\n",
"print a\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 03: Page 258"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number101\n",
"101 is prime\n"
]
}
],
"source": [
"#To say if a number is prime or not\n",
"globals() ['count']=0\n",
"n=input(\"Enter the number\");\n",
"for 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\n",
"if count==0:\n",
" print n,\"is prime\" \n",
"else:\n",
" print n,\"is not prime because its divisible by\",num\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 04: Page 259"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number for which the prime factors have to be found7007\n",
"[7, 7, 11, 13]\n"
]
}
],
"source": [
"#to find the prime factors\n",
"\n",
"def 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\n",
"number=input(\"Enter the number for which the prime factors have to be found\");\n",
"a=prime_factors(number)\n",
"print a\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 10: Page 263"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the first number24\n",
"Enter the second number36\n",
"GCD( 24 , 36 ) is 12\n"
]
}
],
"source": [
"#To compute GCD\n",
"def 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)\n",
"n1=input(\"Enter the first number\");\n",
"n2=input(\"Enter the second number\");\n",
"print \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 11: Page 263"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the first number17\n",
"Enter the second number22\n",
"GCD( 17 , 22 ) is 1\n"
]
}
],
"source": [
"#To compute GCD\n",
"def 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)\n",
"n1=input(\"Enter the first number\");\n",
"n2=input(\"Enter the second number\");\n",
"print \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 16: Page 268"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the first number414\n",
"Enter the second number662\n",
"gcd( 414 , 662 )is 2\n"
]
}
],
"source": [
"#to find gcd using euclidean algorithm\n",
"def 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\n",
"num1=input(\"Enter the first number\");\n",
"num2=input(\"Enter the second number\");\n",
"gcd(num1,num2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# 04:NUMBER THEORY AND CRYPTOGRAPHY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 17: Page 270"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the first number252\n",
"Enter the second number198\n",
"gcd( 252 , 198 )is 18\n"
]
}
],
"source": [
"#to find gcd using euclidean algorithm\n",
"def 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\n",
"num1=input(\"Enter the first number\");\n",
"num2=input(\"Enter the second number\");\n",
"gcd(num1,num2)\n"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|