Left Truncatable Prime
solipsys.co.uk
solipsys.co.uk
Plotting the distribution gives a nice curve which you can model quite well with the prime number theorem. From memory it looks like x^2*e^-x. Then plotting in different bases is also interesting.
I first learned about truncatable primes from Project Euler problem 37: "Find the sum of the only eleven primes that are both truncatable from left to right and right to left."
https://projecteuler.net/problem=37
(I have some rust code that solves this problem in ~300 microseconds on my laptop. I bet it's possible to get it faster though. Anyone want to try?)
I also avoided any string manipulation, favoring truncating by using integer division for right truncation and modulus for left truncation.
Is that similar to your algorithm?
Mine was the brute-force "calculate all primes under a million then test them all" approach.
That’s why I don’t think speeding up such solutions beyond making it really fast using algorithmic changes/specializations is worthwhile.
I suppose an extra challenge would be to find values which are truncatable primes in more than one base at once... Hmmm.
[1] https://oeis.org/search?q=left+truncatable+primes+base&sort=...
And why does the sequence stop at 29? phi(30) = 8, setting a new minimum for phi(n)/n.
I didn't know about the third parameter in pow(), either. Quite interesting.
I should go looking ...
252601: 41 61 101
294409: 37 73 109
399001: 31 61 211
410041: 41 73 137
488881: 37 73 181
512461: 31 61 271
1152271: 43 127 211
1193221: 31 61 631
1461241: 37 73 541
1857241: 31 181 331I could write a more comprehensive test - didn't seem worth it for this case, but it's an interesting exercise. It's easy to extend this test so the list of primes used goes as far as the smallest factor in each of these factorisations, say, 109. Then these would all be correctly identified as composite. It would also be easy to combine it with Pollard Rho or PPP tests, to extend the range before we need to do something more serious.
Anyway, here are the first 30 with factorisations
252601 : 41 61 101
294409 : 37 73 109
399001 : 31 61 211
410041 : 41 73 137
488881 : 37 73 181
512461 : 31 61 271
1152271 : 43 127 211
1193221 : 31 61 631
1461241 : 37 73 541
1615681 : 23 199 353
1857241 : 31 181 331
1909001 : 41 101 461
2508013 : 53 79 599
3057601 : 43 211 337
3581761 : 29 113 1093
3828001 : 101 151 251
4335241 : 53 157 521
5049001 : 31 271 601
5148001 : 41 241 521
5444489 : 29 197 953
5481451 : 31 151 1171
5968873 : 43 127 1093
6189121 : 61 241 421
6733693 : 109 163 379
6868261 : 43 211 757
7519441 : 41 241 761
8134561 : 37 109 2017
8927101 : 31 37 43 181
9439201 : 61 271 571
9494101 : 23 61 67 101The primality test in the post (Fermat's test) is interesting to analyze though. BTW how did you generate your list of false positives? I didn't mention it earlier, but as a shortcut I used the list from https://oeis.org/A001567 (the base-2 pseudoprimes up to 10^12), and found that the function in the post has 2690 of these false positives less than 10^12. I wrote out only the first 10 above (after piping through `factor`). Of these 2690 false positives, these have the 5 largest smallest prime factors:
681719179681: 5743 8527 13921
802204143001: 5981 8971 14951
747734199031: 6791 8731 12611
949631589089: 6917 10193 13469
251994268081: 354961 709921
And indeed the `small_primes` used by the test needs to be extended up to the prime 6917, if we want to get no false positives at least for numbers up to 10^12. And even if we went that far, we'd encounter the next false positive soon after, at 1025414056801 (factors 7561 8101 16741) and then (if we included primes up to 7561) at 1196508858409 (factors 8059 11833 12547). (See https://oeis.org/A083876)I think compared to the Fermat test used in the post, the Miller-Rabin test is almost as easy to write (only a few lines longer), and it comes with stronger guarantees. Not only is it a test used in the real world (using it with just a few random bases gives a high degree of confidence), but it also comes with guarantees when a deterministic set of primes (like your idea) is used: for example with the first 13 primes (2 to 41), the first false positive is over 3.3 × 10^24 (https://en.wikipedia.org/w/index.php?title=Miller%E2%80%93Ra...). See also https://miller-rabin.appspot.com/
def is_probable_prime(n, num_random_trials=5):
"""
An implementation of the Miller-Rabin primality test.
If this function returns False, then n is definitely not prime.
If this function returns True, then n is probably prime:
the probability of n being composite is less than 1/4^(num_random_trials), and
nonzero only if n >= 3317044064679887385961981 > 3.3*10^24 > 1.3*2^81 (by https://arxiv.org/abs/1509.00864).
"""
assert n == abs(int(n)), 'Need a nonnegative integer, got %s' % n
if n < 2: return False
if n % 2 == 0: return n == 2
(s, d) = (0, n - 1)
while d % 2 == 0: (s, d) = (s + 1, d // 2)
assert n - 1 == (2**s) * d and d % 2 == 1
small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
for i in range(len(small_primes) + num_random_trials):
a = small_primes[i] if i < len(small_primes) else random.randrange(small_primes[-1], n - 1)
if a >= n: break
if not (pow(a, d, n) == 1 or any(pow(a, 2**j * d, n) == n - 1 for j in range(s))):
return False
return True> Yes the full program for left-truncatable primes happens to be fine ...
Yes - although it's a little lucky, but easy enough to run an extra confirmation.
> how did you generate your list of false positives?
Nothing clever - I literally wrote a slow trial division program, then ran through all integers up to some limit - 10^8 I think - and when the "is_prime" routine said it was prime I called the slow version to see if it was right. Machines are fast.
> the function in the post has 2690 of these false positives less than 10^12. ... Of these 2690 false positives, these have the 5 largest smallest prime factors:
> 681719179681: 5743 8527 13921
> 802204143001: 5981 8971 14951
> 747734199031: 6791 8731 12611
> 949631589089: 6917 10193 13469
> 251994268081: 354961 709921
That's cool.> And indeed the `small_primes` used by the test needs to be extended up to the prime 6917, if we want to get no false positives at least for numbers up to 10^12.
We can augment this with the Perrin Psuedo-Prime test, but Miller-Rabin is still probably the best way to go to do it "properly". If I re-wrote the material on the Perrin tests[0] I'd probably use M-R.
So we're pulling in the same direction, although making different trade-offs. And it's fun to play and see what happens.
[0] http://www.solipsys.co.uk/new/FindingPerrinPseudoPrimes_Part...
In this form of the problem, it is not the length, L, of the initial prime that we want to maximize but rather L-K.
My guess is that these looser requirements won't make much difference. Estimating based on the prime number theorem and pretending that primes are essentially random numbers (an assumption that works remarkably well a large fraction of the time), I get, if I didn't totally botch it, that given a k digit integer that is prime, there is about a 1/(2k) chance that you can add a digit on the left and still have a prime.
Another generalization comes to mind. Suppose that instead of a pencil, we are dealing with some item with break-away segment. Each segment has d digits printed on it, so when we have L segments we have a dL digit number. As we break away segments we left truncate this number by d digits.
I don't think that would make much of a difference. We can treat it as an L digit number in base 10^d, which we truncate a digit at a time. Another hand wavy application of prime number theorem gives 1/(2kd) as the chance that you can left-extend a k digit base 10^d prime by one digit and get a prime.
Are there infinitely many 0-enabled left-truncatable primes?
Suppose we start with a prime p which is d digits long (say p=3 so that d=1), and consider the sequence {a10^k + p : 1≤a≤9, k ≥ d}. So for p=3, we would consider 13, 23, … 93, then 103, 203, … 903, then 1003, 2003, … 9003, etc. By a standard heuristic argument (based on the prime number theorem), the number n is prime with "probability" about 1/log n, so each a10^k + p will be prime with probability about 1/k (ignoring some constant factors). The sum of these probabilities diverges, so one can be very confident that there will be a prime number in this sequence. In other words, we can be extremely confident of the following lemma: given any prime number p, there is a way of extending it to another prime number by adding some digit (followed by 0s) to its left.
Repeated applications of this lemma (with any starting prime p, or even different primes p if you wish) will give the result. For example starting with the prime p=3 and taking the first choice each time, you get primes like 30007020009000000000000000600000000900906001600500006000001026001050612113.
Of course, turning this heuristic argument from "extremely confident" to an actual proof is a challenge. (There may be ways to prove this, I don't know: I know this heuristic is often used by number theorists though.)
Ranges in Python don't include their endpoint. A few people have expressed confusion as to this choice in the design, but it means that the expression:
range(a,b) + range(b,c) + range(c,d)
includes everything from a to d-1 one and only once. Knuth said that intervals should include the initial point and not the endpoint for this reason.Regular ranges include their endpoints, and you can think of ^ as "not including" or "up to (exclusive)"
1..5 is 1, 2, 3, 4, 5
1..^5 is 1, 2, 3, 4
1^..5 is 2, 3, 4, 5
1^..^5 is 2, 3, 4
There's also convenient shorthand for 0..^n (useful if you want to do something n times): ^n
◄ TOO COOL TO DO DRUGS
◄ COOL TO DO DRUGS
◄ DO DRUGS
◄ DRUGS
http://www.nytimes.com/1998/12/12/nyregion/slogan-causes-pen...Someone has even done a remake of the pencil:
(edit) Now edited - thanks again.
Heck, I didn't even expect a mention of my nom de bit.
I was just happy to be able to help you make a fun story even more fun!
◄ TOO COOL TO CARE ABOUT LINKS
◄ COOL TO CARE ABOUT LINKS
◄ CARE ABOUT LINKS
◄ LINKS> I certainly wasn't offended by not having a link to the specific comment.
“It is amazing what you can accomplish if you do not care who gets the credit.” ― Harry Truman
Long may you accomplish many things, and I hope the credit sticks at least occasionally.