April 2008 - Posts
Sum the primes below 2,000,000.
This is the sort of problem that Matlab handles very well. The code looks pretty, and I got an answer right quick. Now if I type fast, I'll get my code posted before my battery drains and Baseball Tonight ends.
function euler10
sum(primes(2000000),2)
Now I'm really challenging myself - can I get a solution to a problem without the fan kicking on? This one took a few tries, but I finally found a good algorithm.
We're asked to find the pythagorean triplet (a, b, c) that sums to 1000. I made an educated guess as to a good range for c, to limit the search space.
function euler9
% a < b < c
for cc = 500:-1:300
aa_bb = 1000-cc;
for bb = aa_bb-1:-1:1
aa = aa_bb - bb;
if (aa*aa + bb*bb) == cc*cc
disp([num2str(aa), ", ", num2str(bb), ", ", num2str(cc)]);
aa*bb*cc
return;
end
end
end
This problem reminded me of Professor Rida Farouki, who teaches his students the value of conserving computations in CAD systems, and who has written several papers on Pythagorean hodograph curves.
This problem asks us to find the greatest product of five consecutive digits in a given 1000 digit number. First the 1000 digit number has to be stored as a string. I wanted to make sure that each digit of the number was converted from a string to an int only once, which complicates the code a bit. Second, it occurred to me that if I'm looking for the greatest product, I'm also looking for the greatest sum. This allows me to save some further compute time.
function euler8
big_num = "73167176531330624919225119674426574742355349194934...
96983520312774506326239578318016984801869478851843...
85861560789112949495459501737958331952853208805511...
12540698747158523863050715693290963295227443043557...
66896648950445244523161731856403098711121722383113...
62229893423380308135336276614282806444486645238749...
30358907296290491560440772390713810515859307960866...
70172427121883998797908792274921901699720888093776...
65727333001053367881220235421809751254540594752243...
52584907711670556013604839586446706324415722155397...
53697817977846174064955149290862569321978468622482...
83972241375657056057490261407972968652414535100474...
82166370484403199890008895243450658541227588666881...
16427171479924442928230863465674813919123162824586...
17866458359124566529476545682848912883142607690042...
24219022671055626321111109370544217506941658960408...
07198403850962455444362981230987879927244284909188...
84580156166097919133875499200524063689912560717606...
05886116467109405077541002256983155200055935729725...
71636269561882670428252483600823257530420752963450";
sum = 0;
product = 1;
for ii = 1:5
digit(ii) = str2num(big_num(ii));
sum = sum + digit(ii);
product = product*digit(ii);
end
biggest_product = product;
biggest_sum = sum;
for ii = 2:996
sum = sum - digit(1);
for jj = 1:4
digit(jj) = digit(jj+1);
end
digit(5) = str2num(big_num(ii+4));
sum = sum + digit(5);
if sum > biggest_sum
biggest_sum = sum;
biggest_product = digit(1)*digit(2)*digit(3)*digit(4)*digit(5);
end
end
biggest_product
Is it cheating to use the Matlab built in isprime function? It saves me from going to Google to find a prime number algorithm.
Here's my code:
function euler7
ii = 0;
jj = 2;
while 1
if isprime(jj)
ii=ii+1;
if ii == 10001
break;
end
end
jj=jj+1;
end
jj
This completed in a reasonable amount of time on my slow Mac, but later I read about only testing odd numbers, and I wish I had thought of that.
I'm skipping around a bit, but this one shows off Matlab a little better.
function euler6
nat_nums = 1:1:100
sum(nat_nums,2)^2 - sum(nat_nums .* nat_nums, 2)
.* is the array multiplcation operator (element-wise)
Arrays in Matlab are really 1xn matrices, so in the sum functions, the second argument indicates that I should sum across columns.
Matlab rocks.
I thought I'd join the Euler problem bandwagon. In true mechanical engineering fashion, I'm using Matlab to solve them. Actually, just for an added twist, I'm using Octave, an open source math interpreter that is mostly compatible with Matlab, on my dog-slow Mac notebook, forcing me to not use brute-force approaches if they can be avoided.
I had already seen solutions to problems 1-3, so I started with 4. So here goes:
function euler4
max_result = 0;
for ii = 999:-1:100
for jj = 999:-1:100
% Don't bother to check if this number is smaller than the max found
result = ii*jj;
if (result < max_result)
continue;
end
% Check for palindrome
result_string = int2str(result);
success = 1;
rlength = length(result_string);
for kk = 1:rlength/2
if result_string(kk) ~= result_string(rlength-kk+1)
success = 0;
break;
end
end
if success && result > max_result
max_result = result;
end
end
end
max_result
Not pretty, but it gets the job done.