2
$\begingroup$

I'm looking for a number which is the same when you read it reverse like 101 or 878. I have to get all numbers from 10 to 1000. There are 99 numbers. {11,...,99,101,...,191,...,202,...,292,...,909,...999}

  1. The first question I have is, how to say Mathematica "Hey Mathematica, I want you to give me these 99 numbers in a range from 10 to 1000"?

  2. Now I have to build the sum of a triple out of these 99 numbers and look for the result, whether it is a palindrome or not. The triple have to contain 3 different numbers. For example {11,22,33} is a good one while {11,11,22} isn't.

  3. And from all these triples, whose sum gives me the result I'm looking for, I have to build the products of each and look for the biggest result.

I know there are 941094 possible triples. I could do it manual but it would take weeks so my problem is to learn how to say Mathematica, how to calculate these things. And please do it as simple as possible and explain every step so I can understand everything.

I want to do it like following:

  • get a list with all 99 numbers.
  • get a permutationlist with triples out of these 99 numbers
  • say Mathematica to take the sum of every triple, look for palindromes and make a new table
  • delete duplicates in the list
  • now find the highest product of the triple which satisfy the 3rd point

It would be very helpful, if you could advise me a good book to learn programming and such things especially in Mathematica (or maybe Python). I need some good books for Mathematica (or maybe Python) to learn such computations and how to say Mathematica what I want from it.

$\endgroup$
  • $\begingroup$ It's unclear what you mean by "the sum gives you the result you're looking for". Could you elaborate on what the final result would be if the palindromes under consideration were just {11, 22, 33}? $\endgroup$ – Patrick Stevens Aug 19 '15 at 13:28
  • $\begingroup$ We take for example {11,22,33}. This is a good list, because every number is different. The sum of the triple is 11+22+33 = 66. Bingo. This triple is a result I am looking for because the result is a palindrome, too. Now I have to go on and find every triple which do this. $\endgroup$ – Masirius Aug 19 '15 at 13:31
5
$\begingroup$
  1. Select is the command. Select[Range[10, 999], palindromeQ] where palindromeQ is a function you define.
  2. Subsets is the command. Subsets[nums, {3}] gives a list which contains every 3-subset of the list nums. Then you'll want to Select the palindromes obtained from applying Plus to the lists.
  3. Deleting duplicates can be done with DeleteDuplicates, but it's not necessary here.
  4. The maximal product can be found with MaximalBy in version 10.2; before that, you'll need to SortBy.

That is, the total program might look like:

palindromeQ[n_] := IntegerDigits[n] === Reverse@IntegerDigits[n]

palindromes = Select[Range[10, 999], palindromeQ];
subs = Subsets[palindromes, {3}];

palindromesBySum = Select[subs, palindromeQ[Plus @@ #] &];
MaximalBy[palindromesBySum, Times @@ # &]

The only thing in there which is inherently a bit difficult is the use of @@ (Apply) and #& (Function), which are things you should look up in the documentation because they're extremely useful.

$\endgroup$
  • $\begingroup$ Wow! Thank you very much. That was very short and very clear. I want to ask you something personal. How did you learn Mathematica? Can you advise me some good or even excellent books to learn this stuff? Maybe with problem sets for "learning by doing"? It would be very helpful. $\endgroup$ – Masirius Aug 19 '15 at 15:02
  • 1
    $\begingroup$ I used Project Euler (www.projecteuler.net) and hacker.org to learn-by-doing; I didn't learn out of books at all. The documentation is very clear and excellent in nearly everything, though, so I do recommend reading the tutorials it contains. Took me months to get my head around functional programming properly, IIRC. $\endgroup$ – Patrick Stevens Aug 19 '15 at 15:07
3
$\begingroup$

The posted method is rather inefficient, and becomes intractable for large cases. A refactoring:

{minbase, maxbase, chunksize} = {10, 999, 3};

palindromeQ[n_] := IntegerDigits[n] === Reverse@IntegerDigits[n];

palindromes = Select[Range[minbase, maxbase], palindromeQ];

p = Select[Range[palindromes[[;; chunksize]] // Tr, 
                 palindromes[[-chunksize ;;]] // Tr], palindromeQ];

f = Flatten[With[{ip = IntegerPartitions[#, {chunksize}, palindromes]},
      Pick[ip, Length /@ DeleteDuplicates /@ ip, chunksize]] & /@ p, 1];
t = Times @@@ f;
m = Max@t;
{m, Pick[f, t, m]}

This is much faster (about 20-30X) on the OP example, and can be used on larger cases, e.g. {minbase, maxbase, chunksize} = {10, 99999, 5}, so looking for the same kind of result for all two to five digit palindromes taken as five-tuples. There would be over 12½ trillion subsets to be evaluated, ruling out methods using subsets for anything beyond pretty trivial cases.

Additional speed could be had be directly generating palindromes vs selecting, an exercise left to the reader.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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