10 Jun 2011

Euler Project8 欧拉工程第八题




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



计算上面100位数中,连续5个数字的最大乘积.
只有100位数字,暴力循环即可.每次取5个数,如果包含0,乘积是0,直接跳过;如果这5个数中max-min大于4,则这5个数不连续.

代码:
%% greatest product of five consecutive digits
% Elapsed time is 0.042594 seconds.
% ans =
% 40824
function result = euler8()
tic;
s = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450';
result = 0;
for i=5:length(s)
temp = s(i-4:i);
if ismember(0,temp) || max(temp)-min(temp)>4 % 0 or not consecutive digits
continue;
end
result = max([result,prod(str2num(temp(:)))]);
end
toc;
end

9 Jun 2011

hhc.exe制作chm



反编译chm后,如果修改了内容,需要编译成chm,按照如下方法即可: 制作chm帮助文件,很多软件制作CHM都是调用hhc.exe,比如国产软件QuickCHM,就是调用了hhc.exe
微软的HTML Help Workshop也是调用了hhc.exe,比如我们安装完HTML Help Workshop之后,
在它的安装目录就会看到,有hhc.exe和hha.dll这两个文件。

hhc.exe可以用命令行操作,非常简单,只需要一个参数

hhc.exe C:\makechm\aa.hhp

hhp这个文件是工程文件,hhc是目录文件,hhk是index目文件。
hhp文件可自己新建,按照如下格式写好路径:

[OPTIONS]
Compiled file=C:\Documents and Settings\Administrator\桌面\test.chm
Contents file=test.hhc
Index file=test.hhk
Title=test
[FILES]
mark.htm
C:\fkchm\compile_date.htm
C:\fkchm\scanner_camera_add.htm
C:\fkchm\scanner_camera_deletepix.htm

用命令行反编译CHM文档



现在很多电子书都是CHM格式的,它们是HTML打包生成的。网上有很多的生成CHM和反编译CHM的工具,有很多还是收费的。而最近我发现了一个命令,是Windows自带的一个反编译Chm的命令。

命令行格式输入:

hh -decompile X:\A Y:\B.chm

其中 X:\A是反编译后储存文件的路径, Y:\B.chm是CHM文件的路径。

回车即可在X:\A中找到反编译的文件,连hhc和hhk都有!hhc是CHM的目录结构文件,hhk是CHM的元数据文件。。

Euler Project7 欧拉工程7



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?

寻找第10001个素数. %% find 10001's prime number
% ======use primes==========
% Elapsed time is 0.008245 seconds.
% ans =
% 104743
function result = euler7()
tic;
x = primes(200000);
result = x(10001);
toc;
end


用循环的办法:

% ======use loop===========
% Elapsed time is 5.486655 seconds.
% ans =
% 104743
function result = euler7()
n = 0;
result = 1;
while n<10001
result = result+1;
if isprime(result)
n = n+1;
end
end
end

Euler Project6 欧拉工程6



The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 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.

计算前100个自然数"和的平方"与"平方的和"的差值.
%% Find the difference between the sum of the squares of 1:100 and the square of the sum.
% Elapsed time is 0.000032 seconds.
% ans =
% 25164150
function result = euler6()
tic;
x = 1:100;
result = sum(x).^2-sum(x.^2);
toc;
end