Dive into Project Euler Part 1 (1-20)

Although calculation of mathematica codes could be slower than those written in C/C++, or even those in python, the prodigious and interesting if not funny functions do save coding time a lot.

Problem 1

Brute force solution at first thought:

1
2
Total@Select[Range[999], 3\[Divides]# || 5\[Divides]# &]
233168

What if the upper limit is extreme large, like \(10^{10}\), or \(10^{10^7}\)? Calculating one after another or even saving them using Range is impossible. Here comes the mathematical way:

1
2
3
4
5
top = 10^(10^7) - 1;
SumMultiplesOf[n_] := (p = Quotient[top, n]; n*p*(p + 1)/2);
SumMultiplesOf[m_, n_] := SumMultiplesOf[m] + SumMultiplesOf[n] - SumMultiplesOf[LCM[m, n]];
N[SumMultiplesOf[3, 5], 2] // AbsoluteTiming
{2.222280, 2.3*10^19999999}

Problem 2

1
2
3
n=NestWhile[# + 1 &, 1, Fibonacci@# <= 4*^6 &] - 1
Total@Select[Array[Fibonacci,n] ,  EvenQ]
4613732

Even numbers appear every 3rd positions in Fibonacci series:

1
Total@Table[Fibonacci@i, {i, 3, n, 3}]

Problem 3

1
2
Max[FactorInteger[600851475143][[All, 1]]]
6857

Problem 4

1
2
Catch@Scan[If[FromDigit@Reverse@IntegerDigits@# == #, Throw[#]] &, Reverse@Sort@Flatten@Table[i*j, {i, 999, 100, -1}, {j, i, 100, -1}]]
906609

Problem 5

1
2
LCM @@ Range[20]
232792560

PythonChallenge Solutions Using Mathematica

Level 1:

1
a = CharacterRange["a", "z"]; StringJoin[Characters[s] /. Thread[a -> RotateLeft[a, 2]]]

Level 2:

1
Sort[Tally[Characters[s]], #1[[2]] <= #2[[2]] &]

Level 3:

1
StringJoin[StringCases[s,RegularExpression["[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]"] ->  "$1"]]

Level 4:

1
2
3
4
5
NestWhileList[(resp = Import["http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="<>#];
   m = StringCases[resp ,"next nothing is " ~~ (x : DigitCharacter ..) -> x];
   If[m != {}, m[[1]], Print[resp]; ""]) &,
"12345",
StringLength[#] > 0 &]

16044->8022, continue

Level 5:
pickle module needed, python only.

Mummy Maze Solver

Mummy Maze is a game created by PopCap in 2002. It is based on Robert Abbott’s Theseus maze. There are 3 scales of lattices: 6*6, 8*8, 10*10. With white mummy functioning as Minotaur, Mummy Maze introduced many varieties: red mummy, scorpion, trap, gate and key. There is only one Minotaur in the original Theseus maze, while the explorer in Mummy Maze faces more enemies.

If solutions are all you want, please visit this Complete Walktrough.

The 14th puzzle in Pharaoh’s Tomb, has the solution with the most moves, 66 steps.
mummy maze demo

Frog Mania

Frog Mania is a flash game. It is quite easy to play, so this is not about its solver, but the way to get the underlying matrix representation of each level.
frog_mania

Kami Solver

Kami
Actually, all puzzles data are located in the “puzzles” folder under the game directory, including classic group from A to E, and pays needed premium “5C1” and “Pat1”.

PuzzleData are stored in files such as SAL1_TwoSides.xml(Stage A Level 1). colours variable denotes the grid. gold, silver, bronze variables are the moves count to achieve these medals. The “solution” part, well, is the solution. Following computer world’s tradition, in each turn, x, y mean the column, row coordinates, both originating from 0.

If solution is all your want, it’s done.