Represent number N (N>1) as N = x + y, where x * y is the maximum possible value
Clash 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);
c# algorithm
add a comment |Â
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);
c# algorithm
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
add a comment |Â
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);
c# algorithm
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);
c# algorithm
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
add a comment |Â
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
add a comment |Â
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.
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'ss
? And if that should say2
, 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 assigned12
.
â Peter Taylor
Jun 5 at 16:50
1
@paparazzo I missed the MathJax at first as well: the stated pair isfloor(N/2), ceil(N/2)
, which will give12, 13
forN=25
(and otherwise equivalent to your suggestion offloor(N/2), N-floor(N/2)
)
â VisualMelon
Jun 5 at 17:12
 |Â
show 1 more comment
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.
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'ss
? And if that should say2
, 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 assigned12
.
â Peter Taylor
Jun 5 at 16:50
1
@paparazzo I missed the MathJax at first as well: the stated pair isfloor(N/2), ceil(N/2)
, which will give12, 13
forN=25
(and otherwise equivalent to your suggestion offloor(N/2), N-floor(N/2)
)
â VisualMelon
Jun 5 at 17:12
 |Â
show 1 more comment
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.
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'ss
? And if that should say2
, 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 assigned12
.
â Peter Taylor
Jun 5 at 16:50
1
@paparazzo I missed the MathJax at first as well: the stated pair isfloor(N/2), ceil(N/2)
, which will give12, 13
forN=25
(and otherwise equivalent to your suggestion offloor(N/2), N-floor(N/2)
)
â VisualMelon
Jun 5 at 17:12
 |Â
show 1 more comment
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.
$$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.
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'ss
? And if that should say2
, 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 assigned12
.
â Peter Taylor
Jun 5 at 16:50
1
@paparazzo I missed the MathJax at first as well: the stated pair isfloor(N/2), ceil(N/2)
, which will give12, 13
forN=25
(and otherwise equivalent to your suggestion offloor(N/2), N-floor(N/2)
)
â VisualMelon
Jun 5 at 17:12
 |Â
show 1 more comment
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'ss
? And if that should say2
, 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 assigned12
.
â Peter Taylor
Jun 5 at 16:50
1
@paparazzo I missed the MathJax at first as well: the stated pair isfloor(N/2), ceil(N/2)
, which will give12, 13
forN=25
(and otherwise equivalent to your suggestion offloor(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
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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