Re: More 41 birthday fun. Message #68 Posted by Thomas Klemm on 2 Aug 2009, 2:39 p.m., in response to message #65 by Thomas Klemm
Quotations
Quote:
although 49 is a given max for the problem here
Quote:
02 3.04902
03 STO 00
I wasn't aware of this fact. But I realized that there are no solutions bigger than 41.
That's why I considered that as an upper limit. From now on I will do my timing based on 49. IIRC the Swiss lottery used 6 out of 42.
Now they are using 45.
Quote:
 as n is odd, x^{2}x + n is also always odd. So the primality test does not check for 2
That saved nearly 100 tests I guess. So my list is starting now with 3, 5, 7, ...
Quote:
04 2
05 VIEW X
I consider that a little cheating. But I'm sure I can use that trick as well since 2 and 3 are missing now in my solution.
Quote:
 as digits are the slowest things on the 41, often used numbers are stored in registers
Quote:
STO a ;(3)
I wasn't aware that I could use register a without harm. Or probably I just forgot about that.
But since I'm not using subroutines I'm on the safe side. Using register P is probably a bad idea, I guess.
Quote:
Another optimisation would be to only test primes instead of odd numbers.
Not exactly what I did but a little closer. I'm using only 17 numbers instead of 24 of which only 25, 35 and 49 are composite.
Quote:
Lastly, I think that starting the x^{2}x+n test with n1 is advantageous.
It is the largest number to be tested for prime and hence has the highest chance to be not prime, at which point no further tests have to be done.
I'm not sure whether I understood that correctly but if you suggest to count x down from n1 to 0
then there are certain cases where you start testing a big prime but you could find the smallest
composite much faster.
E.g. n = 37
ascending:
37 is a prime, therefore you have to check 3 and 5 to find out
39 is composite, divisible by 3
3 checks needed to reject 37
descending:
1297 is a prime, therefore you have to check 3, 5, 7, ..., 35 to find out
1227 is composite, divisible by 3
18 checks needed to reject 37
Here are the other cases:
7: 37
15: 197
21: 401
25: 577
27: 677
However as you may see from the following table there's only one case (namely 29) where you'd have to check more than on prime
prior to find the first composite. Primes are marked with a sharp #.
3: 3# 5#
5: 5# 7# 11# 17#
7: 7# 9 13# 19# 27 37#
9: 9 11# 15 21 29# 39 51 65
11: 11# 13# 17# 23# 31# 41# 53# 67# 83# 101#
13: 13# 15 19# 25 33 43# 55 69 85 103# 123 145
15: 15 17# 21 27 35 45 57 71# 87 105 125 147 171 197#
17: 17# 19# 23# 29# 37# 47# 59# 73# 89# 107# 127# 149# 173# 199# 227# 257#
19: 19# 21 25 31# 39 49 61# 75 91 109# 129 151# 175 201 229# 259 291 325
21: 21 23# 27 33 41# 51 63 77 93 111 131# 153 177 203 231 261 293# 327 363 401#
23: 23# 25 29# 35 43# 53# 65 79# 95 113# 133 155 179# 205 233# 263# 295 329 365 403 443# 485
25: 25 27 31# 37# 45 55 67# 81 97# 115 135 157# 181# 207 235 265 297 331# 367# 405 445 487# 531 577#
27: 27 29# 33 39 47# 57 69 83# 99 117 137# 159 183 209 237 267 299 333 369 407 447 489 533 579 627 677#
29: 29# 31# 35 41# 49 59# 71# 85 101# 119 139# 161 185 211# 239# 269# 301 335 371 409# 449# 491# 535 581 629 679 731 785
31: 31# 33 37# 43# 51 61# 73# 87 103# 121 141 163# 187 213 241# 271# 303 337# 373# 411 451 493 537 583 631# 681 733# 787# 843 901
33: 33 35 39 45 53# 63 75 89# 105 123 143 165 189 215 243 273 305 339 375 413 453 495 539 585 633 683# 735 789 845 903 963 1025
35: 35 37# 41# 47# 55 65 77 91 107# 125 145 167# 191# 217 245 275 307# 341 377 415 455 497 541# 587# 635 685 737 791 847 905 965 1027 1091# 1157
37: 37# 39 43# 49 57 67# 79# 93 109# 127# 147 169 193# 219 247 277# 309 343 379# 417 457# 499# 543 589 637 687 739# 793 849 907# 967# 1029 1093# 1159 1227 1297#
39: 39 41# 45 51 59# 69 81 95 111 129 149# 171 195 221 249 279 311# 345 381 419# 459 501 545 591 639 689 741 795 851 909 969 1031# 1095 1161 1229# 1299 1371 1445
41: 41# 43# 47# 53# 61# 71# 83# 97# 113# 131# 151# 173# 197# 223# 251# 281# 313# 347# 383# 421# 461# 503# 547# 593# 641# 691# 743# 797# 853# 911# 971# 1033# 1097# 1163# 1231# 1301# 1373# 1447# 1523# 1601#
43: 43# 45 49 55 63 73# 85 99 115 133 153 175 199# 225 253 283# 315 349# 385 423 463# 505 549 595 643# 693 745 799 855 913 973 1035 1099 1165 1233 1303# 1375 1449 1525 1603 1683 1765
45: 45 47# 51 57 65 75 87 101# 117 135 155 177 201 227# 255 285 317# 351 387 425 465 507 551 597 645 695 747 801 857# 915 975 1037 1101 1167 1235 1305 1377 1451# 1527 1605 1685 1767 1851 1937
47: 47# 49 53# 59# 67# 77 89# 103# 119 137# 157# 179# 203 229# 257# 287 319 353# 389# 427 467# 509# 553 599# 647# 697 749 803 859# 917 977# 1039# 1103# 1169 1237# 1307# 1379 1453# 1529 1607# 1687 1769 1853 1939 2027# 2117
49: 49 51 55 61# 69 79# 91 105 121 139# 159 181# 205 231 259 289 321 355 391 429 469 511 555 601# 649 699 751# 805 861 919# 979 1041 1105 1171# 1239 1309 1381# 1455 1531# 1609# 1689 1771 1855 1941 2029# 2119 2211 2305
So I agree with you that chances are lower that bigger numbers are primes.
However if you run into one it takes much longer to prove it is one.
Listing
01 LBL A 24 LBL B 47 /
02 E3 25 E3 48 INT
03 / 26 STO a 49 RCL a
04 STO M 27 / 50 /
05 2 28 STO M 51 STO O
06 ENTER 29 LBL 02 52 LBL 04
07 X^2 30 RCL IND M 53 RDN
08 3 31 RCL X 54 RCL IND O
09 STO IND M 32 2 55 MOD
10 ISG M 33  56 X=0?
11 LASTX 34 RCL a 57 GTO 05
12 + 35 / 58 ISG O
13 GTO 00 36 STO N 59 GTO 04
14 LBL 01 37 RDN 60 RDN
15 RCL Z 38 LBL 03 61 ISG N
16 X<>Y 39 RCL N 62 GTO 03
17 R^ 40 INT 63 VIEW IND M
18 + 41 ST+ X 64 LBL 05
19 LBL 00 42 + 65 ISG M
20 STO IND M 43 ENTER 66 GTO 02
21 ISG M 44 ENTER 67 END
22 GTO 01 45 SQRT
23 RTN 46 3
Timing
16 XEQ A: 0'02.06"
16 XEQ B: 1'27.56"
Conclusions
I realized that my calculation of the upper bound in lines 4548 could be improved:
45 SQRT
46 1
47 
48 3
49 /
50 INT
This would save around 30 tests in the innermost loop.
But since these lines are executed about 90 times the saving of time is nullified.
What I might try is to reverse all the indexes and use DSE instead of ISG.
This would save the division by E3. But then I'd have to solve the problem that 0 is skipped.
Just wanted to thank you for all the contributions so far. They have been a great help.
Nevertheless I don't think that I can beat Peter's solution. It seems that caching the
numbers isn't suitable in this situation. Most of the numbers can be rejected by simply
trying 3, 5 or 7 as divisors. But for exactly these cases my list doesn't differ from
what he's using.
Best regards
Thomas
