Crack the Code #2

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

Clues

• 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?

• 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. Commented May 21, 2016 at 13:17
• Actually, 5431, 5321 and 4321 are all possible solutions (yep, miscalculation from over a year ago :P) Commented May 21, 2016 at 16:34
• @ΈρικΚωνσταντόπουλος 5321 and 4321 are both composite numbers. Commented May 21, 2016 at 16:36
• @mdc32 composite??? I have $mod$ed the numbers with $2, 3, 5, 7$ to make sure they ARE prime...weird THEY MUST BE PRIME!!! Commented May 21, 2016 at 16:39

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

$$5431$$

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

$$3210,4210,4310,4320,4321,5210,5310,5320,5321,5410,5420,5421,5430,5432$$

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

$$4321=29\cdot149,5321=17\cdot313,5421=3\cdot1807$$

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

5431

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();
System.out.println(codeCrackerProblem.solveSingle());
}

@Override
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.
.findFirst()
.orElse(0);
}

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

private List<Integer> seperate(Integer n) {
List<Integer> result = new ArrayList<>();
while (n > 0) {
n = n / 10;
}
Collections.reverse(result);
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);}
}


5431

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]:
print(number)


This prints 5431

• 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. Commented Oct 23, 2014 at 21:16
• 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? Commented Oct 23, 2014 at 23:52
• @Jon I meant this wasn't providing anything new to the question, seeing as all it provides is a way to obtain the answer. Commented Oct 24, 2014 at 0:38
• 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 Commented Oct 24, 2014 at 0:57
• That's the program for Crack the Code #1, isn't it? Commented May 21, 2016 at 16:14