Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

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.

REPORT ZMLA_TEST4.

DATA: sol  TYPE INT4 VALUE 0,

      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.

    CONDENSE s NO-GAPS.

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

      sol = num.

    ENDIF.

    b = b - 1.

  ENDWHILE.

  a = a - 1.

ENDWHILE.

s = |The solution is { sol }|.

WRITE / s.