Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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
2  
@user116988 In that case, you should use the homework tag. –  becko Dec 20 '13 at 13:59
1  
What's n? It should be p, right? –  becko Dec 20 '13 at 14:07

1 Answer 1

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.

share|improve this answer
4  
999 is not a prime –  becko Dec 20 '13 at 14:06
1  
@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  
+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
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.