Tag Archives: R

project euler -- problem 68

Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.

Working clockwise, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can be described uniquely. For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.

It is possible to complete the ring with four different totals: 9, 10, 11, and 12. There are eight solutions in total.

Total      Solution Set
9        4,2,3; 5,3,1; 6,1,2
9        4,3,2; 6,2,1; 5,1,3
10      2,3,5; 4,5,1; 6,1,3
10      2,5,3; 6,3,1; 4,1,5
11      1,4,6; 3,6,2; 5,2,4
11      1,6,4; 5,4,2; 3,2,6
12      1,5,6; 2,6,4; 3,4,5
12      1,6,5; 3,5,4; 2,4,6
By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.

Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum 16-digit string for a "magic" 5-gon ring?

Read more »

modified wp-codebox to highlight R code as in Pretty-R

I found wp-codebox could highlight R code two years ago. This plugin is based on GeSHi to highlight source code internally.

Now there are many ways to highlight R syntax in the website. Pretty-R provided by Inside-R is a popular tool in the community.

I like the color style of Pretty-R more than which provided by GeSHi. GeSHi also links functions to the online manuals; this feature is very helpful for those not familiar with R. But I found many of the keywords are not linked properly.

I modified wp-codebox, to color the functions as in Pretty-R, and link the documents back to inside-R. The external link works fine and syntax now highlighted just exactly like the Pretty-R as you can refer to my previous post.

The modified file can be downloaded from github.

project euler: problem 62

The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.Find the smallest cube for which exactly five permutations of its digits are cube.

---

I tried to generate all the cubic number with specific length, and iterate until exactly five numbers have the same digits.
Read more »

project euler: problem 61


Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:
Triangle          P3,n=n(n+1)/2      1, 3, 6, 10, 15, ...
Square            P4,n=n2            1, 4, 9, 16, 25, ...
Pentagonal        P5,n=n(3n−1)/2     1, 5, 12, 22, 35, ...
Hexagonal         P6,n=n(2n−1)       1, 6, 15, 28, 45, ...
Heptagonal        P7,n=n(5n−3)/2     1, 7, 18, 34, 55, ...
Octagonal         P8,n=n(3n−2)       1, 8, 21, 40, 65, ...

The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.

    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).
    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.
    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.

Firstly, I defined getPolygonalNumber to generate all the polygonal numbers and split the first and last two digits in a data.frame.

I think this problem can be done by something like the merge function. Then I got confused, since the order of this cycle was unknown beforehand.

The findConnected function was implemented, to work like a merge function for combining array. Polygonal types were recorded in findConnected function, and screen out those do not met the "only one number for each polygonal type" criteria.

Then, wrapping findConnected function in a for loop to find the cycle at a specific length.
Read more »

project euler -- problem 66

Consider quadratic Diophantine equations of the form:

\( x^2-Dy^2=1 \)

For example, when D=13, the minimal solution in x is 6492 – 13×1802 = 1.

It can be assumed that there are no solutions in positive integers when D is square.

By finding minimal solutions in x for D = {2, 3, 5, 6, 7}, we obtain the following:

32 – 2×22 = 1
22 – 3×12 = 1
92 – 5×42 = 1
52 – 6×22 = 1
82 – 7×32 = 1

Hence, by considering minimal solutions in x for D ≤ 7, the largest x is obtained when D=5.

Find the value of D ≤ 1000 in minimal solutions of x for which the largest value of x is obtained.

这道题如果用brute-force的话,很容易写,但是因为x,y的解增长很快,求解时间实在是太长了。想不出什么好办法。只得去补补课,看了mathworld上的资料和Matthew Wright写的一份文档

这个方程叫佩尔方程,分类上属于丢番图方程,这是一个非常著名的方程,公元前251年阿基米德提出的牛群问题,其解就可以直接由佩尔方程给出。费马研究了这个方程,并且引起了大家的注意,欧拉在给哥德巴赫写信时,把这个方程称为佩尔方程,结果就一直沿用叫佩尔方程。

\( \sqrt{D} = \sqrt{\frac{x^2-1}{y^2}} \)可以看出\( \sqrt{D}\)的值和x/y非常接近,利用problem64把开方变成连分数的做法,估计出一对x和y,它们的比值非常接近\( \sqrt{D}\)。代码基本上来自于problem 64,少量的修改,主要是对长整数的支持。
Read more »