|
C语言趣味程序百例精解之java实现(31)哥德巴赫猜想
程序:
public class Test31{
public static void main(String args[]){
new Test31().GeDeBaHe31();
}
/**
* 31.哥德巴赫猜想
*/
public void GeDeBaHe31() {
int count=0;
for (int i = 4; i <= 2000; i+=2) {
int n;
for( n=2;n< i;n++)
if (isSuShu(n) && isSuShu(i-n)) {
System.out.printf(++count%6!=0? "%d=%d+%d\t":"%d=%d+%d\n",i,n,i-n);
break;
}
if(n==i) System.out.printf("error %d\n",i);
}
}
/**
* 是素数
*/
public boolean isSuShu(int n) {
boolean isSuShu = true;
if (n == 1 || n == 2)
return true;
for (int i = 2; i < Math.sqrt(n) + 1; i += 1) {
if (n % i == 0) {
return false;
}
}
if (isSuShu == true)
return true;
else
return false;
}
}
C:\bat>javac Test31.java
C:\bat>java Test31
4=2+2 6=3+3 8=3+5 10=3+7 12=5+7 14=3+11
16=3+13 18=5+13 20=3+17 22=3+19 24=5+19 26=3+23
28=5+23 30=7+23 32=3+29 34=3+31 36=5+31 38=7+31
40=3+37 42=5+37 44=3+41 46=3+43 48=5+43 50=3+47
52=5+47 54=7+47 56=3+53 58=5+53 60=7+53 62=3+59
64=3+61 66=5+61 68=7+61 70=3+67 72=5+67 74=3+71
76=3+73 78=5+73 80=7+73 82=3+79 84=5+79 86=3+83
88=5+83 90=7+83 92=3+89 94=5+89 96=7+89 98=19+79
100=3+97 102=5+97 104=3+101 106=3+103 108=5+103 110=3+107
112=3+109 114=5+109 116=3+113 118=5+113 120=7+113 122=13+109
124=11+113 126=13+113 128=19+109 130=3+127 132=5+127 134=3+131
136=5+131 138=7+131 140=3+137 142=3+139 144=5+139 146=7+139
148=11+137 150=11+139 152=3+149 154=3+151 156=5+151 158=7+151
160=3+157 162=5+157 164=7+157 166=3+163 168=5+163 170=3+167
172=5+167 174=7+167 176=3+173 178=5+173 180=7+173 182=3+179
184=3+181 186=5+181 188=7+181 190=11+179 192=11+181 194=3+191
196=3+193 198=5+193 200=3+197 202=3+199 204=5+199 206=7+199
208=11+197 210=11+199 212=13+199 214=3+211 216=5+211 218=7+211
220=23+197 222=11+211 224=13+211 226=3+223 228=5+223 230=3+227
232=3+229 234=5+229 236=3+233 238=5+233 240=7+233 242=3+239
244=3+241 246=5+241 248=7+241 250=11+239 252=11+241 254=3+251
256=5+251 258=7+251 260=3+257 262=5+257 264=7+257 266=3+263
268=5+263 270=7+263 272=3+269 274=3+271 276=5+271 278=7+271
280=3+277 282=5+277 284=3+281 286=3+283 288=5+283 290=7+283
292=11+281 294=11+283 296=3+293 298=5+293 300=7+293 302=19+283
304=11+293 306=13+293 308=31+277 310=3+307 312=5+307 314=3+311
316=3+313 318=5+313 320=3+317 322=5+317 324=7+317 326=13+313
328=11+317 330=13+317 332=19+313 334=3+331 336=5+331 338=7+331
340=3+337 342=5+337 344=7+337 346=29+317 348=11+337 350=3+347
352=3+349 354=5+349 356=3+353 358=5+353 360=7+353 362=3+359
364=5+359 366=7+359 368=19+349 370=3+367 372=5+367 374=7+367
376=3+373 378=5+373 380=7+373 382=3+379 384=5+379 386=3+383
388=5+383 390=7+383 392=3+389 394=5+389 396=7+389 398=19+379
400=3+397 402=5+397 404=3+401 406=5+401 408=7+401 410=13+397
412=3+409 414=5+409 416=7+409 418=17+401 420=11+409 422=3+419
424=3+421 426=5+421 428=7+421 430=11+419 432=11+421 434=3+431
436=3+433 438=5+433 440=7+433 442=3+439 444=5+439 446=3+443
448=5+443 450=7+443 452=3+449 454=5+449 456=7+449 458=19+439
460=3+457 462=5+457 464=3+461 466=3+463 468=5+463 470=3+467
472=5+467 474=7+467 476=13+463 478=11+467 480=13+467 482=3+479
484=5+479 486=7+479 488=31+457 490=3+487 492=5+487 494=3+491
496=5+491 498=7+491 500=13+487 502=3+499 504=5+499 506=3+503
508=5+503 510=7+503 512=3+509 514=5+509 516=7+509 518=19+499
520=11+509 522=13+509 524=3+521 526=3+523 528=5+523 530=7+523
532=11+521 534=11+523 536=13+523 538=17+521 540=17+523 542=19+523
544=3+541 546=5+541 548=7+541 550=3+547 552=5+547 554=7+547
556=47+509 558=11+547 560=3+557 562=5+557 564=7+557 566=3+563
568=5+563 570=7+563 572=3+569 574=3+571 576=5+571 578=7+571
580=3+577 582=5+577 584=7+577 586=17+569 588=11+577 590=3+587
592=5+587 594=7+587 596=3+593 598=5+593 600=7+593 602=3+599
604=3+601 606=5+601 608=7+601 610=3+607 612=5+607 614=7+607
616=3+613 618=5+613 620=3+617 622=3+619 624=5+619 626=7+619
628=11+617 630=11+619 632=13+619 634=3+631 636=5+631 638=7+631
640=23+617 642=11+631 644=3+641 646=3+643 648=5+643 650=3+647
652=5+647 654=7+647 656=3+653 658=5+653 660=7+653 662=3+659
664=3+661 666=5+661 668=7+661 670=11+659 672=11+661 674=13+661
676=3+673 678=5+673 680=3+677 682=5+677 684=7+677 686=3+683
688=5+683 690=7+683 692=19+673 694=3+691 696=5+691 698=7+691
700=17+683 702=11+691 704=3+701 706=5+701 708=7+701 710=19+691
712=3+709 714=5+709 716=7+709 718=17+701 720=11+709 722=3+719
724=5+719 726=7+719 728=19+709 730=3+727 732=5+727 734=7+727
736=3+733 738=5+733 740=7+733 742=3+739 744=5+739 746=3+743
748=5+743 750=7+743 752=13+739 754=3+751 756=5+751 758=7+751
760=3+757 762=5+757 764=3+761 766=5+761 768=7+761 770=13+757
772=3+769 774=5+769 776=3+773 778=5+773 780=7+773 782=13+769
784=11+773 786=13+773 788=19+769 790=3+787 792=5+787 794=7+787
796=23+773 798=11+787 800=3+797 802=5+797 804=7+797 806=19+787
808=11+797 810=13+797 812=3+809 814=3+811 816=5+811 818=7+811
820=11+809 822=11+811 824=3+821 826=3+823 828=5+823 830=3+827
832=3+829 834=5+829 836=7+829 838=11+827 840=11+829 842=3+839
844=5+839 846=7+839 848=19+829 850=11+839 852=13+839 854=31+823
856=3+853 858=5+853 860=3+857 862=3+859 864=5+859 866=3+863
868=5+863 870=7+863 872=13+859 874=11+863 876=13+863 878=19+859
880=3+877 882=5+877 884=3+881 886=3+883 888=5+883 890=3+887
892=5+887 894=7+887 896=13+883 898=11+887 900=13+887 902=19+883
904=17+887 906=19+887 908=31+877 910=3+907 912=5+907 914=3+911
916=5+911 918=7+911 920=13+907 922=3+919 924=5+919 926=7+919
928=17+911 930=11+919 932=3+929 934=5+929 936=7+929 938=19+919
940=3+937 942=5+937 944=3+941 946=5+941 948=7+941 950=3+947
952=5+947 954=7+947 956=3+953 958=5+953 960=7+953 962=43+919
964=11+953 966=13+953 968=31+937 970=3+967 972=5+967 974=3+971
976=5+971 978=7+971 980=3+977 982=5+977 984=7+977 986=3+983
988=5+983 990=7+983 992=73+919 994=3+991 996=5+991 998=7+991
1000=3+997 1002=5+997 1004=7+997 1006=23+983 1008=11+997 1010=13+997
1012=3+1009 1014=5+1009 1016=3+1013 1018=5+1013 1020=7+1013 1022=3+1019
1024=3+1021 1026=5+1021 1028=7+1021 1030=11+1019 1032=11+1021 1034=3+1031
1036=3+1033 1038=5+1033 1040=7+1033 1042=3+1039 1044=5+1039 1046=7+1039
1048=17+1031 1050=11+1039 1052=3+1049 1054=3+1051 1056=5+1051 1058=7+1051
1060=11+1049 1062=11+1051 1064=3+1061 1066=3+1063 1068=5+1063 1070=7+1063
1072=3+1069 1074=5+1069 1076=7+1069 1078=17+1061 1080=11+1069 1082=13+1069
1084=23+1061 1086=17+1069 1088=19+1069 1090=3+1087 1092=5+1087 1094=3+1091
1096=3+1093 1098=5+1093 1100=3+1097 1102=5+1097 1104=7+1097 1106=3+1103
1108=5+1103 1110=7+1103 1112=3+1109 1114=5+1109 1116=7+1109 1118=31+1087
1120=3+1117 1122=5+1117 1124=7+1117 1126=3+1123 1128=5+1123 1130=7+1123
1132=3+1129 1134=5+1129 1136=7+1129 1138=29+1109 1140=11+1129 1142=13+1129
1144=41+1103 1146=17+1129 1148=19+1129 1150=41+1109 1152=23+1129 1154=3+1151
1156=3+1153 1158=5+1153 1160=7+1153 1162=11+1151 1164=11+1153 1166=3+1163
1168=5+1163 1170=7+1163 1172=19+1153 1174=3+1171 1176=5+1171 1178=7+1171
1180=17+1163 1182=11+1171 1184=3+1181 1186=5+1181 1188=7+1181 1190=3+1187
1192=5+1187 1194=7+1187 1196=3+1193 1198=5+1193 1200=7+1193 1202=31+1171
1204=3+1201 1206=5+1201 1208=7+1201 1210=17+1193 1212=11+1201 1214=13+1201
1216=3+1213 1218=5+1213 1220=3+1217 1222=5+1217 1224=7+1217 1226=3+1223
1228=5+1223 1230=7+1223 1232=3+1229 1234=3+1231 1236=5+1231 1238=7+1231
1240=3+1237 1242=5+1237 1244=7+1237 1246=17+1229 1248=11+1237 1250=13+1237
1252=3+1249 1254=5+1249 1256=7+1249 1258=29+1229 1260=11+1249 1262=3+1259
1264=5+1259 1266=7+1259 1268=19+1249 1270=11+1259 1272=13+1259 1274=37+1237
1276=17+1259 1278=19+1259 1280=3+1277 1282=3+1279 1284=5+1279 1286=3+1283
1288=5+1283 1290=7+1283 1292=3+1289 1294=3+1291 1296=5+1291 1298=7+1291
1300=3+1297 1302=5+1297 1304=3+1301 1306=3+1303 1308=5+1303 1310=3+1307
1312=5+1307 1314=7+1307 1316=13+1303 1318=11+1307 1320=13+1307 1322=3+1319
1324=3+1321 1326=5+1321 1328=7+1321 1330=3+1327 1332=5+1327 1334=7+1327
1336=17+1319 1338=11+1327 1340=13+1327 1342=23+1319 1344=17+1327 1346=19+1327
1348=29+1319 1350=23+1327 1352=31+1321 1354=47+1307 1356=29+1327 1358=31+1327
1360=41+1319 1362=41+1321 1364=3+1361 1366=5+1361 1368=7+1361 1370=3+1367
1372=5+1367 1374=7+1367 1376=3+1373 1378=5+1373 1380=7+1373 1382=61+1321
1384=3+1381 1386=5+1381 1388=7+1381 1390=17+1373 1392=11+1381 1394=13+1381
1396=23+1373 1398=17+1381 1400=19+1381 1402=3+1399 1404=5+1399 1406=7+1399
1408=41+1367 1410=11+1399 1412=3+1409 1414=5+1409 1416=7+1409 1418=19+1399
1420=11+1409 1422=13+1409 1424=43+1381 1426=3+1423 1428=5+1423 1430=3+1427
1432=3+1429 1434=5+1429 1436=3+1433 1438=5+1433 1440=7+1433 1442=3+1439
1444=5+1439 1446=7+1439 1448=19+1429 1450=3+1447 1452=5+1447 1454=3+1451
1456=3+1453 1458=5+1453 1460=7+1453 1462=3+1459 1464=5+1459 1466=7+1459
1468=17+1451 1470=11+1459 1472=13+1459 1474=3+1471 1476=5+1471 1478=7+1471
1480=29+1451 1482=11+1471 1484=3+1481 1486=3+1483 1488=5+1483 1490=3+1487
1492=3+1489 1494=5+1489 1496=3+1493 1498=5+1493 1500=7+1493 1502=3+1499
1504=5+1499 1506=7+1499 1508=19+1489 1510=11+1499 1512=13+1499 1514=3+1511
1516=5+1511 1518=7+1511 1520=31+1489 1522=11+1511 1524=13+1511 1526=3+1523
1528=5+1523 1530=7+1523 1532=43+1489 1534=3+1531 1536=5+1531 1538=7+1531
1540=17+1523 1542=11+1531 1544=13+1531 1546=3+1543 1548=5+1543 1550=7+1543
1552=3+1549 1554=5+1549 1556=3+1553 1558=5+1553 1560=7+1553 1562=3+1559
1564=5+1559 1566=7+1559 1568=19+1549 1570=3+1567 1572=5+1567 1574=3+1571
1576=5+1571 1578=7+1571 1580=13+1567 1582=3+1579 1584=5+1579 1586=3+1583
1588=5+1583 1590=7+1583 1592=13+1579 1594=11+1583 1596=13+1583 1598=19+1579
1600=3+1597 1602=5+1597 1604=3+1601 1606=5+1601 1608=7+1601 1610=3+1607
1612=3+1609 1614=5+1609 1616=3+1613 1618=5+1613 1620=7+1613 1622=3+1619
1624=3+1621 1626=5+1621 1628=7+1621 1630=3+1627 1632=5+1627 1634=7+1627
1636=17+1619 1638=11+1627 1640=3+1637 1642=5+1637 1644=7+1637 1646=19+1627
1648=11+1637 1650=13+1637 1652=31+1621 1654=17+1637 1656=19+1637 1658=31+1627
1660=3+1657 1662=5+1657 1664=7+1657 1666=3+1663 1668=5+1663 1670=3+1667
1672=3+1669 1674=5+1669 1676=7+1669 1678=11+1667 1680=11+1669 1682=13+1669
1684=17+1667 1686=17+1669 1688=19+1669 1690=23+1667 1692=23+1669 1694=31+1663
1696=3+1693 1698=5+1693 1700=3+1697 1702=3+1699 1704=5+1699 1706=7+1699
1708=11+1697 1710=11+1699 1712=3+1709 1714=5+1709 1716=7+1709 1718=19+1699
1720=11+1709 1722=13+1709 1724=3+1721 1726=3+1723 1728=5+1723 1730=7+1723
1732=11+1721 1734=11+1723 1736=3+1733 1738=5+1733 1740=7+1733 1742=19+1723
1744=3+1741 1746=5+1741 1748=7+1741 1750=3+1747 1752=5+1747 1754=7+1747
1756=3+1753 1758=5+1753 1760=7+1753 1762=3+1759 1764=5+1759 1766=7+1759
1768=47+1721 1770=11+1759 1772=13+1759 1774=41+1733 1776=17+1759 1778=19+1759
1780=3+1777 1782=5+1777 1784=7+1777 1786=3+1783 1788=5+1783 1790=3+1787
1792=3+1789 1794=5+1789 1796=7+1789 1798=11+1787 1800=11+1789 1802=13+1789
1804=3+1801 1806=5+1801 1808=7+1801 1810=23+1787 1812=11+1801 1814=3+1811
1816=5+1811 1818=7+1811 1820=19+1801 1822=11+1811 1824=13+1811 1826=3+1823
1828=5+1823 1830=7+1823 1832=31+1801 1834=3+1831 1836=5+1831 1838=7+1831
1840=17+1823 1842=11+1831 1844=13+1831 1846=23+1823 1848=17+1831 1850=3+1847
1852=5+1847 1854=7+1847 1856=67+1789 1858=11+1847 1860=13+1847 1862=31+1831
1864=3+1861 1866=5+1861 1868=7+1861 1870=3+1867 1872=5+1867 1874=3+1871
1876=3+1873 1878=5+1873 1880=3+1877 1882=3+1879 1884=5+1879 1886=7+1879
1888=11+1877 1890=11+1879 1892=3+1889 1894=5+1889 1896=7+1889 1898=19+1879
1900=11+1889 1902=13+1889 1904=3+1901 1906=5+1901 1908=7+1901 1910=3+1907
1912=5+1907 1914=7+1907 1916=3+1913 1918=5+1913 1920=7+1913 1922=43+1879
1924=11+1913 1926=13+1913 1928=61+1867 1930=17+1913 1932=19+1913 1934=3+1931
1936=3+1933 1938=5+1933 1940=7+1933 1942=11+1931 1944=11+1933 1946=13+1933
1948=17+1931 1950=17+1933 1952=3+1949 1954=3+1951 1956=5+1951 1958=7+1951
1960=11+1949 1962=11+1951 1964=13+1951 1966=17+1949 1968=17+1951 1970=19+1951
1972=23+1949 1974=23+1951 1976=3+1973 1978=5+1973 1980=7+1973 1982=3+1979
1984=5+1979 1986=7+1979 1988=37+1951 1990=3+1987 1992=5+1987 1994=7+1987
1996=3+1993 1998=5+1993 2000=3+1997
C:\bat> |
|