Thursday, July 30, 2009

Write C/C++ program whre input will an integer N ≤ 100000 and output M ≥ N such that M is a Palindrome Prime.

An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes.





Write the Algorithm or C program whre input will an integer N, 1 ≤ N ≤ 1000000 and output should be the smallest integer M ≥ N such that M is a prime number and M is a palindrome.


For example, if N is 31 then the answer is 101.

Write C/C++ program whre input will an integer N ≤ 100000 and output M ≥ N such that M is a Palindrome Prime.
palindrome :: Eq a =%26gt; [a]-%26gt;Bool


palindrome (p:[]) = True


palindrome (p:ps) = p == head (reverse ps) %26amp;%26amp; palindrome (reverse(tail(reverse ps)))


prime n = not (factors 2 n)


factors m n | m == n = False | m %26lt; n = divides m n || factors (m+1) n


divides a b = (mod a b == 0)





This bit of Haskell tells you if a list is a palindrome and a number is a prime.


i.e. palindrome [1,2,1] = true


prime 2 = true


I was just about to knock it together to make you a 10 line solution but then I realized haskell has no type conversion. Meh. Maybe what I've written will help.
Reply:#include %26lt;iostream.h%26gt;





main()


{


cout %26lt;%26lt; "Hello World!";


return 0;


}





Now do your own homework :P
Reply:Since there are only 114 answers, the fastest way would be a lookup table. Generating the contents of the table would be another matter, the first 113 numbers can be found at the first link in "sources" below, but 114'th seems harder to find, But since a program to do that would only have to be run once, it would not have to be very efficient, simply checking every possible integer for for being a palindrome and then, if it is, is it prime, until you find one %26gt; 100000 (or 1003001) might do. I wrote a crude little program in interpreted BASIC that checked every integer between 2 and 1,005,000 in about two seconds.





I suppose that actually the main object of the assignment is to actually do the necessary calculation in real time, but even for that, avoiding that last 1003001, my crude, interpreted, 16 bit "DOS" BASIC program can find 98689 in a third of a second, a complied program should be able to do considerably better





long prime_palindromes[] = {2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929, 10301, 10501, 10601, 11311, 11411, 12421, 12721, 12821, 13331, 13831, 13931, 14341, 14741, 15451, 15551, 16061, 16361, 16561, 16661, 17471, 17971, 18181, 18481, 19391, 19891, 19991, 30103, 30203, 30403, 30703, 30803, 31013, 31513, 32323, 32423, 33533, 34543, 34843, 35053, 35153, 35353, 35753, 36263, 36563, 37273, 37573, 38083, 38183, 38783, 39293, 70207, 70507, 70607, 71317, 71917, 72227, 72727, 73037, 73237, 73637, 74047, 74747, 75557, 76367, 76667, 77377, 77477, 77977, 78487, 78787, 78887, 79397, 79697, 79997, 90709, 91019, 93139, 93239, 93739, 94049, 94349, 94649, 94849, 94949, 95959, 96269, 96469, 96769, 97379, 97579, 97879, 98389, 98689, 1003001};


No comments:

Post a Comment