Posts tonen met het label recursion. Alle posts tonen
Posts tonen met het label recursion. Alle posts tonen

vrijdag 1 juli 2011

Euler problem 61


Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

TriangleP3,n=n(n+1)/21, 3, 6, 10, 15, ...
SquareP4,n=n21, 4, 9, 16, 25, ...
PentagonalP5,n=n(3n−1)/21, 5, 12, 22, 35, ...
HexagonalP6,n=n(2n−1)1, 6, 15, 28, 45, ...
HeptagonalP7,n=n(5n−3)/21, 7, 18, 34, 55, ...
OctagonalP8,n=n(3n−2)1, 8, 21, 40, 65, ...

The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.
  1. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
  2. Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
  3. This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.

analyse:

Start with n = 19. This is the minimal number to generate a four digit Octagonal number.



Problem 61 =       28684 elapsed time:    1 ms. Test Passed.

Euler problem 60


The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

















Problem 60 =       26033 elapsed time:  236 ms. Test Passed.

vrijdag 1 april 2011

Euler problem 57


It is possible to show that the square root of two can be expressed as an infinite continued fraction.
√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...
By expanding this for the first four iterations, we get:
1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666...
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...
The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

analyse:
If you examine the function: Log10(Numerator)/Log10(Denominator) 
you'll notice a pattern. It looks like that each 8th and 13th iteration 
yield to a fraction 2 (iteration 8,13,21,26,34,39,...)
So in 1000 iterations there are 2 positive interations every 13 iterations. 
=> answer: 2 * 1000 / 13 









Problem 57 =         153 elapsed time:    0 ms. Test Passed.

zondag 20 februari 2011

Euler problem 24

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
012   021   102   120   201   210
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

Problem24 =  2783915460 elapsed time:    0 ms. Test Passed.

Euler problem 20

n! means n × (n − 1) × ... × 3 × 2 × 1
Find the sum of the digits in the number 100!



Problem20 =         648 elapsed time:    3 ms. Test Passed.


Euler problem 18

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

Solution:
This problem can be solved by using recursion. The maximum total at any node is: node + maximum of two nodes below.

Performance improvement:
Use a cache to store maximum total of each node. Visited nodes will not be computed again. This gives a performance boost from 720 ms to almost 0 ms.

Problem18 =        1074 elapsed time:    0 ms. Test Passed.

Euler problem 15

Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20×20 grid?


Solution:

Wanneer we het grid 20x20 met het eindpunt omhoog vastpakken, krijgen we 
onderstaande driehoek:

                 1
               1   1
             1   2   1
           1   3   3   1
rij 4-> 1   4   6   4   1     
       1   5  10   10  5   1
     1   6  15   20  15  6   1
   1   7   21  35  35  21  7   1
 1   8   28  56  70  56  28  8   1


 De getallen in de driehoek geven het aantal wegen aan vanaf dat getal naar de top.
 Zo zijn vanuit het getal 2 precies twee wegen naar de top.
 Omdat er steeds 2 keuzes zijn om van een getal de weg naar de top te vervolgen is
 een getal gelijk aan som van beide bovenliggende getallen.
 Het "toeval" wil dat bovenstaande eigenschap overeen komt met de driehoek van Pascal!

 De getallen in de driehoek kunnen we met de binominaal formule berekenen.

 B(n/k) = n!/(k!*(n-k)!)

 Hierin is rij n de macht en k de plaats v/d coefficienten in een n'e graads vergelijking.
 Voor een 4e graads vergelijking (a+b)^4 krijgen we de coefficienten: 1 4 6 4 1

 Het beginpunt voor een grid 2 x 2 is n=4 en k=2 -> aantal wegen is B(n/k)=4!/2!*2! = 6.
 Het beginpunt voor een grid 3 x 3 is n=6 en k=3 -> aantal wegen is B(n/k)=6!/3!*3! = 20.

Het antwoord op de gestelde vraag is dus:
Het beginpunt voor een grid 20 x 20 is n=40 en k=20 -> aantal wegen is B(n/k)=40!/20!*20! =



Problem15 =137846528820 elapsed time:    0 ms. Test Passed.


zaterdag 19 februari 2011

Euler problem 14

The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.








Performance improvements:

1) i/2 -> i>>1                           // 3842 -> 2795 ms.
2) Inline (i & 1) == 0.                // 2795 -> 2344 ms.
3) Check only odd numbers.      // 2344 -> 1215 ms.
4) use cache to store terms.     // 1215 ->   82 ms.



Problem14 =      837799 elapsed time:   82 ms. Test Passed.