Skip to content

Commit 30b3c3c

Browse files
author
sushmit
committed
SR: Chapter 7 addition
1 parent b0e318c commit 30b3c3c

File tree

2 files changed

+460
-22
lines changed

2 files changed

+460
-22
lines changed

.ipynb_checkpoints/Chapter7-checkpoint.ipynb

Lines changed: 230 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
},
7979
{
8080
"cell_type": "code",
81-
"execution_count": 71,
81+
"execution_count": 113,
8282
"metadata": {
8383
"collapsed": false
8484
},
@@ -102,7 +102,7 @@
102102
"Bd = (set([0,1,2,3]), set([0,1,2,3]))\n",
103103
"Bf = {(0,1):2, (0,2):4,(0,3):2,(0,4):8,(1,0):2,(1,1):1}\n",
104104
"B=Mat(Bd, Bf)\n",
105-
"print(B)"
105+
"print(B)\n"
106106
]
107107
},
108108
{
@@ -420,7 +420,7 @@
420420
},
421421
{
422422
"cell_type": "code",
423-
"execution_count": 89,
423+
"execution_count": 100,
424424
"metadata": {
425425
"collapsed": false
426426
},
@@ -429,8 +429,8 @@
429429
"name": "stdout",
430430
"output_type": "stream",
431431
"text": [
432-
"5180351 9796624 7848882\n",
433-
"9796624\n",
432+
"6172657 6045011 4283665\n",
433+
"6045011\n",
434434
"True\n",
435435
"True\n",
436436
"True\n",
@@ -439,11 +439,19 @@
439439
"[(3, 1), (5, 2)]\n",
440440
"[(2, 2), (3, 1)]\n",
441441
"[(2, 1), (7, 1), (11, 1)]\n",
442-
"[(2, 1), (3, 3), (11, 2)]\n",
443-
"[]\n",
442+
"3\n",
444443
"[]\n",
444+
"0\n",
445445
"one\n",
446-
"0\n"
446+
"0\n",
447+
"\n",
448+
" 11 2 3 5 7\n",
449+
"-------------\n",
450+
" 0 0 one 0 0\n",
451+
"\n",
452+
" 11 2 3 5 7\n",
453+
"----------------\n",
454+
" one one 0 one 0\n"
447455
]
448456
}
449457
],
@@ -481,9 +489,9 @@
481489
"primeset = {2,3,5,7,11,13}\n",
482490
"print(dumb_factor(12,primeset))\n",
483491
"print(dumb_factor(154,primeset))\n",
484-
"print(dumb_factor(2*3*3*3*11*11,primeset))\n",
492+
"print(len(dumb_factor(2*3*3*3*11*11,primeset)))\n",
485493
"print(dumb_factor(2 * 17,primeset))\n",
486-
"print(dumb_factor(2 *3 *5 *7 *19,primeset))\n",
494+
"print(len(dumb_factor(2 *3 *5 *7 *19,primeset)))\n",
487495
"# Task 7.8.5\n",
488496
"def int2GF2(i):\n",
489497
" if ( i % 2 == 0):\n",
@@ -497,7 +505,218 @@
497505
"print(int2GF2(3))\n",
498506
"print(int2GF2(4))\n",
499507
"\n",
500-
"# Task 7.8.6"
508+
"# Task 7.8.6\n",
509+
"\n",
510+
"def make_Vec(primeset, factors):\n",
511+
" \n",
512+
" return Vec(primeset,{i[0]:int2GF2(i[1]) for i in factors})\n",
513+
"\n",
514+
"print(make_Vec({2,3,5,7,11}, [(3,1)]))\n",
515+
"print(make_Vec({2,3,5,7,11}, [(2,17), (3, 0), (5,1), (11,3)]))\n",
516+
"\n",
517+
"\n"
518+
]
519+
},
520+
{
521+
"cell_type": "code",
522+
"execution_count": 122,
523+
"metadata": {
524+
"collapsed": false
525+
},
526+
"outputs": [
527+
{
528+
"name": "stdout",
529+
"output_type": "stream",
530+
"text": [
531+
"[Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: one}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{1: one}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{4: one}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: 0, 2: 0, 3: 0, 4: one, 5: 0, 6: 0, 7: one, 8: 0, 9: 0, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: one, 1: 0, 2: one, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: one, 2: 0, 3: 0, 4: 0, 5: one, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: one, 1: 0, 2: one, 3: one, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: one, 1: one, 2: one, 3: 0, 4: one, 5: one, 6: one, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: one, 2: 0, 3: 0, 4: 0, 5: one, 6: 0, 7: 0, 8: one, 9: 0, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: one, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: one, 8: 0, 9: one, 10: 0, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: 0, 2: one, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: one, 11: 0}), Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: one, 2: one, 3: 0, 4: 0, 5: one, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: one})]\n"
532+
]
533+
}
534+
],
535+
"source": [
536+
"# 7.8.7 Factoring an integer\n",
537+
"\n",
538+
"def find_candidates( N, primeset):\n",
539+
" roots = []\n",
540+
" rowlist = []\n",
541+
" x = intsqrt(N) + 2\n",
542+
" index = 0 \n",
543+
" while ( index < len(primeset) + 1):\n",
544+
" if len(dumb_factor(x*x -N , primeset)) > 0:\n",
545+
" roots.append(x)\n",
546+
" rowlist.append(make_Vec(primeset ,dumb_factor(x*x -N , primeset) ))\n",
547+
" index = index + 1\n",
548+
" x = x + 1\n",
549+
" return roots,rowlist\n",
550+
"\n",
551+
"#print(rowdict2mat(transformation_rows(find_candidates(2419,primes(32))[1])) *\n",
552+
"#rowdict2mat(find_candidates(2419,primes(32))[1]))\n",
553+
"\n",
554+
"print(transformation_rows(find_candidates(2419,primes(32))[1]))\n",
555+
"\n"
556+
]
557+
},
558+
{
559+
"cell_type": "code",
560+
"execution_count": 178,
561+
"metadata": {
562+
"collapsed": false
563+
},
564+
"outputs": [
565+
{
566+
"name": "stdout",
567+
"output_type": "stream",
568+
"text": [
569+
"41\n",
570+
"2419\n"
571+
]
572+
}
573+
],
574+
"source": [
575+
"# Task 7.8.8\n",
576+
"a = 53 * 77\n",
577+
"b = 2 * (3**2 )* 5 *13\n",
578+
"N = 2419\n",
579+
"print(gcd( a -b ,N))\n",
580+
"\n",
581+
"# Task 7.8.9\n",
582+
"a = 52*67*71\n",
583+
"b = 2 *(3 **2)*5*19*23\n",
584+
"print(gcd( a- b , N))\n"
585+
]
586+
},
587+
{
588+
"cell_type": "code",
589+
"execution_count": 179,
590+
"metadata": {
591+
"collapsed": false
592+
},
593+
"outputs": [
594+
{
595+
"name": "stdout",
596+
"output_type": "stream",
597+
"text": [
598+
"\n",
599+
" 0 1 10 11 2 3 4 5 6 7 8 9\n",
600+
"---------------------------------\n",
601+
" 0 one 0 one one 0 0 one 0 0 0 0\n",
602+
"[51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79]\n",
603+
"True\n",
604+
"True\n",
605+
"True\n",
606+
"True\n"
607+
]
608+
},
609+
{
610+
"data": {
611+
"text/plain": [
612+
"2911"
613+
]
614+
},
615+
"execution_count": 179,
616+
"metadata": {},
617+
"output_type": "execute_result"
618+
}
619+
],
620+
"source": [
621+
"M = transformation_rows(find_candidates(2419,primes(32))[1])\n",
622+
"roots = find_candidates(2419,primes(32))[0]\n",
623+
"# Task 7.8.10:\n",
624+
"def find_a_and_b( v, roots , N):\n",
625+
" alist = [roots[key] for key in v.f if v[key] == one ]\n",
626+
" a = prod(alist)\n",
627+
" c = prod([x*x - N for x in alist])\n",
628+
" b = intsqrt(c)\n",
629+
" print(b*b == c)\n",
630+
" \n",
631+
" return a,b\n",
632+
"print(M[11])\n",
633+
"print(roots) \n",
634+
"\n",
635+
"# alist = [1,2,9,4]\n",
636+
"# prod(alist)\n",
637+
"gcd(find_a_and_b(M[10],roots,N)[0] - find_a_and_b(M[10],roots,N)[1] , N)\n",
638+
"find_a_and_b(M[10],roots,N)[0] - find_a_and_b(M[10],roots,N)[1]"
639+
]
640+
},
641+
{
642+
"cell_type": "code",
643+
"execution_count": 182,
644+
"metadata": {
645+
"collapsed": false
646+
},
647+
"outputs": [],
648+
"source": [
649+
"# Task 7.8.11 and Task 7.8.12\n",
650+
"#N = 2461799993978700679\n",
651+
"N = 20672783502493917028427\n",
652+
"primelist = primes(10000)\n",
653+
"\n",
654+
"roots = find_candidates(N,primelist)[0]\n",
655+
"rowlist = find_candidates(N,primelist)[1]\n",
656+
"\n",
657+
"M = transformation_rows(rowlist,sorted(primelist , reverse = True))\n"
658+
]
659+
},
660+
{
661+
"cell_type": "code",
662+
"execution_count": 185,
663+
"metadata": {
664+
"collapsed": false
665+
},
666+
"outputs": [
667+
{
668+
"name": "stdout",
669+
"output_type": "stream",
670+
"text": [
671+
"True\n",
672+
"True\n",
673+
"True\n",
674+
"True\n",
675+
"True\n",
676+
"True\n",
677+
"20672783502493917028427\n",
678+
"1229\n"
679+
]
680+
}
681+
],
682+
"source": [
683+
"index = len(M) -1\n",
684+
"while (gcd(find_a_and_b(M[index],roots,N)[0] - find_a_and_b(M[index],roots,N)[1] , N))== 1 or (gcd(find_a_and_b(M[index],roots,N)[0] - find_a_and_b(M[index],roots,N)[1] , N)) != N:\n",
685+
" index = index -1\n",
686+
"print(gcd(find_a_and_b(M[index],roots,N)[0] - find_a_and_b(M[index],roots,N)[1] , N))\n",
687+
"print(index)"
688+
]
689+
},
690+
{
691+
"cell_type": "code",
692+
"execution_count": 181,
693+
"metadata": {
694+
"collapsed": false
695+
},
696+
"outputs": [
697+
{
698+
"ename": "IndexError",
699+
"evalue": "list index out of range",
700+
"output_type": "error",
701+
"traceback": [
702+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
703+
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
704+
"\u001b[0;32m<ipython-input-181-a80f75932abd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfind_a_and_b\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mroots\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mfind_a_and_b\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mroots\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m# # Task 7.8.13\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# #N = 2461799993978700679\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mN\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m20672783502493917028427\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# primelist = primes(10000)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
705+
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
706+
]
707+
}
708+
],
709+
"source": [
710+
"find_a_and_b(M[index],roots,N)[0] - find_a_and_b(M[index],roots,N)[1] \n",
711+
"# # Task 7.8.13\n",
712+
"# #N = 2461799993978700679\n",
713+
"N = 20672783502493917028427\n",
714+
"# primelist = primes(10000)\n",
715+
"\n",
716+
"# roots = find_candidates(N,primelist)[0]\n",
717+
"# rowlist = find_candidates(N,primelist)[1]\n",
718+
"\n",
719+
"# M_rows = transformation_rows(rowlist, sorted(primelist , reverse = True))"
501720
]
502721
}
503722
],

0 commit comments

Comments
 (0)