Skip to Content

Project Euler is a website dedicated to computational problems intended to be solved with computer programs.

At the time of this writing, it includes over 400 problems, with a new one added every week.

Problems are of varying difficulty but each is solvable in less than a minute using an efficient algorithm on a modestly powered computer.

I have already solved some of the problem, mainly using J (a language in the APL family) or Python.

Last year I learned ABAP and now, to test my skill with it, I decide to solve some of the Project Euler problem using ABAP.

To be able to execute ABAP programs, I have installed a SAP NetWeaver Trial Version ABAP (Windows) under VirtualBox.

The 6th problem (Sum square difference) is the following:

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 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.

 

An ABAP solution is below (run the program to see the solution).

It suffice to loop on the number between 1 and 100, computing a and b at each loop.

Then the solution is given by (a*a)-b.

REPORT ZMLA_EULER_006.

DATA: sol   TYPE INT4 VALUE 0,
      lim   TYPE INT4 VALUE 100,
      n     TYPE INT4 VALUE 1,
      a     TYPE INT4 VALUE 0,
      b     TYPE INT4 VALUE 0,
      s     TYPE STRING.

WHILE n <= lim.
  a = a + n.
  b = b + n * n.
  n = n + 1.
ENDWHILE.
sol = a * a b.

WRITE / |The result is { sol }|.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply