Skip to Content
Author's profile photo Former Member

Project Euler Problem 0008 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.
Find the greatest product of five consecutive digits in the 1000-digit number.





















An ABAP solution is below (run the program to see the solution).
To solve the problem I construct a string of 1000 digits.
This is piecewise because of limitation to the max lenght of a string literal.
Then I loop over the digit to compute the product and I keep the max.

REPORT zmla_euler_008.

DATA: numeraccio TYPE string VALUE ‘73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557’,
      i       TYPE int4,
      j       TYPE int4,
      curr    TYPE int4,
      sol     TYPE int4      VALUE 0,
      s       TYPE string.

CONCATENATE numeraccio ‘66896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776’ INTO numeraccio.
CONCATENATE numeraccio ‘65727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474’ INTO numeraccio.
CONCATENATE numeraccio ‘82166370484403199890008895243450658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408’ INTO numeraccio.
CONCATENATE numeraccio ‘07198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450’ INTO numeraccio.

DO strlen( numeraccio ) – 5 TIMES.
  i = SY-INDEX – 1.
  curr = 1.
    j = SY-INDEX – 1.
    s = substring( val = numeraccio
                   off = i + j
                   len = 1 ).
    curr = curr * s.
  IF curr > sol.
    sol = curr.

s = |The solution is { sol }|.
WRITE / s.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Steve Rumsby
      Steve Rumsby

      I'm very glad you mentioned Project Euler, because I have now signed up and I'm starting to work through solutions to some of the problems, also using ABAP. However, I have discovered this on the Project Euler "About" page:

      I learned so much solving problem XXX so is it okay to publish my solution elsewhere?

      It appears that you have answered your own question. There is nothing quite like that "Aha!" moment when you finally beat a problem which you have been working on for some time. It is often through the best of intentions in wishing to share our insights so that others can enjoy that moment too. Sadly, however, that will not be the case for your readers. Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery. Please do not deny others what you have so richly valued yourself.

      I wonder, therefore, if you should reconsider publishing solutions here? I confess I was considering doing the same, particularly as I was planning on using some non-traditional ABAP techniques (object orientation, functional programming, lazy evaluation, etc.) as I did in this blog - Object-oriented Sieve of Eratosthenes - and I think such things might be educational for others. Now I'll continue working through the Project Euler problems but I'll find another source of inspiration for my non-traditional ABAP blogs.


      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Steve,

      thank you for your feedback.

      I will publish only some solution to very old problem (the one for which the forum is closed).

      For other I will use the Project Euler forum that is accessible after you have solved a particular problem.


      Author's profile photo Steve Rumsby
      Steve Rumsby

      It is a shame, really, because there are some interesting problems there. Producing elegant solutions to them is a good way to learn, and having other people comment on your solutions and suggest alternative mechanisms is even better. As I said, I was going to do exactly the same as you until I read that. But it seems only right to respect their wishes.

      I hadn't noticed that the discussion forums for some problems are closed - I guess I haven't solved any of those yet!