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.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
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.
DO 5 TIMES.
j = SY-INDEX - 1.
s = substring( val = numeraccio
off = i + j
len = 1 ).
curr = curr * s.
ENDDO.
IF curr > sol.
sol = curr.
ENDIF.
ENDDO.
s = |The solution is { sol }|.
WRITE / s.