eBash

It is not the mountain we conquer but ourselves

Project Euler Problem 4

| Comments

Table of Contents

1 Problem

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.

Find the largest palindrome made from the product of two 3-digit numbers.

2 Solution

没什么好说的

3 Answer

906609

Source:C++

Project Euler Problem 3

| Comments

Table of Contents

1 Problem

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

2 Solution

很简单没什么好说的,只是有几点需要注意。

  1. C++中整型文字常量太大,需要在数字后加 LL
  2. 找素数时,除了2,其它以2为递增寻找

3 Answer

6857

Source:C++

Project Euler Problem 5

| Comments

Table of Contents

1 Problem

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

2 Solution

可以直接依题意解决,或者稍微用点数学的知识.

我们知道任何大于1的自然数n可以用:

\(n = p_{i}^{a_{i}}p_{i+1}^{a_{i+1}}…p_{j}^{a_{j}}\)

其中 \(p_{i}\) 为素数, \(a_i\) 为幂次.

我们只要找出 k = 20时,其各素数的最高次冥,然后相乘即可.

3 Answer

232792560

Source:C++

Project Euler Problem 6

| Comments

Table of Contents

1 Problem

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

2 Solution

可以直接依定义计算12 + 22 + … + n2,(1 + 2 + … + n)2 再相减,或者利用简单的初等数学:

\begin{eqnarray} \sum_{i=1}^na_{i} = \frac{(1+n) n}{2} \end{eqnarray} \begin{eqnarray} \sum_{i=1}^n a^2_i = \frac{(2n+1)(n+1)n}{6} \end{eqnarray}

3 Answer

25164150

Source:C++

Project Euler Problem 2

| Comments

Table of Contents

1 Problem

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

2 Solution

可以直接遍历Fibonacci数列,找出偶数相加,需要 \(O\) (n)。但我们观察如下数列:

1 1 2 3 5 8 13 21 34 55 89 144

易知每三个Fibonacci为偶数,F(n)和F(n-3),F(n-6)亦可推导:

F(n) = F(n-1) + F(n-2)
= F(n-2)+F(n-3)+F(n-2)=2 F(n-2) + F(n-3)
= 2(F(n-3)+F(n-4))+F(n-3))=3 F(n-3) + 2 F(n-4)
= 3 F(n-3) + F(n-4) + F(n-5) + F(n-6)
= 4 F(n-3) + F(n-6)

3 Answer

4613732

Source:C++

Project Euler Problem 1

| Comments

Table of Contents

1 Problem

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

2 Solution

可以直接遍历找出所有整除3或5的数,然后相加,需要 \(O\) (n)。但如果运用数学集合的概念,只要 \(O\) (1).

基本思路:

  1. 所有被3整除数相加 3 + 6 + … + 999 = 3 * (1 + 2 + … + 333)
  2. 加上所有被5整除数 5 + 10 + … + 995 = 5 * (1 + 2 + … + 199)
  3. 减去被15整除的数 15 + 30 + … + 995 = 15 * (1 + 2 + …+ 16)

其中(1 + 2 + … + n) = (1 + n) * n / 2;

333 = floor(1000/3);

3 Answer

233168

Source:C++

Octopress-encoding-problem

| Comments

初始安装使用octopress总会碰到各种问题,比较常见的就是编码问题

注意:我的安装环境为win7 64位

rake generate提示编码错误

以下是我碰到的已经解决的问题

...
/convertible.rb:29:in `read_yaml': invalid byte sequence in GBK (ArgumentError)
....

原因:你生成的文件编码方式不是Ansi(比如是UTF-8)

解决:转换文件编码为Ansi即可,不过生成的网页在UTF-8下会显示乱码

...
invalid byte sequence in UTF-8 (ArgumentError)
...

原因:你生成的文件编码方式为UTF-8,,但在Ruby convertible.rb 文件中没有进行相应的修改

解决:

def read_yaml(base, name)
     -self.content = File.read(File.join(base, name))
     +self.content = File.read(File.join(base, name), :encoding => "utf-8")

下面是我尝试但无效的解决方法

解决: 在命令行下改变当前代码页编码为UTF-8:

chcp.com 65001

解决:

`打开 shell|cmd 之后,先执行

    set LC_ALL=en_US.UTF-8
    set LANG=en_US.UTF-8
然后再执行 rake 的命令。
或者,在 d:\RailsInstaller\Ruby1.9.2\setup_environment.bat 的最后面加上这两句。

解决:在octopress根目录下加

export LC_ALL=zh_CN.UTF-8
export LANG=zh_CN.UTF-8

解决:与上一解决方法相似,只是加入的位置不同

在git bash下
cd ~
emacs -nw .bash_profile
加入
export   LC_ALL=zh_CN.UTF-8
export   LANG=zh_CN.UTF-8

Statement

| Comments

鉴于本人近期时间有限,无法对博客进行版式布局的改善,但会坚持丰富博客内容,欢迎各种交流.