Knowledge

Trial division

Source 📝

32: 198:
is more likely to be divisible by two than by three, and so on. With this ordering, there is no point in testing for divisibility by four if the number has already been determined not divisible by two, and so on for three and any multiple of three, etc. Therefore, the effort can be reduced by
743:
to obtain the prime numbers as candidate factors. A useful table need not be large: P(3512) = 32749, the last prime that fits into a sixteen-bit signed integer and P(6542) = 65521 for unsigned sixteen-bit integers. That would suffice to test primality for numbers up to 65537 = 4,295,098,369.
697: 891:
are chosen to have large prime factors of similar size so that they cannot be factored by any publicly known method in a useful time period on any available computer system or computer cluster such as
748:) would only be worthwhile if many numbers were to be tested. If instead a variant is used without primality testing, but simply dividing by every odd number less than the square root the base-2 1570: 871:
However, many-digit numbers that do not have factors in the small primes can require days or months to factor with the trial division. In such cases other methods are used such as the
227: 812:, and so on. It can be shown that 88% of all positive integers have a factor under 100 and that 92% have a factor under 1000. Thus, when confronted by an arbitrary large 792: 729: 840: 866: 1574: 1082: 604: 1115: 61: 1052: 20: 1630: 1439: 1075: 800:
Even so, this is a quite satisfactory method, considering that even the best-known algorithms have exponential time growth. For
1307: 979: 1249: 1068: 574:, not just primes. A more complicated implementation only testing primes would be significantly faster in the worst case. 1178: 1355: 967: 1017: 83: 903:, a 250-digit number, using the GNFS and resources of several supercomputers. The running time was 2700 core years. 54: 19:
This article is about the mathematical algorithm. For the judicial chamber of the International Criminal Court, see
1302: 1264: 1239: 1153: 1635: 1444: 1335: 1254: 1244: 1183: 1146: 1272: 1120: 1041: 206: 1525: 359: 1520: 1487: 1449: 1350: 920:
Mollin, Richard A. (2002). "A brief history of factoring and primality testing B. C. (before computers)".
358:
An example of the trial division algorithm, using successive integers as trial factors, is as follows (in
1401: 804:
chosen uniformly at random from integers of a given length, there is a 50% chance that 2 is a factor of
1566: 1556: 1515: 1291: 1285: 1259: 1130: 876: 190:
is divisible by any smaller number. Clearly, it is only worthwhile to test candidate factors less than
186:
refers to "the integer to be factored"), the trial division consists of systematically testing whether
1551: 1492: 44: 1454: 1327: 1173: 1125: 48: 40: 1469: 884: 732: 1360: 1580: 1530: 1510: 65: 1590: 1231: 1206: 1135: 762: 745: 705: 100: 819: 397:"""Return a list of the prime factors for a natural number.""" 1585: 1477: 1459: 1434: 1396: 1140: 949: 1027: 989: 845: 107:, the integer to be factored, can be divided by each number in turn that is less than the 8: 1595: 1561: 1482: 1386: 1345: 1340: 1317: 1221: 879:(GNFS). Because these methods also have superpolynomial time growth a practical limit of 1426: 1373: 1370: 1211: 1160: 1110: 1046: 937: 1167: 1546: 1502: 1216: 1193: 1013: 975: 740: 692:{\displaystyle \pi (2^{n/2})\approx {2^{n/2} \over \left({\frac {n}{2}}\right)\ln 2}} 1042:
Wikiversity offers a lesson on prime factorization using trial division with Python.
797:
In both cases, the required time grows exponentially with the digits of the number.
1391: 1023: 997: 985: 929: 1381: 1280: 1009: 971: 945: 872: 103:
algorithms. The essential idea behind trial division tests to see if an integer
1411: 1297: 1201: 1102: 1001: 896: 1060: 1624: 1406: 1091: 892: 352: 203:
as candidate factors. Furthermore, the trial factors need go no further than
816:, it is worthwhile to check for divisibility by the small primes, since for 1416: 296:= 5, etc. Then the last prime number worth testing as a possible factor of 200: 166: 108: 941: 583: 339:= 48 not just 25 because the square of the next prime is 49, and below 1094: 161: 933: 899:. The largest cryptography-grade number that has been factored is 900: 594:, if it starts from two and works up only to the square root of 1047:
Fast JavaScript Prime Factor Calculator using trial division
343:= 25 just 2 and 3 are sufficient. Should the square root of 335:
is a factor. Thus, testing with 2, 3, and 5 suffices up to
264:
A definite bound on the prime factors is possible. Suppose
570:
This version tests every integer up to the square root of
586:, trial division is a laborious algorithm. For a base-2 466:# The remainder of n divided by f might be zero. 1611:
indicate that algorithm is for numbers of special forms
253:
would have been detected earlier as being divisible by
99:
is the most laborious but easiest to understand of the
210: 848: 822: 765: 708: 607: 565:# Prime factors may be repeated: 12 factors to 2,2,3. 209: 194:, and in order from two upwards because an arbitrary 883:
digits is reached very quickly. For this reason, in
442:# While f is no larger than the square root of n ... 860: 834: 786: 739:. This does not take into account the overhead of 723: 691: 221: 996: 1622: 53:but its sources remain unclear because it lacks 1090: 1076: 1083: 1069: 1006:Prime numbers. A computational perspective 118:For example, to find the prime factors of 21:Judges of the International Criminal Court 964:A concrete introduction to higher algebra 756:, prime or not, it can take up to about: 487:# If so, it divides n. Add f to the list. 84:Learn how and when to remove this message 1053:Trial Division in Java, C and JavaScript 744:Preparing such a table (usually via the 222:{\displaystyle \scriptstyle {\sqrt {n}}} 808:and a 33% chance that 3 is a factor of 556:# Add the remaining prime n to the list 347:be an integer, then it is a factor and 1623: 961: 919: 160:Trial division was first described by 1064: 25: 13: 1049:. Can handle numbers up to about 2 968:Undergraduate Texts in Mathematics 14: 1647: 1292:Special number field sieve (SNFS) 1286:General number field sieve (GNFS) 1035: 735:, the number of primes less than 1631:Integer factorization algorithms 508:# But if f is not a factor of n, 418:# The first possible factor. 323:; equality here would mean that 30: 16:Integer factorization algorithm 1008:(2nd ed.). New York, NY: 970:(3rd ed.). New York, NY: 913: 718: 712: 632: 611: 499:# Divide that factor out of n. 1: 906: 520:# Add one to f and try again. 129:by successive primes: first, 1250:Lenstra elliptic curve (ECM) 233:is divisible by some number 7: 962:Childs, Lindsay N. (2009). 10: 1652: 1557:Exponentiation by squaring 1240:Continued fraction (CFRAC) 877:general number field sieve 18: 1604: 1539: 1501: 1468: 1425: 1369: 1326: 1230: 1192: 1101: 598:, the algorithm requires 173: 577: 406:# Prepare an empty list. 364: 257:or by a prime factor of 125:, one can try to divide 39:This article includes a 1470:Greatest common divisor 885:public key cryptography 787:{\displaystyle 2^{n/2}} 733:prime-counting function 724:{\displaystyle \pi (x)} 702:trial divisions, where 68:more precise citations. 1636:Division (mathematics) 1581:Modular exponentiation 862: 836: 835:{\displaystyle a=1000} 788: 725: 693: 223: 1308:Shanks's square forms 1232:Integer factorization 1207:Sieve of Eratosthenes 863: 837: 789: 746:Sieve of Eratosthenes 726: 694: 224: 101:integer factorization 1586:Montgomery reduction 1460:Function field sieve 1435:Baby-step giant-step 1281:Quadratic sieve (QS) 922:Mathematics Magazine 861:{\displaystyle n=10} 846: 820: 763: 706: 605: 207: 153:is itself prime. So 1596:Trachtenberg system 1562:Integer square root 1503:Modular square root 1222:Wheel factorization 1174:Quadratic Frobenius 1154:Lucas–Lehmer–Riesel 275:'th prime, so that 1488:Extended Euclidean 1427:Discrete logarithm 1356:Schönhage–Strassen 1212:Sieve of Pritchard 858: 832: 784: 721: 689: 245:were smaller than 219: 218: 41:list of references 1618: 1617: 1217:Sieve of Sundaram 998:Crandall, Richard 981:978-0-387-74527-5 741:primality testing 687: 671: 216: 178:Given an integer 94: 93: 86: 1643: 1567:Integer relation 1540:Other algorithms 1445:Pollard kangaroo 1336:Ancient Egyptian 1194:Prime-generating 1179:Solovay–Strassen 1092:Number-theoretic 1085: 1078: 1071: 1062: 1061: 1057: 1031: 993: 954: 953: 917: 867: 865: 864: 859: 841: 839: 838: 833: 793: 791: 790: 785: 783: 782: 778: 730: 728: 727: 722: 698: 696: 695: 690: 688: 686: 676: 672: 664: 657: 656: 652: 639: 631: 630: 626: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 334: 322: 306: 274: 270: 228: 226: 225: 220: 217: 212: 156: 152: 148: 144: 140: 136: 133:; next, neither 132: 128: 124: 114: 106: 89: 82: 78: 75: 69: 64:this article by 55:inline citations 34: 33: 26: 1651: 1650: 1646: 1645: 1644: 1642: 1641: 1640: 1621: 1620: 1619: 1614: 1600: 1535: 1497: 1464: 1421: 1365: 1322: 1226: 1188: 1161:Proth's theorem 1103:Primality tests 1097: 1089: 1056:(in Portuguese) 1055: 1038: 1020: 1010:Springer-Verlag 1002:Pomerance, Carl 982: 972:Springer-Verlag 958: 957: 934:10.2307/3219180 918: 914: 909: 873:quadratic sieve 847: 844: 843: 821: 818: 817: 774: 770: 766: 764: 761: 760: 707: 704: 703: 663: 659: 658: 648: 644: 640: 638: 622: 618: 614: 606: 603: 602: 580: 568: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 333: 324: 317: 308: 305: 301: 295: 288: 281: 272: 269: 265: 211: 208: 205: 204: 199:selecting only 176: 154: 150: 146: 142: 141:evenly divides 138: 134: 130: 126: 119: 112: 104: 90: 79: 73: 70: 59: 45:related reading 35: 31: 24: 17: 12: 11: 5: 1649: 1639: 1638: 1633: 1616: 1615: 1613: 1612: 1605: 1602: 1601: 1599: 1598: 1593: 1588: 1583: 1578: 1564: 1559: 1554: 1549: 1543: 1541: 1537: 1536: 1534: 1533: 1528: 1523: 1521:Tonelli–Shanks 1518: 1513: 1507: 1505: 1499: 1498: 1496: 1495: 1490: 1485: 1480: 1474: 1472: 1466: 1465: 1463: 1462: 1457: 1455:Index calculus 1452: 1450:Pohlig–Hellman 1447: 1442: 1437: 1431: 1429: 1423: 1422: 1420: 1419: 1414: 1409: 1404: 1402:Newton-Raphson 1399: 1394: 1389: 1384: 1378: 1376: 1367: 1366: 1364: 1363: 1358: 1353: 1348: 1343: 1338: 1332: 1330: 1328:Multiplication 1324: 1323: 1321: 1320: 1315: 1313:Trial division 1310: 1305: 1300: 1298:Rational sieve 1295: 1288: 1283: 1278: 1270: 1262: 1257: 1252: 1247: 1242: 1236: 1234: 1228: 1227: 1225: 1224: 1219: 1214: 1209: 1204: 1202:Sieve of Atkin 1198: 1196: 1190: 1189: 1187: 1186: 1181: 1176: 1171: 1164: 1157: 1150: 1143: 1138: 1133: 1128: 1126:Elliptic curve 1123: 1118: 1113: 1107: 1105: 1099: 1098: 1088: 1087: 1080: 1073: 1065: 1059: 1058: 1050: 1044: 1037: 1036:External links 1034: 1033: 1032: 1018: 994: 980: 956: 955: 911: 910: 908: 905: 897:computer grids 893:supercomputers 857: 854: 851: 831: 828: 825: 795: 794: 781: 777: 773: 769: 720: 717: 714: 711: 700: 699: 685: 682: 679: 675: 670: 667: 662: 655: 651: 647: 643: 637: 634: 629: 625: 621: 617: 613: 610: 579: 576: 370:trial_division 365: 353:perfect square 332: + 1 328: 316: + 1 312: 303: 293: 286: 279: 267: 215: 175: 172: 155:70 = 2 × 5 × 7 97:Trial division 92: 91: 49:external links 38: 36: 29: 15: 9: 6: 4: 3: 2: 1648: 1637: 1634: 1632: 1629: 1628: 1626: 1610: 1607: 1606: 1603: 1597: 1594: 1592: 1589: 1587: 1584: 1582: 1579: 1576: 1572: 1568: 1565: 1563: 1560: 1558: 1555: 1553: 1550: 1548: 1545: 1544: 1542: 1538: 1532: 1529: 1527: 1524: 1522: 1519: 1517: 1516:Pocklington's 1514: 1512: 1509: 1508: 1506: 1504: 1500: 1494: 1491: 1489: 1486: 1484: 1481: 1479: 1476: 1475: 1473: 1471: 1467: 1461: 1458: 1456: 1453: 1451: 1448: 1446: 1443: 1441: 1438: 1436: 1433: 1432: 1430: 1428: 1424: 1418: 1415: 1413: 1410: 1408: 1405: 1403: 1400: 1398: 1395: 1393: 1390: 1388: 1385: 1383: 1380: 1379: 1377: 1375: 1372: 1368: 1362: 1359: 1357: 1354: 1352: 1349: 1347: 1344: 1342: 1339: 1337: 1334: 1333: 1331: 1329: 1325: 1319: 1316: 1314: 1311: 1309: 1306: 1304: 1301: 1299: 1296: 1294: 1293: 1289: 1287: 1284: 1282: 1279: 1277: 1275: 1271: 1269: 1267: 1263: 1261: 1260:Pollard's rho 1258: 1256: 1253: 1251: 1248: 1246: 1243: 1241: 1238: 1237: 1235: 1233: 1229: 1223: 1220: 1218: 1215: 1213: 1210: 1208: 1205: 1203: 1200: 1199: 1197: 1195: 1191: 1185: 1182: 1180: 1177: 1175: 1172: 1170: 1169: 1165: 1163: 1162: 1158: 1156: 1155: 1151: 1149: 1148: 1144: 1142: 1139: 1137: 1134: 1132: 1129: 1127: 1124: 1122: 1119: 1117: 1114: 1112: 1109: 1108: 1106: 1104: 1100: 1096: 1093: 1086: 1081: 1079: 1074: 1072: 1067: 1066: 1063: 1054: 1051: 1048: 1045: 1043: 1040: 1039: 1029: 1025: 1021: 1019:0-387-25282-7 1015: 1011: 1007: 1003: 999: 995: 991: 987: 983: 977: 973: 969: 965: 960: 959: 951: 947: 943: 939: 935: 931: 927: 923: 916: 912: 904: 902: 898: 894: 890: 887:, values for 886: 882: 878: 874: 869: 855: 852: 849: 829: 826: 823: 815: 811: 807: 803: 798: 779: 775: 771: 767: 759: 758: 757: 755: 752:digit number 751: 747: 742: 738: 734: 715: 709: 683: 680: 677: 673: 668: 665: 660: 653: 649: 645: 641: 635: 627: 623: 619: 615: 608: 601: 600: 599: 597: 593: 590:digit number 589: 585: 575: 573: 363: 361: 356: 354: 350: 346: 342: 338: 331: 327: 321: 315: 311: 299: 292: 285: 278: 262: 260: 256: 252: 248: 244: 240: 236: 232: 213: 202: 201:prime numbers 197: 193: 189: 185: 181: 171: 169: 168: 163: 158: 122: 116: 110: 102: 98: 88: 85: 77: 67: 63: 57: 56: 50: 46: 42: 37: 28: 27: 22: 1608: 1312: 1290: 1273: 1265: 1184:Miller–Rabin 1166: 1159: 1152: 1147:Lucas–Lehmer 1145: 1005: 963: 928:(1): 18–29. 925: 921: 915: 888: 880: 870: 842:, in base-2 813: 809: 805: 801: 799: 796: 753: 749: 736: 731:denotes the 701: 595: 591: 587: 581: 571: 569: 357: 348: 344: 340: 336: 329: 325: 319: 313: 309: 297: 290: 283: 276: 263: 258: 254: 250: 246: 242: 238: 234: 230: 229:because, if 195: 191: 187: 183: 179: 177: 165: 164:in his book 159: 120: 117: 96: 95: 80: 71: 60:Please help 52: 1440:Pollard rho 1397:Goldschmidt 1131:Pocklington 1121:Baillie–PSW 167:Liber Abaci 145:; finally, 131:70 / 2 = 35 109:square root 66:introducing 1625:Categories 1552:Cornacchia 1547:Chakravala 1095:algorithms 1028:1088.11001 990:1165.00002 907:References 584:worst case 147:35 / 5 = 7 74:March 2014 1526:Berlekamp 1483:Euclidean 1371:Euclidean 1351:Toom–Cook 1346:Karatsuba 710:π 681:⁡ 636:≈ 609:π 239:n = p × q 162:Fibonacci 1493:Lehmer's 1387:Chunking 1374:division 1303:Fermat's 1004:(2005). 875:and the 170:(1202). 1609:Italics 1531:Kunerth 1511:Cipolla 1392:Fourier 1361:FĂŒrer's 1255:Euler's 1245:Dixon's 1168:PĂ©pin's 950:2107288 942:3219180 901:RSA-250 582:In the 271:is the 241:and if 237:, then 62:improve 1591:Schoof 1478:Binary 1382:Binary 1318:Shor's 1136:Fermat 1026:  1016:  988:  978:  948:  940:  559:return 544:append 475:append 360:Python 307:where 174:Method 149:, and 1412:Short 1141:Lucas 938:JSTOR 578:Speed 433:<= 421:while 388:-> 351:is a 318:> 289:= 3, 282:= 2, 47:, or 1407:Long 1341:Long 1014:ISBN 976:ISBN 895:and 830:1000 502:else 391:list 137:nor 123:= 70 1571:LLL 1417:SRT 1276:+ 1 1268:− 1 1116:APR 1111:AKS 1024:Zbl 986:Zbl 930:doi 493://= 382:int 367:def 362:): 300:is 115:. 111:of 1627:: 1575:KZ 1573:; 1022:. 1012:. 1000:; 984:. 974:. 966:. 946:MR 944:. 936:. 926:75 924:. 868:. 856:10 678:ln 529:!= 523:if 514:+= 457:== 445:if 355:. 261:. 249:, 157:. 143:35 127:70 51:, 43:, 1577:) 1569:( 1274:p 1266:p 1084:e 1077:t 1070:v 1030:. 992:. 952:. 932:: 889:a 881:n 853:= 850:n 827:= 824:a 814:a 810:a 806:a 802:a 780:2 776:/ 772:n 768:2 754:a 750:n 737:x 719:) 716:x 713:( 684:2 674:) 669:2 666:n 661:( 654:2 650:/ 646:n 642:2 633:) 628:2 624:/ 620:n 616:2 612:( 596:a 592:a 588:n 572:n 562:a 553:) 550:n 547:( 541:. 538:a 535:: 532:1 526:n 517:1 511:f 505:: 496:f 490:n 484:) 481:f 478:( 472:. 469:a 463:: 460:0 454:f 451:% 448:n 439:: 436:n 430:f 427:* 424:f 415:2 412:= 409:f 403:= 400:a 394:: 385:) 379:: 376:n 373:( 349:n 345:n 341:n 337:n 330:i 326:P 320:n 314:i 310:P 304:i 302:P 298:n 294:3 291:P 287:2 284:P 280:1 277:P 273:i 268:i 266:P 259:q 255:q 251:n 247:p 243:q 235:p 231:n 214:n 196:n 192:n 188:n 184:n 182:( 180:n 151:7 139:3 135:2 121:n 113:n 105:n 87:) 81:( 76:) 72:( 58:. 23:.

Index

Judges of the International Criminal Court
list of references
related reading
external links
inline citations
improve
introducing
Learn how and when to remove this message
integer factorization
square root
Fibonacci
Liber Abaci
prime numbers
perfect square
Python
worst case
prime-counting function
primality testing
Sieve of Eratosthenes
quadratic sieve
general number field sieve
public key cryptography
supercomputers
computer grids
RSA-250
doi
10.2307/3219180
JSTOR
3219180
MR

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑