Skip to Content
Author's profile photo Former Member

Project Euler Problem 0004 in ABAP

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 4th problem (Largest palindrome product) does not need particular utility functions to be solved:

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.         

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

It is composed of two loops.

The variable a is assumed to be bigger or equal to b (multiplication is commutative).

The product a * b is assigned to the string s.

The blanks are removed from s with CONDENSE.

Then we check if s is equal to its reverse and if it is bigger than previous found solutions.



      start TYPE INT4 VALUE 999,

      stop  TYPE INT4 VALUE 99,

      a    TYPE INT4,

      b    TYPE INT4,

      num  TYPE INT4,

      s    TYPE STRING.

a = start.

WHILE a > stop.

  b = a.

  WHILE b > stop.

    num = a * b.

    s = num.


    IF s = reverse( s ) and num > sol.

      sol = num.


    b = b – 1.


  a = a – 1.


s = |The solution is { sol }|.

WRITE / s.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.