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.

After this problem I will stop to publish here the code of the solution.

I will post the code of the solution in the forum of Project Euler that can be accessed after having solved the problem.

The 9th problem (Special Pythagorean triplet) is the following:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^2 + b^2 = c^2

For example,


32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product a*b*c.

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

The code use the Euclid’s formula for generating Pythagorean triples given an arbitrary pair of positive integers m and n with m > n.

The formula states that the integers

a = m^2 – n^2, b = 2 m n, c = m^2 + n^2

form a Pythagorean triple.

I loop over n and m.

The termination condition is given by

a + b + c = 1000

That is

m^2 – n^2 + 2 m n +  m^2 + n^2 = 1000

m^2 + 2 m n +  m^2 = 1000

2 m^2 + 2 m n = 1000

m^2 + m n = 500

REPORT  ZMLA_EULERO_009.

DATA: m     TYPE NUM4,

      n     TYPE NUM4,

      somma TYPE NUM8,

      a     TYPE NUM4,

      b     TYPE NUM4,

      c     TYPE NUM4,

      sol   TYPE NUM8,

      s     TYPE STRING.

DO 10 TIMES.

    n = SY-INDEX.

    m = n + 1.

    somma = m * m + m * n.

    WHILE somma <= 500.

        IF somma = 500.

            a = m * m – n * n.

            b = 2 * m * n.

            c = m * m + n * n.

            sol = a * b * c.

            EXIT.

        ENDIF.

        m = m + 1.

        somma = m * m + m * n.

    ENDWHILE.

ENDDO.

s = |The solution is { sol }|.

WRITE / s.

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