In the last PHP tutorial, I showed how you could create an HTML form that would use PHP to write data into a MySQL database. In this tutorial, I’ll show you how you can use PHP to pull that data from the database and display it in your HTML. If you need the previous tutorial, you can find it here:

Simple PHP Script to Input Data to MySQL Database from HTML Form

If you recall, in our last example we have a database called “bozo” and a table called “famous_clowns.” In this table we have five fields. You can think of these as columns since we’ll be displaying them in an HTML table anyway. The data fields were: seed, name, country, rating and comments.

The below script is going to be called displayclowns.php. There’s nothing wrong with mixing your HTML with PHP calls, but the document needs to end with the .php extension so your server will know that it contains calls to PHP. Continue reading »

 

The following is problem 8 of Project Euler. To date, it has been solved 66,993 times.

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

The following is the Octave/MATLAB code that will solve it. The below executed in 1.502 seconds.

% ProjectEuler.net problem #008
% Find the greatest product of five consecutive digits in the 1000-digit number.
 
clear
t = clock;
 
% First, we'll treat the huge number as a string.
% Note, the code will need the whole number pasted here.
 
n = "7316717....3450";
 
for a = 1:996;
 
% Find the five numbers to multiply, convert from string to number.
for j=a:a+4
	five_nums(j-a+1) = str2num(n(j));
end
 
colm(a, 1:2) = [a, prod(five_nums)];
 
end
 
fin_answer = max(colm(1:996,2))
 
exec_time = etime(clock, t)
 
% It doesn't ask which numbers give this product, but by
% manually looking through colm, we see that they were
% the 365th - 369th digits, or 9,9,8,7,9.
 
the_digits = n(365:369)

There you have it. Happy coding!

 

The following is problem 7 of Project Euler. As of this posting, it has been solved 74,865 times.

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?

As MATLAB and Octave already have an optimized function for returning prime numbers, this simple solution almost feels like cheating. Maybe I’ll go back later and try a brute force approach with a couple of loops. Or maybe I’ll just live with the below and move on to the next one.

% ProjectEuler.net problem #007
% 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 10,001st prime number?
% answer should be 104,743, exec time 0.01036.
 
clear
tic();
 
which_prime = 10001; % This is the prime we're looking for.
 
% We can start our counter pretty high since we know
% that there are not 10k prime numbers below 100,000.
a = 100000;
 
while length(primes(a)) < which_prime
	a = a + 10000;
end
 
get_primes = primes(a);
 
final_answer = get_primes(which_prime)'
 
exec_time = toc()

You could easily do this with one line of code and it would still execute in about a tenth of a second. You’d just have to guess the upper limit. Maybe guess that there are at least 10,000 prime numbers below, say 2 million, and you could execute the following line:

a = primes(2e6)(10001)

There you have it. Happy coding!

 

The following is problem 6 of Project Euler. As of this posting, it has been solved 89,315 times.

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 = 55^2 = 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.

This one is fairly simple. Create an array from 1 to 100, sum it, then square it. Then create another array and take the sum of each individual element squared. It could probably be done in one line, but the below makes it a little clearer. And it still executes in less than a thousandth of a second.

% ProjectEuler.net problem #006
% Find the difference between the sum of the squares 
% of the first one hundred natural numbers and the square of the sum.
% Below script executes in 0.000366 seconds.
 
clear
t = clock;
 
a = (sum(1:100))^2;
b = [1:100];
c = sum(b.^2);
 
diff = a-c
exec_time = etime(clock, t)

There you have it. Happy coding!

 

One way to speed up the executing times of your coding projects is to use MATLAB and Octave’s native vectorization capabilities. In short, this is one of the significant advantages of using a vector- or matrix-based programming language over the more popular object oriented languages. What do I mean by vectorization? I’ll show you with a quick example.

Maybe you need to sum all the numbers from 1 to 1,000,000. A simple for loop will do the trick.

clear
tic();
 
x = 0;
 
for n = 1:1e6
	x = x + n;
end
x
exec_time = toc()

On my computer the above took 1.249 seconds to execute. Not a tremendous amount of time, but how can we make it faster? By not using a loop, and operating directly on vectors. Try the below…

clear
tic();
 
s = sum(1:1e6);
 
exec_time = toc()

This code executed in 0.0192 seconds, which is more than 50 times faster. The difference between a second and 0.019 seconds may not seem like much, but what if you had to run something like the above within another loop for a hundred or more iterations? Now the difference becomes code that could execute in about 3 seconds compared to 3 minutes. The more complex your coding gets, the more important it is to try to use MATLAB and Octave’s inherent strengths whenever possible.

Now, it may be impossible to eliminate loops from your coding all together, but ask yourself whenever you’re writing a for or while loop, are you doing it to create another vector? If you’re using it with an embedded conditional (IF/ELSE), chances are you may be stuck with it. But if you are looping through a vector to create another vector, chances are you could speed it up by performing the operation directly on the initial vector.

And so it goes. Happy coding!

 

The following is problem 5 of Project Euler. To date, it has been solved 87,591 times.

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?

The following is a MATLAB/Octave solution.

% ProjectEuler.net problem #005
% What is the smallest positive number that is evenly divisible
% by all of the numbers from 1 to 20?
% Given, the answer for 1 to 10 is 2520. All answers will be an increment of this.
% Below code took 9.5974 seconds to execute.
 
clear
tic();
a = 1;
b = 2520; % Increment by this, since we know all answers will be.
start = 11; % don't bother checking 1 to 10, since I'm checking multiples of 2520
stop = 20; % this is the top end of what we're polling
d=0;
 
while a == 1 % This just keeps it in a run loop.
c = start;
b = b + 2520;
 
while c <= stop
	if rem(b,c) ~= 0 % c is not a factor, end loop
		break
	else
		c = c + 1; % c IS a factor, try next higher, cycling 11 through 20
		d = d + 1; % d counts all factors. If d = num tries, we have answer
	end
end
 
if d == stop-start+1	% got answer, kick out of loop
	a = 2;
else			% did not get answer, reset counter and try again
	d = 0;
end
 
end % end "while a" loop. When we have answer, a is 2 and script ends.
 
final_answer = b
exec_time = toc()

Almost 10 seconds to execute is probably a bit much and I’m sure this could be optimized, but I’ll leave that for another exercise.

 

Often you’ll have a need for recording the data your programs generate. MATLAB and Octave have a few ways to do this. We can best show this with a quick example.

The following code will generate a matrix 10,000 by 6. On my current hardware this takes about 2 seconds to execute.

% Sample code to generate a large matrix
clear 
 
for i = 1:10000
	a = sin(i*pi/180);
	b = cos(i*pi/180);
	c = tan(i*pi/180);
	temparray(i, 1:6) = [a, b, c, a^2, b^2, c^2];
end

In MATLAB and Octave, the process of outputting data to files is called “exporting.” Reading data in is likewise called “importing.”

The easiest way to save the above output to a file is to use the save function and add it directly to the code. Adding the following line just below the for loop would do the trick.

save test.txt temparray -ascii

The format is: save filename variable -format. The above will result in a text file called test.txt which will contain the variable “temparray.” You can open or view this in any text editor. The default delimiter is a space but if you’d like to save it as tab-delimited data, you would simply change the line to the following:

save test.txt temparray -ascii -tabs

If you do not specify the variable, all variables in the current workspace will be saved to the file. The above could also be executed from the command line, as long as you had recently run the code and the variable “temparray” was still assigned the value you need. Worth noting is that within Octave or MATLAB, if you save without using the -ascii tag, the default will be to save the data in a proprietary binary format and you will have something like the below text at the top of your document:

# Created by Octave 3.2.4, Thu Mar 10 00:24:15 2011 EST
# name: temparray
# type: matrix
# rows: 10 000
# columns: 6
1.7452e-02   9.9985e-01   1.7455e-02   3.0459e-04   9.9970e-01   3.0468e-04
3.4899e-02   9.9939e-01   3.4921e-02   1.2180e-03   9.9878e-01   1.2195e-03
5.2336e-02   9.9863e-01   5.2408e-02   2.7391e-03   9.9726e-01   2.7466e-03
...

This may be inconvenient for your purposes (for example, if you are using the data with an external program), but if you are going to use the “load” command (described later) to import this file, it doesn’t matter that this header information is present. If it is a hindrance, you can always save the data using the -ascii tag.

Other ways to export data to a file is with the fwrite, csvwrite and dlmwrite commands. Both programs have detailed help files on these commands but in short, they are for appending to files (fwrite), saving as comma-separated-values (csvwrite) and using specific delimiters (dlmwrite).

 

The following is the 4th problem in the Project Euler series. To date, it has been solved 77,499 times.

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.

The following is a MATLAB/Octave solution.

% ProjectEuler.net problem #004
% Find the largest palindromic number that
% is a product of two three digit numbers
% Below code executed in 0.46925 seconds.
 
clear
tic();
 
% First, assume the answer will be two numbers between 900 and 999.
% If this doesn't find one, you could start at 800, then 700, etc.
% Make an array of all these multiples. Will be 10,000 elements
 
i=1;
for a = 900:999
	for b = 900:999
		new(i,1)=a*b;
		i=i+1;
	end
end
 
len = length(new)
 
temp = num2str(new);	% convert elements to strings
temprev = fliplr(temp);	% flip each element
 
index = temp == temprev; % compare the normal string to flipped string
 
% index is now an array, 10,000 x 6, of zeros and ones.
% Where all six in a row are ones, this number is the
% same forwards and backwards (temp = temprev).
% This will be the palindrome, ie., the answer.
 
for j = 1:len
	if index(j,1:6) == ones(1,6)
		max_index = j;
	end
end
 
max_element = max_index
final_answer = temp(max_index, 1:6)
exec_time = toc()

There you have it. Probably not the most efficient way to find the answer but it still executed in less than half a second.

© 2012 mattoneal.com Suffusion theme by Sayontan Sinha