# Maximum value of a function using loops

I have a function ab+bc+a/c, where a,b and c ranges from 1 to 5.I have to get an output of maximum value of this function and the values of a,b and c which give that maximum value. Is it possible to do it with loops? Please help me to solve this as I am not familiar with loops...

• Are you going to use the program Mathematica for this? May 31, 2016 at 12:27
• Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
May 31, 2016 at 12:35

Expanding on answer by Louis using Table

Return maximum value and associated value of variables (a, b, c) as replacement rules:

Module[{max = {0, 0, 0, 0}, val},
Table[
If[(val = a*b + b*c + a/c) > First[max],
max = {val, a, b, c}],
{a, 5}, {b, 5}, {c, 5}];
Clear[a, b, c];
{First[max], Thread[{a, b, c} -> Rest[max]]}]

(*  {51, {a -> 5, b -> 5, c -> 5}}  *)


Comparing with Maximize

Maximize[{a*b + b*c + a/c,
1 <= a <= 5, 1 <= b <= 5, 1 <= c <= 5},
{a, b, c}, Integers]

(*  {51, {a -> 5, b -> 5, c -> 5}}  *)

% === %%

(*  True  *)


See

Looping Constructs

Looping is a core concept in programming. The Wolfram Language provides powerful primitives for specifying and controlling looping, not only in traditional procedural programming, but also in other, more modern and streamlined programming paradigms.

Table[a b + b c + a/c, {a, 1, 5, 1}, {b, 1, 5, 1}, {c, 1, 5, 1}]


{{{3, 7/2, 13/3, 21/4, 31/5}, {5, 13/2, 25/3, 41/4, 61/5}, {7, 19/2,
37/3, 61/4, 91/5}, {9, 25/2, 49/3, 81/4, 121/5}, {11, 31/2, 61/3,
101/4, 151/5}}, {{5, 5, 17/3, 13/2, 37/5}, {8, 9, 32/3, 25/2, 72/
5}, {11, 13, 47/3, 37/2, 107/5}, {14, 17, 62/3, 49/2, 142/5}, {17,
21, 77/3, 61/2, 177/5}}, {{7, 13/2, 7, 31/4, 43/5}, {11, 23/2, 13,
59/4, 83/5}, {15, 33/2, 19, 87/4, 123/5}, {19, 43/2, 25, 115/4,
163/5}, {23, 53/2, 31, 143/4, 203/5}}, {{9, 8, 25/3, 9, 49/5}, {14, 14, 46/3, 17, 94/5}, {19, 20, 67/3, 25, 139/5}, {24, 26, 88/3, 33, 184/5}, {29, 32, 109/3, 41, 229/5}}, {{11, 19/2, 29/3, 41/4, 11}, {17, 33/2, 53/3, 77/4, 21}, {23, 47/2, 77/3, 113/4, 31}, {29,
61/2, 101/3, 149/4, 41}, {35, 75/2, 125/3, 185/4, 51}}}