Let S[p] denote the sum of digits of p. A prime p is said to be stubborn if none of S[n] + 1, S[n], S[n] - 1, S[n] - 2 , or S[n] - 3 is a prime. Write a mathematica code that finds the smallest stubborn prime and tell which prime is it?

-
Hi there! Have you written the code for S yet? –  cormullion Dec 20 '13 at 13:45
Is it homework? –  belisarius Dec 20 '13 at 13:49
yeah in fact it is a homework :\$ –  user116988 Dec 20 '13 at 13:55
@user116988 In that case, you should use the homework tag. –  becko Dec 20 '13 at 13:59
What's n? It should be p, right? –  becko Dec 20 '13 at 14:07

Here is a direct approach:

digitsum = Composition[Total, IntegerDigits];

stubbornQ = PrimeQ[#] && Nor @@ PrimeQ[digitsum[#] - {1, 2, 3, 0, -1}] &;

i = 1;
While[! stubbornQ[++i]]
i


8999

This was quite sufficient in this case. For larger search spaces see Iterate until condition is met.

-
999 is not a prime –  becko Dec 20 '13 at 14:06
@becko lol -- well, I couldn't be bothered to read someone else's homework too closely. :o) –  Mr.Wizard Dec 20 '13 at 14:07
+1 for demonstration of Composition usage. –  Chris Degnen Dec 20 '13 at 14:11
@Chris Thank you. I tried to show several different styles in this short bit of code hoping it would be instructive to work through. –  Mr.Wizard Dec 20 '13 at 14:13
@belisarius and Anon; I thought about including additional optimizations but I decided to leave room for improvement for the OP. –  Mr.Wizard Dec 20 '13 at 14:43