Better programmer: solve problems related to programming

If you want to be better programmer then you need to practice a lot:

  • solve programming problems
  • work on logic puzzle and problems
  • play games like Go and Chess
  • think for new ideas and solutions of known problems

In this post only the first point will be cover: solve programming problems. In my opinion it's better to advance in small steps every single day. You can watch a short "funny" video on this topic here:

how to learn programming

In practice this means that if you have 500 programming problems and 3 months for them. It's better to solve several problems each day instead of planning one week to solve all 500 at once.

Mathematical problems and Project Euler

About Project Euler

Project Euler contains many interesting mathematical and computer programming problems that will require good knowledge of math and programming. Solving problems from this project will improve you computer and programming skills. The other benefit of solving problems is training your brain on variety of challenging questions.

This project offers you really good problems, progress tracking, difficulty rating and how many people solved this problem. This will help you to measure your programming level and where are you in the ocean of programmers.

How to solve problems

Let have a look on the first problem from Project Euler:

Project Euler Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

This is the information related to this question:

Published on Friday, 5th October 2001, 06:00 pm; Solved by 749760; Difficulty rating: 5%

So we can assume that this question is easy one and solve by many people.

I'll post my steps in solving this problem. If you have different solution or improvement on the solving process please do share it. You can check the also the thread related to each question and check for solutions in many different languages(this requires registration):

My way of solving problems

  • Be sure that you understand the problem - read the problem description several times. Analyze what is given and what should be solved.

  • Solve the problem as meta code or algorithm - I prefer to use meta code and list of paper in order to list all needed steps. For easier problems like the first one you can skip this step because you can do it naturally. But it's a good practice and you can work on improving this skill.

  • Write code example based on the example problem - Before solving the problem I prefer to play a bit with the given example. I'll check what operations and methods can be used. Can I use something from functional programming or any other area of programming? If there is an example like this one:

    natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

I'll write a small code snippet which is doing exactly what is described. This is a small code snippet in Groovy which is doing what is described in this problem:

def sum = 0
(0..9).each{ num->
	if( num % 3 == 0 || num % 5 == 0 ){
		sum += num
	}
}
  • Once I'm sure in my beta code - I'll continue to the real solution. I'll write the solution and test the answer against the one in the web site. If the solution is correct I'll move on the next step.

  • Trying to optimize and search for alternative solutions - as a final step I'll try to optimize the solution. For example in case of Fibonacci problem you can speed up the solution by using memoization( from functional programming - example in python - Python Fibonacci and memoization ).

I'll try to find alternative solutions. Maybe there is some tricky way of solving the problem? This will help you to think out ouf the box. This is an interesting problem:

How do I print my name 1000 times in Java without looping?

First problem of Project Euler

I'll post my solution on the Project Euler problems with a small changes in it. If you want to check the exact problem and solution you can refer to the official site of the project.

My version of the first question is:

If we list all the natural numbers below 20 that are multiples of 11 or 17, we get 11 and 17. The sum of these multiples is 28.

Find the sum of all the multiples of 11 or 17 below 10000.

Solution in groovy:

def sum = 0
(0..9999).each{ num->
	if( num % 11 == 0 || num % 17 == 0 ){
		sum += num
	}
}
println sum

result:

7225770