# Crack the Code #3

You return to the bench after cracking the last code, and you notice an envelope on the ground directly under it. You put down your envelope with the previous code and pick up the new one, anticipating another challenge.

Crack the Code #2

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 number is prime.
• No digits are repeated.
• $A\cdot B=C\cdot D$
• The first digit is greater than 3.

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. If you find the answer, put it in a spoiler so the question is not ruined for those who want to solve it.

• 2 possible solutions: 6329 and 9263... May 21, 2016 at 16:49
• @ΈρικΚωνσταντόπουλος Again, 9263 is composite. Check your numbers before commenting. May 21, 2016 at 16:50
• How is it composite? May 21, 2016 at 16:51
• @ΈρικΚωνσταντόπουλος 59*157 = 9263 May 21, 2016 at 16:52
• Oh, I thought it cannot be divided by 2, 3, 5 or 7. May 21, 2016 at 16:52

Do not hover over the below unless you wish to know the answer...

6329

I cheated and wrote a small program to do the hard work for me :p which I'll post in a moment - I'm a lazy software developer and we never do any un-necessary maths.

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{

public static boolean isPrime(long n)
{
if(n < 2) return false;

if(n == 2 || n == 3) return true;

if(n%2 == 0 || n%3 == 0) return false;

long sqrtN = (long)Math.sqrt(n)+1;

for(long i = 6L; i <= sqrtN; i += 6)
{
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}

return true;
}

public static void main (String[] args) throws java.lang.Exception
{
int a;
int b;
int c;
int d;
int combined;

for(a = 4; a < 10; a++)
{
for(b = 0; b < 10; b++)
{
for(c = 0; c < 10; c++)
{
for(d = 0; d < 10; d++)
{
if (a != b && a != c && a !=d && b != c && b != d && b != d && c != d)
{
if(a*b == c*d)
{
combined = (((a * 10 + b) * 10 + c) * 10) + d;
if(isPrime(combined))
{
System.out.println(combined);
}
}

}
}
}
}
}
}
}

• Nice. Java is pretty verbose with these formulas though. I used javascript (shh) to check this, and my code's pretty short. Oct 23, 2014 at 23:43
• Yeah, I considered some code golf, but I figured in this case it's easier to see with some nice verbose code Oct 23, 2014 at 23:49
• I tried this in Ruby: require 'prime' (4..9).to_a.product((1..9).to_a,(1..9).to_a,(1..9).to_a).select{|num| num.uniq.length == 4 && num*num == num*num && Prime.prime?(num.join.to_i)}. I also tried with 6 digits; A.B == C.D == E.F has no solutions, but A.B.C == D.E.F has 9 solutions. Oct 24, 2014 at 7:13

6329

How I found the answer with mental arithmetic only.

First I realized the two sides must have the same set of prime factors, and since they are multiples of two one digit numbers those primes are restricted to being one of 2,3,5, or 7. We can discard 5 and 7 because they cannot be multiplied by any non unit and result in a digit, so they would have to be repeated on both sides were they factors. Therefore the answer must consist only of the primes 2 and 3. since we know digits are not repeated, we must have > 2 factors and since 2^4 is greater than one digit we must have exactly 3 prime factors. The two choices are 2,2,3 and 2,3,3. (2*3)3 == 2(3*3) is the solution that matches the requirements.

• I like this far better, despite being a programmer. Oct 27, 2014 at 17:16

I commented the code so that it is easy to read for people new to programming languages. It is written in Java 8 and can only be executed by Java 8.

public class SecondCodeCrackerProblem implements SingleSolution<Integer> {

public static void main(String[] args) {
SingleSolution<Integer> secondCodeCrackerProblem = new SecondCodeCrackerProblem();
System.out.println(secondCodeCrackerProblem.solveSingle());
}

@Override
public Integer solveSingle() {
return IntStream.rangeClosed(4000, 9999).parallel() // The first digit is greater than 3.
.filter(this::prime) // The number is prime.
.filter(value -> {
List<Integer> s = seperate(value);
if (a(s) != b(s) && a(s) != c(s) && a(s) != d(s) && b(s) != c(s) && b(s) != d(s) && c(s) != d(s)) {
return true;
}
return false;
}) // No digits are repeated.
.filter(value -> {
List<Integer> s = seperate(value);
if (a(s) * b(s) == c(s) * d(s)) {
return true;
}
return false;
}) // A⋅B = C⋅D
.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);}
}


6329