Windows下GVim的全屏

下载gvim的一个扩展“gvimfullscreen_win32”,并解压缩。 将gvimfullscreen.dll复制到gvim安装目录下,与gvim.exe同目录。 修改gvim配置文件_vimrc,在其中添加如下内容:
1
2
3
if has('win32')
map <F11> <Esc>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
endif
此后,即可使用F11键开关gvim的全屏状态。
欧拉工程第十一解

In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid?

这道题最难的地方在于将上面400个数输入到程序中的矩阵里,嘿嘿,后来看别人的解,真有这么干的,饿滴神呀。

我直接把这个矩阵复制到一个文本文件里,然后在程序里解析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/python
# -*- coding: utf-8 -*-

matrix = []
products = []

metaFile = open('c:/matrix.txt', 'rb')
for line in metaFile.readlines():
matrix.append(line.split())
metaFile.close()

for row in range(20):
for col in range(20):
if col+3 < 20:
products.append( int(matrix[row][col]) * int(matrix[row][col+1]) * int(matrix[row][col+2]) * int(matrix[row][col+3]) )
if row+3 < 20:
products.append( int(matrix[row][col]) * int(matrix[row+1][col]) * int(matrix[row+2][col]) * int(matrix[row+3][col]) )
if col+3 < 20 and row+3 < 20:
products.append( int(matrix[row][col]) * int(matrix[row+1][col+1]) * int(matrix[row+2][col+2]) * int(matrix[row+3][col+3]) )
if row+3 < 20 and col-3 > 0:
products.append( int(matrix[row][col]) * int(matrix[row+1][col-1]) * int(matrix[row+2][col-2]) * int(matrix[row+3][col-3]) )
products.sort()

print products

rxvt-unicode的真透明

nacre同学说,urxvt是可以实现真透明的。起初我以为是要用transset-df来实现,但这个东西一般需要手动操作,不具有太大的实用性,而且会把整个窗口透明化。不过后来发现真的不需要用它来画蛇添足,有xcompmgr足矣。

因为我用openbox,要实现窗口的阴影和动画效果,xcompmgr是必须的,我把它设成了开机自启动。比起xcompmgr默认的参数值,下面这条定制的命令实现了简洁的阴影和合理的渐隐渐显时间,因此提供了一个各方面都比较均衡、合理的桌面体验:

xcompmgr -Ss -n -Cc -fF -I-10 -O-10 -D1 -t-3 -l-4 -r4

然后在.Xresources中添加以下两行:

URxvt.depth:32 URxvt.background:rgba:0000/0000/0000/dddd

最后当然要执行一下:

xrdb ~/.Xresources

此后直接启动urxvt即可。

这里面最有意思的就是background项的配置,它有两种形式,一种是:

URxvt.background:[80]black

还有一种就是前面提到的形式。

第一种形式中,中括号里的数字表示半透明度对应的百分比,括号外是颜色名称;第二种形式提供了比第一种更多的色彩选择,四组数字都是十六进制数,前三组是RGB颜色值,最后一组是半透明度,数值越大,透明度越低。

在查阅“man urxvt”的时候,发现urxvt的man pages里的内容真是异常丰富,大部分功能都讲解得言简意赅。以往还抱怨这个东西在网上连个健全的文档都找不到,原来全在这儿呢,真不知道以往无数次地man的时候为什么没有注意到这些,难道man了rxvt了?

PS:这样实现urxvt的真半透明后,貌似xcompmgr实现的阴影在urxvt身上就消失了,求解中……

完美的终端模拟器:rxvt-unicode

urxvtrxvt的unicode版本,支持多国语言,官方网站)。

具有以下特性:

  1. 支持真半透明
  2. 支持Unicode编码,支持多国语言
  3. 支持英文和非英文字符使用不同的字体
  4. 支持CS模式,节省系统资源
  5. 轻量,速度快
  6. 支持Perl扩展功能

但是urxvt有一个很影响情绪的缺陷,就是字符的间距过大,看起来很不舒服。不过已有补丁用来解决这个问题,例如Archlinux用户可以安装rxvt-unicode-chinese

urxvt的配置选项可以参考其官方网站上的说明,另外这里有一份详细的配置文件。 更新日志: 2011-01-15
  1. 修改文章标题
  2. 重写文章内容
  3. 由于原来的rxvt-unicode-256color包已不存在,更换为rxvt-unicode-chinese
  4. 补充一份配置文件
欧拉工程第十解

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

题目越来越变态,开始好玩儿了。

第七解里的算法在这里算是废了,一万个素数都算得那么费劲,两百万以下的素数有十几万个,不得不用筛选法了。

普通的筛选效率也不行,当初就是因为这个原因才没用它。不过优化过的筛选法就很奇妙了,下面是Lua的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
require('math')

local limit = 2000000

local primes = {}
for i=1,limit do
table.insert(primes,true)
end
primes[0] = false
primes[1] = false

for i=0,math.floor(math.sqrt(limit)) do
if primes[i] then
for j=math.pow(i,2),limit,i do
primes[j] = false
end
end
end

local sumVal = 0
for i,j in ipairs(primes) do
if j then
sumVal = sumVal + i
end
end

print(sumVal)

在我这里两秒半就出结果了,Python的表现也不错,四秒半出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from math import sqrt

limit = 2000000
primes = [True for i in range(0,limit)]
primes[0] = False
primes[1] = False

for i in range(1,int(sqrt(limit))+1):
if primes[i]:
for j in range(i**2,limit,i):
primes[j] = False

sumVal = 0
for i in range(len(primes)):
if primes[i]:
sumVal += i

print sumVal

欧拉工程第九解

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

解:

1
2
3
4
5
6
7
8
9
10
flag = False
for a in range(1,1000):
for b in range(1,1000):
if a ** 2 + b ** 2 == (1000 - a - b) ** 2:
print a,b,(1000 - a - b)
print a * b * (1000 - a - b)
flag = True
break
if flag:
break

欧拉工程第八解

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

穷举,解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def MakeProduct(strNum):
prod = 1
for char in strNum:
prod = prod * int(char)
return prod

def GetTheFirstProduct(strNum):
if len(strNum) < 5:
return 0,0
return MakeProduct(strNum[:5]),strNum[1:]

num = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'

largestProduct = 0
while len(num) >= 5:
tmp = GetTheFirstProduct(num)
if tmp == (0,0):
break
num = tmp[1]
if largestProduct < tmp[0]:
largestProduct = tmp[0]
print largestProduct

如果先找到下五个均不为零的连续整数,然后计算它们的积并以之参与比较,效率会更高:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def MakeProduct(strNum):
prod = 1
for char in strNum:
prod = prod * int(char)
return prod

def GetTheFirstProduct(strNum):
if len(strNum) < 5:
return 0,0
subStr = strNum[:5]
index = subStr.rfind('0')
if index == -1:
return MakeProduct(subStr),strNum[1:]
else:
return GetTheFirstProduct(strNum[index+1:])

num = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'

largestProduct = 0
while len(num) >= 5:
tmp = GetTheFirstProduct(num)
if tmp == (0,0):
break
num = tmp[1]
if largestProduct < tmp[0]:
largestProduct = tmp[0]
print largestProduct

欧拉工程第七解

第七解:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

穷举,并加以最大程度的优化:对大于2的素数,只判断奇数;判断一个奇数是否素数时,只拿已经找到的素数中小于第这个数平方根的数来相除,如果均不能整除,就是素数。Python的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def IsPrimeNum(num,feed):
from math import sqrt
tmp = feed[:]
while tmp[-1] > int(sqrt(num)):
tmp.pop()
for i in tmp:
if num % i == 0:
return False
return True

limit = 10001
feed = [2,3,5,7]
temp = 7
counter = 4
while counter < limit :
temp += 2
if IsPrimeNum(temp,feed):
feed.append(temp)
counter += 1
print temp

执行了一下,在我这里居然用了五百秒才出结果,神啊,差不多十分钟啊。想到PHP号称速度很快,于是用PHP重新实现了一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function IsPrimeNum($num,$feed){
$base = floor(sqrt($num));
foreach($feed as $i=>$v){
if($v > $base){
return true;
}
if($num % $v == 0){
return false;
}
}
}

$limit = 10001;
$feed = array(2,3,5,7);
$counter = 4;
$tmp = 7;
while($counter < $limit){
$tmp += 2;
if(IsPrimeNum($tmp,$feed)){
$counter++;
$feed[] = $tmp;
}
}

echo $tmp;

还好,七十四秒出结果,看来PHP的牛皮不是吹的。当然,Lua会更快:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function IsPrimeNum(num,feed)
require('math')
local limit = math.floor(math.sqrt(num))
for i,v in ipairs(feed) do
if v > limit then
return true
end
if num % v == 0 then
return false
end
end
end

local limit = 10001
local feed = {2,3,5,7}
local counter = 4
local tmp = 7
while counter < limit do
tmp = tmp + 2
if IsPrimeNum(tmp,feed) then
counter = counter + 1
table.insert(feed,tmp)
end
end

print(tmp)

执行完后还是吓了一跳,0.3秒,同样是语言,效率的差别咋就那么大呢?!我在想用Java会不会算到2009去。

我不相信这道题用Python就那么难解,下面是用递归实现的程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from math import sqrt

def GuessPrime(feed,limit):
if feed == 2 :
return [2]
elif feed == 3 :
return [2,3]

tmp = int(sqrt(feed))
primes = GuessPrime(tmp,limit)

base = 0
if tmp % 2 == 0:
base = tmp + 1
else:
base = tmp
for i in range(base,feed,2):
flag = 0
for j in primes:
if i % j == 0:
flag = 1
break
if flag == 0:
primes.append(i)
if len(primes) == limit:
return primes

return primes

limit = 10001
feed = 1000000
primes = GuessPrime(feed,limit)
print primes[limit-1]

11秒就出了结果,说到底,算法的改进才是硬道理!

欧拉工程第六解

第六解:

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.

解:

1
2
3
4
5
6
sumSquare = 0
sum = 0
for i in range(1,101):
sumSquare += i**2
sum += i
print sum**2 - sumSquare

欧拉工程第五解

第五解:

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 number that is evenly divisible by all of the numbers from 1 to 20?

解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#为简洁明了,此处不作校验
def GetGreatestCommonDivisor(min,max):
'''辗转相除法求最大公约数'''
while min > 0:
tmp = min
min = max % min
max = tmp
return max

def GetLeastCommonMultiple(a,b):
if a > b:
max = a
min = b
else:
max = b
min = a
div = GetGreatestCommonDivisor(min,max)
return min * max / div

temp = 1
for i in range(1,21):
temp = GetLeastCommonMultiple(i,temp)
print temp

本题旨在求最小公倍数。此算法有意思的是,它的精华在于如何求解两个正整数的最大公约数,有点围魏救赵的意思。

这里可以找到另外一些求解最小公倍数的方法。