withAda.Strings.Unbounded;useAda.Strings.Unbounded;subtypeParameter_TypeisIntegerrange10..9999;subtypeFruit_IndexisIntegerrange1..100;Names:array(Fruit_Index)ofUnbounded_String:=(-- Fill with names);functionSubctractSum(N:Parameter_Type)returnUnbounded_Stringis(ifNinFruit_IndexthenNames(N)else"apple");-- Explanation: -- N can be written as ---- N = a*1000 + b*100 + c*10 + d ---- if N <= 100, we access directly the name array. ---- If N > 100 (which means b >= 1) the new N is---- New_N = a*1000 + b*100 + c*10 + d - (a+b+c+d)-- = a*999 + b*99 + c*9---- If the iteration stops with New_N it must be -- New_N <= 100 which implies a = 0, b=1 -- (it cannot be b=0 otherwise we would had -- not done another iteration) and c=0, that is, -- New_N = 99.---- Therefore, unless N<=100, the result is "apple"

Log in to continue

We're a place where coders share, stay up-to-date and grow their careers.

Ada solution