After the last bank encounter, you decide that you could make a living off of just cracking codes (as if robbing banks wasn't enough). A shady man meets you on a park bench and slides you an envelope, dramatically. It contains clues for the next number.

Crack the Code #1

Digits are referred to as A-B-C-D in the clues. "A + B" is the sum of the first and second digit. All math follows the standard order of operations


  • The digits are in descending order.
  • The number is prime.
  • The first digit is less than 6.

What four digit number matches these criteria? Also, if you want, post your methodology for finding the correct answer, as this will help me in the future.

Note: I am pretty sure that only one number matches all these clues. However, I may have miscalculated. Please correct me in the comments.

Another note: YenTheFirst, you could probably solve this pretty easily, seeing as the other one was pretty fast. This is great, but can you put the answer in a spoiler this time if you solve it?

  • $\begingroup$ I wonder if you could make this puzzle harder by making one (or more) of the clues false, thus requiring solvers to deduce which clue(s) is/are false before finding the correct solution. $\endgroup$ Commented May 21, 2016 at 13:17
  • $\begingroup$ Actually, 5431, 5321 and 4321 are all possible solutions (yep, miscalculation from over a year ago :P) $\endgroup$
    – EKons
    Commented May 21, 2016 at 16:34
  • $\begingroup$ @ΈρικΚωνσταντόπουλος 5321 and 4321 are both composite numbers. $\endgroup$
    – mdc32
    Commented May 21, 2016 at 16:36
  • $\begingroup$ @mdc32 composite??? I have $mod$ed the numbers with $2, 3, 5, 7$ to make sure they ARE prime...weird THEY MUST BE PRIME!!! $\endgroup$
    – EKons
    Commented May 21, 2016 at 16:39

4 Answers 4


Assuming you mean strictly descending order, there's only one such number:


The only other numbers satisfying clue 1 and 3 simultaneously are:


Clearly, numbers ending in $0$ or $2$ cannot be prime. This leaves:


  • $\begingroup$ I'm not sure if you answered this first or if Penguino did, but your answer is more specific. $\endgroup$
    – mdc32
    Commented Oct 23, 2014 at 12:28
  • $\begingroup$ I was 166 seconds faster. You can see the UTC time stamp by hovering over answered x hours/days ago. $\endgroup$
    – Dennis
    Commented Oct 23, 2014 at 12:33

Only possible answer is (spoiler ahead)


It is pretty easy to calculate on the back of an envelope as most of the 9 possible contenders are even or divisible by 3.


This program solves the problem using Java 8. It is commented throughout for people to understand.

public class CodeCrackerProblem implements SingleSolution<Integer> {

    public static void main(String[] args) {
        SingleSolution<Integer> codeCrackerProblem = new CodeCrackerProblem();

    public Integer solveSingle() {
        return IntStream.rangeClosed(1000, 5999).parallel() // The first digit is less than 6.
            .filter(this::prime) // The number is prime
            .filter(value -> {
                List<Integer> s = seperate(value);
                return (a(s) > b(s) && b(s) > c(s) && c(s) > d(s));
            }) // The digits are in descending order.

    private boolean prime(int n) {
        if (n%2==0) return false;
        for(int i=3;i*i<=n;i+=2) {
                return false;
        return true;

    private List<Integer> seperate(Integer n) {
        List<Integer> result = new ArrayList<>();
        while (n > 0) {
            result.add(n % 10);
            n = n / 10;
        return result;

    private Integer a(List<Integer> l) {return l.get(0);}
    private Integer b(List<Integer> l) {return l.get(1);}
    private Integer c(List<Integer> l) {return l.get(2);}
    private Integer d(List<Integer> l) {return l.get(3);}



Here you have a programme you can you to check all the possibilities (Python 3.x)

for A in range(7,10):
    for B in range(0,10):
        for C in range(1,10):
            for D in range(0,10):
                if sum([A,B,C,D]) > ((A*B)-(C*D)):
                    prod_and_sum = (A*B*C*D) + sum([A,B,C,D])
                    if prod_and_sum % A == 0 and prod_and_sum % C == 0:
                        number = A*(10**3) + B*(10**2) + C*(10**1) + D
                        if not [i for i in range(2,number) if number % i == 0]:

This prints 5431

  • $\begingroup$ This isn't the right program. Also, posting programs such as these isn't really beneficial to the original question, or to people viewing it, as most people don't know Python syntax or programming syntax in general. The question was already answered before this too, so this is not a brand new answer - just a way to find it. $\endgroup$
    – mdc32
    Commented Oct 23, 2014 at 21:16
  • 1
    $\begingroup$ Some of us enjoy solving puzzles using computer code, others enjoy doing them with mental arithmetic or the back of an envelope. Surely all are of merit, as long as we all use spoiler tags so as not to ruin the fun? $\endgroup$
    – Jon Story
    Commented Oct 23, 2014 at 23:52
  • $\begingroup$ @Jon I meant this wasn't providing anything new to the question, seeing as all it provides is a way to obtain the answer. $\endgroup$
    – mdc32
    Commented Oct 24, 2014 at 0:38
  • 1
    $\begingroup$ I see where you're coming from, but I still think it's relevant. Perhaps not the best answer, but worth retaining as an 'also ran' as some readers may be interested in the techniques used for their own code solving problems $\endgroup$
    – Jon Story
    Commented Oct 24, 2014 at 0:57
  • $\begingroup$ That's the program for Crack the Code #1, isn't it? $\endgroup$
    – M Oehm
    Commented May 21, 2016 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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