I would like to be able to solve logic puzzles such as the one described here using Mathematica.
I have copied & pasted the listed example below to save on jumping about with hyperlinks:
- The number is prime.
- No digits are repeated.
- $A\cdot B=C\cdot D$
- The first digit is greater than 3
This does it
li = Prime[Range[PrimePi[4*10^3], PrimePi[10^4]]];
q = IntegerDigits[li[[n]]];
mi = Rest@DeleteCases[Table[If[q[[1]]q[[2]]==q[[3]]q[[4]],li[[n]],0],
{n,1,Length@li}],0];
ni = DeleteCases[Table[If[Length@Complement[IntegerDigits[mi[[n]]]]==4,mi[[n]],0],
{n,1,Length@mi}],0]
but certainly isn't the best approach, especially for more complex problems.
I tried thing like this:
FindInstance[{Element[FromDigits[{a, b, c, d}], Primes], a != b != c != d,
10^3 <= FromDigits[{a, b, c, d}] <= 10^4, a > 3, b > 0, c > 0, d > 0, a*b == c*d},
{a, b, c, d}, 1]
but it didn't work, and I am not sure why.
I was wondering whether I could use Rojo's method for defining a set as a starting point, but again, not sure what is the best approach.
Update
So far:
li = Prime[Range[PrimePi[4*10^3], PrimePi[10^4]]];
q = IntegerDigits[li[[n]]];
mi = Rest@ DeleteCases[Table[If[q[[1]] q[[2]] == q[[3]] q[[4]], li[[n]], 0],
{n, 1, Length@li}], 0];
DeleteCases[Table[If[Length@Complement[IntegerDigits[mi[[n]]]] == 4, mi[[n]], 0],
{n, 1, Length@mi}], 0] // Timing
({0., {6329}})
pred[a_, b_, c_, d_] :=
And @@ {FromDigits@{a, b, c, d} [Element] Primes, a != b != c != d,
a b == c d, a > 3};
Select[Range[1000, 9999], pred @@ IntegerDigits[#] &] // Timing
({0.218401, {6329}})
Select[IntegerDigits@Prime[Range[1, PrimePi[10^4]]],
Length@Union@# == 4 &&(4 different digits)
Equal @@ Times @@@ Partition[#, 2] &&(the product condition)#[[1]]
> 3 & (#[[1]]>3)] // Timing
({0.031200, {{6, 3, 2, 9}}})
Select[Flatten[
Table[(10^3 a + 10^2 b + 10 c + d)*
Boole[a != b && a != c && a != d && b != c && b != d && c != d &&
a b == c d], {a, 4, 9}, {b, 0, 9}, {c, 0, 9}, {d, 0, 9}], 3],
PrimeQ[#] &] // Timing
({0.015600, {6329}})
Select[Most@NestWhileList[NextPrime, 4000, # < 10000 &],
Function[Block[{a, b, c, d}, {a, b, c, d} = IntegerDigits[#];
And @@ {a b == c d, a != b != c != d}]]] // Timing
({0.031200, {6329}})
pred[a_, b_, c_, d_] := And @@ {FromDigits@{a, b, c, d} ∈ Primes, a != b != c != d, a b == c d, a > 3}; Select[Range[1000, 9999], pred @@ IntegerDigits[#] &]– Oct 25 '14 at 12:12FindInstancedoesn't work? – martin Oct 25 '14 at 12:14FindInstanceis a black box full of elves as far as I'm concerned, so no. – Oct 25 '14 at 14:18