Represent number N (N>1) as N = x + y, where x * y is the maximum possible value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












My problem is the following: I have to find such x and y (x+y=N) where x * y is the maximum possible value.



I have written an algorithm (which I think works well) and I am looking for more efficient and faster way to solve this problem.



This is my algorithm:



static void Find(int N)

int multiplication = 1;
int maxMultiplication = 1;
int desiredNumber = 1;

for (int i = 1; i <= N / 2; i++)

multiplication = i * (N - i);
if(multiplication > maxMultiplication)

maxMultiplication = multiplication;
desiredNumber = i;



Console.WriteLine("Desired presentation of number N is --> N = 0 + 1",
desiredNumber, N - desiredNumber);







share|improve this question



















  • Do you know any differential calculus?
    – Peter Taylor
    Jun 5 at 15:49










  • I have done some courses. I think I will be able to understand the explanation...
    – Beginner
    Jun 5 at 15:51
















up vote
0
down vote

favorite












My problem is the following: I have to find such x and y (x+y=N) where x * y is the maximum possible value.



I have written an algorithm (which I think works well) and I am looking for more efficient and faster way to solve this problem.



This is my algorithm:



static void Find(int N)

int multiplication = 1;
int maxMultiplication = 1;
int desiredNumber = 1;

for (int i = 1; i <= N / 2; i++)

multiplication = i * (N - i);
if(multiplication > maxMultiplication)

maxMultiplication = multiplication;
desiredNumber = i;



Console.WriteLine("Desired presentation of number N is --> N = 0 + 1",
desiredNumber, N - desiredNumber);







share|improve this question



















  • Do you know any differential calculus?
    – Peter Taylor
    Jun 5 at 15:49










  • I have done some courses. I think I will be able to understand the explanation...
    – Beginner
    Jun 5 at 15:51












up vote
0
down vote

favorite









up vote
0
down vote

favorite











My problem is the following: I have to find such x and y (x+y=N) where x * y is the maximum possible value.



I have written an algorithm (which I think works well) and I am looking for more efficient and faster way to solve this problem.



This is my algorithm:



static void Find(int N)

int multiplication = 1;
int maxMultiplication = 1;
int desiredNumber = 1;

for (int i = 1; i <= N / 2; i++)

multiplication = i * (N - i);
if(multiplication > maxMultiplication)

maxMultiplication = multiplication;
desiredNumber = i;



Console.WriteLine("Desired presentation of number N is --> N = 0 + 1",
desiredNumber, N - desiredNumber);







share|improve this question











My problem is the following: I have to find such x and y (x+y=N) where x * y is the maximum possible value.



I have written an algorithm (which I think works well) and I am looking for more efficient and faster way to solve this problem.



This is my algorithm:



static void Find(int N)

int multiplication = 1;
int maxMultiplication = 1;
int desiredNumber = 1;

for (int i = 1; i <= N / 2; i++)

multiplication = i * (N - i);
if(multiplication > maxMultiplication)

maxMultiplication = multiplication;
desiredNumber = i;



Console.WriteLine("Desired presentation of number N is --> N = 0 + 1",
desiredNumber, N - desiredNumber);









share|improve this question










share|improve this question




share|improve this question









asked Jun 5 at 14:39









Beginner

896




896











  • Do you know any differential calculus?
    – Peter Taylor
    Jun 5 at 15:49










  • I have done some courses. I think I will be able to understand the explanation...
    – Beginner
    Jun 5 at 15:51
















  • Do you know any differential calculus?
    – Peter Taylor
    Jun 5 at 15:49










  • I have done some courses. I think I will be able to understand the explanation...
    – Beginner
    Jun 5 at 15:51















Do you know any differential calculus?
– Peter Taylor
Jun 5 at 15:49




Do you know any differential calculus?
– Peter Taylor
Jun 5 at 15:49












I have done some courses. I think I will be able to understand the explanation...
– Beginner
Jun 5 at 15:51




I have done some courses. I think I will be able to understand the explanation...
– Beginner
Jun 5 at 15:51










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










$$fractextdtextdx x(N-x) = N - 2x$$
At a maximum, $fractextdftextdx = 0$, so the maximum occurs when $x = fracN2$. (Left as an exercise: show that it's a maximum and not a minimum).



Therefore you can skip the loop: desiredNumber = N / 2; is all you need. Note that if N is odd this gives the pair $leftlfloor fracN2 rightrfloor, leftlceil fracN2 rightrceil$, which is the correct solution in integers.






share|improve this answer





















  • If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
    – paparazzo
    Jun 5 at 16:37











  • @paparazzo, what's s? And if that should say 2, where are we in disagreement?
    – Peter Taylor
    Jun 5 at 16:41











  • Yes 2. In integer math 25/2 = 12 so I think need 12, 13
    – paparazzo
    Jun 5 at 16:43










  • And where are we in disagreement? desiredNumber will be assigned 12.
    – Peter Taylor
    Jun 5 at 16:50






  • 1




    @paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
    – VisualMelon
    Jun 5 at 17:12











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195893%2frepresent-number-n-n1-as-n-x-y-where-x-y-is-the-maximum-possible-value%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










$$fractextdtextdx x(N-x) = N - 2x$$
At a maximum, $fractextdftextdx = 0$, so the maximum occurs when $x = fracN2$. (Left as an exercise: show that it's a maximum and not a minimum).



Therefore you can skip the loop: desiredNumber = N / 2; is all you need. Note that if N is odd this gives the pair $leftlfloor fracN2 rightrfloor, leftlceil fracN2 rightrceil$, which is the correct solution in integers.






share|improve this answer





















  • If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
    – paparazzo
    Jun 5 at 16:37











  • @paparazzo, what's s? And if that should say 2, where are we in disagreement?
    – Peter Taylor
    Jun 5 at 16:41











  • Yes 2. In integer math 25/2 = 12 so I think need 12, 13
    – paparazzo
    Jun 5 at 16:43










  • And where are we in disagreement? desiredNumber will be assigned 12.
    – Peter Taylor
    Jun 5 at 16:50






  • 1




    @paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
    – VisualMelon
    Jun 5 at 17:12















up vote
4
down vote



accepted










$$fractextdtextdx x(N-x) = N - 2x$$
At a maximum, $fractextdftextdx = 0$, so the maximum occurs when $x = fracN2$. (Left as an exercise: show that it's a maximum and not a minimum).



Therefore you can skip the loop: desiredNumber = N / 2; is all you need. Note that if N is odd this gives the pair $leftlfloor fracN2 rightrfloor, leftlceil fracN2 rightrceil$, which is the correct solution in integers.






share|improve this answer





















  • If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
    – paparazzo
    Jun 5 at 16:37











  • @paparazzo, what's s? And if that should say 2, where are we in disagreement?
    – Peter Taylor
    Jun 5 at 16:41











  • Yes 2. In integer math 25/2 = 12 so I think need 12, 13
    – paparazzo
    Jun 5 at 16:43










  • And where are we in disagreement? desiredNumber will be assigned 12.
    – Peter Taylor
    Jun 5 at 16:50






  • 1




    @paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
    – VisualMelon
    Jun 5 at 17:12













up vote
4
down vote



accepted







up vote
4
down vote



accepted






$$fractextdtextdx x(N-x) = N - 2x$$
At a maximum, $fractextdftextdx = 0$, so the maximum occurs when $x = fracN2$. (Left as an exercise: show that it's a maximum and not a minimum).



Therefore you can skip the loop: desiredNumber = N / 2; is all you need. Note that if N is odd this gives the pair $leftlfloor fracN2 rightrfloor, leftlceil fracN2 rightrceil$, which is the correct solution in integers.






share|improve this answer













$$fractextdtextdx x(N-x) = N - 2x$$
At a maximum, $fractextdftextdx = 0$, so the maximum occurs when $x = fracN2$. (Left as an exercise: show that it's a maximum and not a minimum).



Therefore you can skip the loop: desiredNumber = N / 2; is all you need. Note that if N is odd this gives the pair $leftlfloor fracN2 rightrfloor, leftlceil fracN2 rightrceil$, which is the correct solution in integers.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 5 at 16:01









Peter Taylor

14k2454




14k2454











  • If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
    – paparazzo
    Jun 5 at 16:37











  • @paparazzo, what's s? And if that should say 2, where are we in disagreement?
    – Peter Taylor
    Jun 5 at 16:41











  • Yes 2. In integer math 25/2 = 12 so I think need 12, 13
    – paparazzo
    Jun 5 at 16:43










  • And where are we in disagreement? desiredNumber will be assigned 12.
    – Peter Taylor
    Jun 5 at 16:50






  • 1




    @paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
    – VisualMelon
    Jun 5 at 17:12

















  • If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
    – paparazzo
    Jun 5 at 16:37











  • @paparazzo, what's s? And if that should say 2, where are we in disagreement?
    – Peter Taylor
    Jun 5 at 16:41











  • Yes 2. In integer math 25/2 = 12 so I think need 12, 13
    – paparazzo
    Jun 5 at 16:43










  • And where are we in disagreement? desiredNumber will be assigned 12.
    – Peter Taylor
    Jun 5 at 16:50






  • 1




    @paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
    – VisualMelon
    Jun 5 at 17:12
















If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
– paparazzo
Jun 5 at 16:37





If N is odd then n/2 + n/s != n. A better generalization is n/2, n - n/s. OP is using integer math.
– paparazzo
Jun 5 at 16:37













@paparazzo, what's s? And if that should say 2, where are we in disagreement?
– Peter Taylor
Jun 5 at 16:41





@paparazzo, what's s? And if that should say 2, where are we in disagreement?
– Peter Taylor
Jun 5 at 16:41













Yes 2. In integer math 25/2 = 12 so I think need 12, 13
– paparazzo
Jun 5 at 16:43




Yes 2. In integer math 25/2 = 12 so I think need 12, 13
– paparazzo
Jun 5 at 16:43












And where are we in disagreement? desiredNumber will be assigned 12.
– Peter Taylor
Jun 5 at 16:50




And where are we in disagreement? desiredNumber will be assigned 12.
– Peter Taylor
Jun 5 at 16:50




1




1




@paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
– VisualMelon
Jun 5 at 17:12





@paparazzo I missed the MathJax at first as well: the stated pair is floor(N/2), ceil(N/2), which will give 12, 13 for N=25 (and otherwise equivalent to your suggestion of floor(N/2), N-floor(N/2))
– VisualMelon
Jun 5 at 17:12













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195893%2frepresent-number-n-n1-as-n-x-y-where-x-y-is-the-maximum-possible-value%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation