A positive integer satisfies the following conditions:
As an example, the number 43797 satisfies above conditions.
What is the largest positive integer that satisfies both of these conditions?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Each digit of the number after the first is 1 , 3 , 7 , 9 that means that the list of 2-digit primes after the first one is 1 1 , 1 3 , 1 7 , 1 9 , 3 1 , 3 7 , 7 1 , 7 3 , 7 9 , 9 7 . The number can be at most 1 2 digits long. Making the list of the mentioned above 1 0 prime numbers and comparing the number of occurrences of digits 1 , 3 , 7 , 9 one can conclude that the largest number is 6 1 9 7 3 7 1 3 1 1 7 9 , which is also a prime number.
Note: This question is taken from University of Waterloo Mathematics Contest.
Just a plain bash method in Objective-C :
Seeing that the primes must be in order, we create an array with 9 arrays inside holding all primes sorted by starting digit. Using recurrence the program sorts through these and generates every unique combination then just picks the largest. Because arrays are pointer, bear in mind that when copying arrays of arrays, making a deep copy is the only way to avoid pointing to the same memory location from the "copy".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Solution in python 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Output
1 |
|
Problem Loading...
Note Loading...
Set Loading...
First, note that the last digit of a prime number greater than 5 must be one of 1, 3, 7, or 9. (If it's 0, 2, 4, 6, or 8, then it's divisible by 2; if it's 0 or 5, then it's divisible by 5. Since the number is greater than 5, they can't be 2 or 5 themselves, so they have a nontrivial divisor and thus not prime.) This means, except for the first digit, all the digits are 1, 3, 7, or 9.
Construct a directed graph with vertices 1, 3, 7, 9, and arcs from i to j indicating that the two-digit number i j is a prime number. (That is, the arcs are ( 1 , 1 ) , ( 1 , 3 ) , ( 1 , 7 ) , ( 1 , 9 ) , ( 3 , 1 ) , ( 3 , 7 ) , ( 7 , 1 ) , ( 7 , 3 ) , ( 7 , 9 ) , ( 9 , 7 ) .) Now we can interpret our number as a trail (walk that doesn't repeat an arc) on this graph: if the number is d 0 d 1 d 2 … d k , then d 1 , d 2 , … , d k is a trail, because we need each of ( d 1 , d 2 ) , ( d 2 , d 3 ) , … , ( d k − 1 , d k ) to be arcs on the graph, and none of them may repeat.
There are 10 arcs in the graph, so k − 1 ≤ 1 0 , or k ≤ 1 1 , giving a 12-digit number (the first digit d 0 has index 0). Can this be achieved?
It turns out that we can. To arrive at this result, we can first notice that if we want k − 1 = 1 0 , then we need a path that traverses all the arcs; that is, an Eulerian trail. Luckily, this is possible: the in-degree and the out-degree of all vertices are equal, except 1 where the out-degree is larger by 1, and 9 where the in-degree is larger by 1, thus there is an Eulerian trail. All such trails will start from 1 (the one with larger out-degree) and end at 9 (the one with larger in-degree). One such example that can be found by hand is 1-1-3-1-7-1-9-7-3-7-9. This will give d 1 = 1 , so we can pick d 0 = 4 (6 also works), giving the number 411317197379, proving that k − 1 = 1 0 is achievable. There are other trails and other choices of d 0 ; any of them works, because we only need to show existence.
We know Eulerian trails exist on this graph, and that they start at 1 and end at 9. Thus d 1 = 1 , d 1 1 = 9 . To obtain the largest possible number, we need to maximize d 0 ; among those with largest d 0 , we need to maximize d 2 ; among those with largest d 2 , we need to maximize d 3 , and so on. In other words, we greedily select the largest possible digit, but making sure that we can still complete the trail.
That gives the number 619737131179 , which is our answer.