diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-07-04 21:10:19 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-07-04 21:10:19 +0100 |
| commit | 251b2bd8b0ff8d3b1262173c33a4e999cf37846a (patch) | |
| tree | e6d58a6a75f6a895cb07762b0183860f48096b9d /challenge-156 | |
| parent | 9a0e9aa937cd71b048889a57f68896e5187466ac (diff) | |
| download | perlweeklychallenge-club-251b2bd8b0ff8d3b1262173c33a4e999cf37846a.tar.gz perlweeklychallenge-club-251b2bd8b0ff8d3b1262173c33a4e999cf37846a.tar.bz2 perlweeklychallenge-club-251b2bd8b0ff8d3b1262173c33a4e999cf37846a.zip | |
new stuff
Diffstat (limited to 'challenge-156')
| -rw-r--r-- | challenge-156/james-smith/perl/ch-1.pl | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/challenge-156/james-smith/perl/ch-1.pl b/challenge-156/james-smith/perl/ch-1.pl index 3ca22db4cf..2b5c609f54 100644 --- a/challenge-156/james-smith/perl/ch-1.pl +++ b/challenge-156/james-smith/perl/ch-1.pl @@ -9,8 +9,42 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my %primes = map { $_ => 1 } 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37; +#method_sprintf(1000); -my$n=0; +my @TESTS = ( + [ 10, '3 5 6 7 9 10 11 12 13 14' ], + [ 20, '3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28' ], + #[ 1000, '3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 37 38 40 41 42 44 47 48 49 50 52 55 56 59 61 62 65 66 67 68 69 70 72 73 74 76 79 80 81 82 84 87 88 91 93 94 96 97 98 100 103 104 107 109 110 112 115 117 118 121 122 124 127 129 130 131 132 133 134 136 137 138 140 143 144 145 146 148 151 152 155 157 158 160 161 162 164 167 168 171 173 174 176 179 181 182 185 186 188 191 192 193 194 196 199 200 203 205 206 208 211 213 214 217 218 220 223 224 227 229 230 233 234 236 239 241 242 244 247 248 251 253 254 257 258 259 260 261 262 264 265 266 268 271 272 273 274 276 279 280 283 285 286 288 289 290 292 295 296 299 301 302 304 307 309 310 313 314 316 319 320 321 322 324 327 328 331 333 334 336 339 341 342 345 346 348 351 352 355 357 358 361 362 364 367 369 370 372 375 376 379 381 382 384 385 386 388 391 392 395 397 398 400 403 405 406 409 410 412 415 416 419 421 422 425 426 428 431 433 434 436 439 440 443 445 446 448 451 453 454 457 458 460 463 465 466 468 471 472 475 477 478 481 482 484 487 488 491 493 494 496 499 501 502 505 506 508 513 514 515 516 517 518 520 521 522 524 527 528 529 530 532 535 536 539 541 542 544 545 546 548 551 552 555 557 558 560 563 565 566 569 570 572 575 576 577 578 580 583 584 587 589 590 592 595 597 598 601 602 604 607 608 611 613 614 617 618 620 623 625 626 628 631 632 635 637 638 640 641 642 644 647 648 651 653 654 656 659 661 662 665 666 668 671 672 675 677 678 681 682 684 687 689 690 692 695 696 699 701 702 704 707 709 710 713 714 716 719 721 722 724 727 728 731 733 734 737 738 740 743 744 747 749 750 752 755 757 758 761 762 764 768 769 770 772 775 776 779 781 782 784 787 789 790 793 794 796 799 800 803 805 806 809 810 812 815 817 818 820 823 824 827 829 830 832 835 837 838 841 842 844 847 849 850 852 855 856 859 861 862 865 866 868 871 872 875 877 878 880 883 885 886 889 890 892 896 899 901 902 905 906 908 911 913 914 916 919 920 923 925 926 929 930 932 935 936 939 941 942 944 947 949 950 953 954 956 961 962 964 967 968 971 973 974 976 979 981 982 985 986 988 992 995 997 998 1001 1002 1004 1009 1010 1012 1016 1025 1026 1027 1028 1029 1030 1032 1033 1034 1036 1039 1040 1041 1042 1044 1047 1048 1051 1053 1054 1056 1057 1058 1060 1063 1064 1067 1069 1070 1072 1075 1077 1078 1081 1082 1084 1087 1088 1089 1090 1092 1095 1096 1099 1101 1102 1104 1107 1109 1110 1113 1114 1116 1119 1120 1123 1125 1126 1129 1130 1132 1135 1137 1138 1140 1143 1144 1147 1149 1150 1152 1153 1154 1156 1159 1160 1163 1165 1166 1168 1171 1173 1174 1177 1178 1180 1183 1184 1187 1189 1190 1193 1194 1196 1199 1201 1202 1204 1207 1208 1211 1213 1214 1216 1219 1221 1222 1225 1226 1228 1231 1233 1234 1236 1239 1240 1243 1245 1246 1249 1250 1252 1255 1256 1259 1261 1262 1264 1267 1269 1270 1273 1274 1276 1280 1281 1282 1284 1287 1288 1291 1293 1294 1296 1299 1301 1302 1305 1306 1308 1311 1312 1315 1317 1318 1321 1322 1324 1327 1329 1330 1332 1335 1336 1339 1341 1342 1344 1347 1349 1350 1353 1354 1356 1359 1361 1362 1364 1367 1368 1371 1373 1374 1377 1378 1380 1383 1384 1387 1389 1390 1392 1395 1397 1398 1401 1402 1404 1408 1411 1413 1414 1417 1418 1420 1423 1425 1426 1428 1431 1432 1435 1437 1438 1441 1442 1444 1447 1448 1451 1453 1454 1456 1459 1461 1462 1465 1466 1468 1473 1474 1476 1479 1480 1483 1485 1486 1488 1491 1493 1494 1497 1498 1500 1504 1507 1509 1510 1513 1514 1516 1521 1522 1524 1528 1536 1537 1538 1540 1543 1544 1547 1549 1550 1552 1555 1557 1558 1561 1562 1564 1567 1568 1571 1573 1574 1577 1578 1580 1583 1585 1586 1588 1591 1592 1595 1597 1598 1600 1603 1605 1606 1609 1610 1612 1615 1617 1618 1620 1623 1624 1627 1629 1630 1633 1634 1636 1639 1640 1643 1645 1646 1648 1651 1653 1654 1657 1658 1660 1664 1667 1669 1670 1673 1674 1676 1679 1681 1682 1684 1687 1688 1691 1693 1694 1697 1698 1700 1703 1704 1707 1709 1710 1712 1715 1717 1718 1721 1722 1724 1729 1730 1732 1735 1736 1739 1741 1742 1744 1747 1749 1750 1753 1754 1756 1760 1763 1765 1766 1769 1770 1772 1777 1778 1780 1784 1792 1795 1797 1798 1801 1802 1804 1807 1809 1810 1812 1815 1816 1819 1821 1822 1825 1826 1828 1831 1832 1835 1837 1838 1840 1843 1845 1846 1849 1850 1852 1857 1858 1860 1863 1864 1867 1869 1870 1872 1875 1877 1878 1881 1882 1884 1888 1891 1893 1894 1897 1898 1900 1905 1906 1908 1912 1921 1922 1924 1927 1928 1931 1933 1934 1936 1939 1941 1942 1945 1946 1948 1952 1955 1957 1958 1961 1962 1964 1969 1970 1972 1976 1984 1987 1989 1990' ], +); -$n++, exists $primes{(sprintf '%b', $n)=~tr/1/1/} ? (say $n) : redo for 1..100; +is( "@{[ method_sprintf_coll( $_->[0] ) ]}", $_->[1] ) foreach @TESTS; +is( "@{[ method_bitshift( $_->[0] ) ]}", $_->[1] ) foreach @TESTS; + +done_testing(); +cmpthese( 200000, { + 'sprintf' => sub { method_sprintf_coll($_->[0]) for @TESTS }, + 'bitshift' => sub { method_bitshift($_->[0]) for @TESTS }, +}); +sub method_sprintf { + my($N,$n) = (shift,0); + $n++, exists $primes{(sprintf '%b', $n)=~tr/1/1/} ? say $n : redo for 1..$N; +} + +sub method_sprintf_coll { + my($N,$n,@res) = (shift,0); + $n++, exists $primes{(sprintf '%b', $n)=~tr/1/1/} ? push @res, $n : redo for 1..$N; + @res; +} + +sub method_bitshift { + my($N,$n,@res) = (shift,0); + for (1..$N) { + $n++; + my ($t,$m)=(0,$n); + do { $t+=$m&1 } while $m >>= 1; + redo unless exists $primes{$t}; + push @res, $n; + } + @res; +} |
