Learn to program through Project Euler

About 6 months ago, I was surfing and came across a math/programming site that has fascinated me ever since, Project Euler. They have over 200 problems to solve and continue to add more. The problems start off very easy and get difficult rather quickly. Some of the beginning problems can even be solved using paper and pencil.

If you are just starting to learn computer programming, you should check out this site. The first problem can be solved fairly easily, and I will show you how I did it in perl.

First, let’s look at the problem. They want you to “Find the sum of all the multiples of 3 or 5 below 1000.” What the program will have to do is loop through all whole numbers from 1 to 999. Inside the loop, there is a check to see if the number is a multiple of 3 or 5. If it is, it is added to the sum. After the loop, print the sum to the screen.

Here’s the code:

$sum = 0;
$counter = 1;
while( $counter < 1000 ) {
if ( $counter % 3 == 0 || $counter % 5 == 0 ) {
$sum = $sum + $counter;
$counter = $counter + 1;
print "The answer is $sumn";

This program, just like every other computer program, uses flow control. Basically flow control tell the computer what to do and when to do it. There are 2 flow control structures here, a while loop and an if statement. Both of these are started by a comparison inside parenteses, then an opening curly brace. They are ended by the closing curly brace.

The while statement should be fairly straightforward, but the if statement is a little complex. The percent(%) sign means modulus(mod for short), which is simply the remainder of division. For example, 4 % 3 is 1, 4 % 5 is 4, and 9 % 3 is 0. The double pipe(||) means OR. When reading line 4, you say “If counter mod 3 equals 0 or counter mod 5 equals 0, then.”

Let’s go line by line.

Line:1$sum = 0;Set a variable named “sum” to 0.
Line:2$counter = 1;Set a variable named “counter” to 1.
Line:3while( $counter < 1000 ) {Start a loop and continue the loop while counter is less than 1000.
Line:4if ( $counter % 3 == 0 || $counter % 5 == 0 ) {Check to see if the remainder of counter divided by 3 is zero OR the remainder of counter divided by 5 is zero.
Line:5$sum = $sum + $counter;Add counter to sum if so.
Line:6}Close the if block.
Line:7$counter = $counter + 1;Add 1 to counter.
Line:8}Close the while loop.
At this point, the program will repeat the check at line 3 if check is true goto line 4.
Line:9print “The answer is $sumn”;Print the answer to the screen.

You can download perl from perl.com. Once it is installed, you can copy and paste the source code into notepad and save it as euler-1.pl or whatever you want. To run the code, double click on the file.

Play around with the code. Poke it. Prod it. Change the while comparison so it only goes to 10, 100, 100000, etc. Changed the if to check for numbers divisble by 2 and 3. If you mess it up, copy and paste the code back into the file.

If you have any problems, leave a comment.

Web 2.0

When the internet first began, most of the pages were static and didn’t change unless someone manually edited the HTML. Recently, more and more sites have become dynamic. Blogs, Forums, Social Networks, and Wikis have contributed to a movement on the internet dubbed “Web 2.0”. Basically, Web 2.0 refers to website that allow regular users to read and write content to the web.

Forums were the first big step. Forums just let people post questions, answer questions, and share ideas with others. These have been around since the late 1980s when people did not have access to the internet.

Blogs are newer than forums. A blog is simply a self-publication for the web. You can create a blog for any topic you can think of. The blog writers create content and many blogs supply a comments section for feedback, so users can read the blog, then write their ideas.

Wikis, such as Wikipedia, are collections of content. A user can read, edit, and create articles. Then that information is out on the web for anyone to read. Wikipedia is the most popular wiki containing millions of articles on a variety of subjects. Because anyone can edit or create content, the content is almost guaranteed to be up to date.

Social Networks provide a way for real world and internet friends to communicate online very easily. These sites boil down to the simple fact that a user has a set of friends and can send them messages as well as having a profile page. Some sites serve a particular purpose, like delicious is only for bookmarks.

I hope this clears up any confusion about what Web 2.0 is.