Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Closest number from an array

Former Member
0 Kudos

Hi Friends,

We have a requirment to get closest number from an arry of numers for our requirement quantity to round up.

Example we have numbers 100, 100, 80, 80, 80, 60 as available stock.

Our requirement is 230

System should give us the result 240 which is either 80+80+80 or 100+80+60

We are only interested for the result 240 and not bothered with either of options it uses.

Is there any fucntion module or any other method to achieve this.

Regards,

Malay

1 ACCEPTED SOLUTION

raymond_giuseppi
Active Contributor
0 Kudos

AFAIK, No, there are not FM to solve every problem in SAP. You could search but don't expect too much. So use some personal thought or google on: "algorithm" "sum of integer" "value " and then Abap coding your algorithm.

Hint: think recursive, or from school memories some Polynomial time approximate algorithm

Regards,

Raymond

5 REPLIES 5

former_member210008
Active Participant
0 Kudos

I think you're looking for a knapsack problem solution. Am I right?

0 Kudos

It's not like a knapsack problem. In a knapsack problem the objects have two values and you maximize one while limiting the other.

raymond_giuseppi
Active Contributor
0 Kudos

AFAIK, No, there are not FM to solve every problem in SAP. You could search but don't expect too much. So use some personal thought or google on: "algorithm" "sum of integer" "value " and then Abap coding your algorithm.

Hint: think recursive, or from school memories some Polynomial time approximate algorithm

Regards,

Raymond

venkateswaran_k
Active Contributor
0 Kudos

Dear Malay

As Mr.Raymond says, you apply the recursive loop - write your own form routine that returns a result table with storage location and stock.

just pseudo code as follows

requirement = 240

loop at your location wise stock iternanl table

      if location stock is <= requirement

             append this location stock (X) to result table

             requirment = requiement - location stock

      else

             append this (location stock X - requiremnt)  to result table

         

      endif.

endloop

return the result table

Juwin
Active Contributor
0 Kudos