math - given "N" find p & q such that p+q=N and p*q is maximum -


i need logic/idea behind rough solution have come with.

the problem is:

given n, 1<=n<=1000000
need find 2 numbers p , q such p + q = n && p * q = maximum.

i chose use (((n+1)/2)*((n/2))); gives me maximum product minimum sum. kind of interested in proof of logic.

any or leads?

#include<stdio.h>  typedef unsigned long uint; unsigned long calc(uint);  int main(void) {     short = 0;     uint k = 0;     scanf("%i", &i);     while(i--) {         scanf("%lu", &k);         printf("%lu\n", calc(k));     }     return 0; }  uint calc(uint k){     return (((k+1)/2)*((k/2))); } 

let

f(p) = p * (n - p) = p * n - p ^ 2 

then:

f'(p) = n - 2 * p f''(p) = - 2 

since f''(p) < 0, solving f'(p) = 0 gives local maxima.

when f'(p) = 0,  p = n / 2 => q = n / 2 

you need test end-points f(0) , f(n), both of these evaluate zero, our maxima global maximum.

note: saying of rectangles of perimeter, square has largest area.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -