You are on page 1of 2

Problem 1 Sum of Truncatable Primes

Observation: For a truncatable prime, both the right-most digit and the left-most digit must always be a single digit prime number. For a truncatable prime, 2 and 5 can only appear as the left-most digit, because a number ending with 2 or 5 no longer remains a prime. Hence the right-most digit must be either 3 or 7. For a left truncatable prime, one element among {1,2,3,5,7,9} only can appear as the left most digit. Explanation of the Idea: We need to build an array of left truncatable primes. We do this by finding the 2-digit left truncatable prime first, then 3-digit left truncatable prime and so on. We maintain a list of left truncatable primes LTList(say) but consider only 3 and 7 as 1 digit left truncatable prime. We maintain a list L={1,2,3,5,7,9} . Besides, we maintain a variable 'sum', initialized to zero that will finally contain the sum of all the 11 truncatable primes. We also maintain 2 variables low and high which denote the upper and lower limits of the sub-list of LTList consisting of the left truncatable primes with the current number of digits. So initially, 1 digited left truncatable primes are considered and low=0 and high=1 Now , to the left of each of the elements LTList, between low to high, we add each of the elements of the list L (one at a time) and check whether the resulting number is a prime. If not discard it. If yes, then this new number is automatically a left truncatable prime and we add it to the LTList. If this new number is also right truncatable , then we add this new number to 'sum'. We continue until we have found the 11 truncatable primes. Algorithm; 1. 2. 3. 4. 5. Initialize an array LTList (say) containing only 2 elements 3 and 7. Initialize count to 0, sum to 0 Initialize a variable 'low' to 0 and 'high' to 1. /*Generate the 2-digit truncatable primes*/ Initialize the list L to {1,2,3,5,7,9} For each element of LTList between low to high for each element of L add the element of L to the left of the element of LTList if the new number is a prime add it to the list LTList if the new number is a right truncatable prime

add new number to 'sum' increment count if count==11 goto step 8 6. Re assign low and high to reflect the new limits of the sub-array of LTLIst containing the current-digited truncatable primes. 7. Go to step 5. 8. Exit. As example, start with LTList={3,7} I. II. III. IV. V. VI. VII. Add 1 to the left of 3 and get 13. 13 is a prime. Add 13 to LTList Similarly we get 17,23,27,33,37,53,57,73,77,93,97 and add 17,23,37,53,73,97 to LTList . Since 23,37,53,73 are also right truncatable, so add each of them to 'sum'. At this point we have found all the 2-digited left truncatable primes and thereby all the truncatable primes . Now we have count=4. So we make low=2 and high=8. We continue the same process until we have reached count =11.

You might also like