solving T(n) = 2T(n/2) + log n [closed] - divide-and-conquer

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am trying to solve T(n) = 2T(n/2) + log n
substituted n = 2^k
T(2^k) = 2T(2^(k-1)) + k
T(2^k) = 2^2 T(2^(k-1)) + 2(k-1) + k
after k steps
T(2^k) = 2^k T(1) + 2^(k-1) + 2 * (2^(k-2)) +....+k
So basically I need to sum a term of i*2^i where i = 1 to log n - 1.
And I could not find an easy way to sum these terms. Am I doing something wrong ? Is there any other way to solve this recursion ? would master theorem work her ? if yes than how ?
Thanks.

first you should define a recursive export,say T(1)
then:
since T(2^k) = 2T(2^(k-1)) + k; *
we define g(k) = T(2^k)/2^k;
then * come into:
g(k) = g(k-1) + k/2^k = g(1) + sum(i/2^i); i=2,3,4...k
where g(1) = T(1)/2 = c;
where you could then unfold the sum expression and define it = y;
then unfold the expression of y/2;
y-y/2 is a geometric progression, so youcan solve it
as I worked out, sum = 3/2 - (k+2)/2^k;
so T(n) = 2^k * g(k) = (3/2+c)*n - (2+logn)

Wolfram|Alpha gives a closed form solution:
for a constant c_1 that is fixed by the initial condition.
By the way, log(n)/log(2) = lg(n), where lg is the base two logarithm.

Related

How to solve "X^2 - Y is perfect square, X, Y ≤ N" in O(sqrt(N)) time complexity? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
Given N. Find number of all the integer pairs x, y such that
1<=x, y<=N
and x^2 - y is a perfect square
the N is large but O(sqrt(N)) will be fine to solve this problem.
I tried to solve this problem like, letting z^2 be the square number
x^2 - z^2 = y = (x+z)(x-z)
then let x + z = p and x - z = q;
then x = (p+q)/2 and z = (p-q)/2;
and (p+q)/2<=N;
and p and q should have same parity (both even or odd as (p+q)/2 is integer)
also pq<=N
Now I don't know how to proceed from here
or tell me some other method to solve this problem efficiently.
This solution solves the problem in O(sqrt N).
Rephrasing the problem
Let z^2 = x^2 - y, z ≥ 0, or equivalently 0 < y = x^2 - z^2 ≤ N
We need pairs of perfect squares under N^2 whose differences are less than or equal to N. By arithmetic series,
1 + 3 + 5 + 7 + ... + (2k - 1) = k^2
That means x^2 - z^2 is a sum of some n consecutive odd integers.
Counting odd integers
z^2 + (2z + 1) + (2z + 3) + ... + (2x - 1) = x^2. Apply arithmetic series formula
z^2 + n/2 * (4z + 2 + 2(n - 1)) = x^2
z^2 + n * (2z + n) = x^2
n(2z + n) ≤ N
z ≤ floor((N/n - n)/2)
We are thus able to find the last values of z for which at least n+1 odd consecutive integers are needed for their sum to exceed N.
For each z, the x can be z+1, z+2 ... z+n, for a total of n pairs.
#include <cmath>
#include <iostream>
int N = 99;
int main(void){
int z = -1;
// z = 0 is valid for x^2 < N, so -1 is largest invalid z.
int count = 0;
for (int n = std::sqrt(N); n > 0; n--){
int zNew = (N/n - n)/2;
// zNew is max z that has n perfect squares from z + 1 to z + n
count += (zNew - z) * n;
z = zNew;
}
std::cout << count << '\n';
}
A version in Java passed these unit tests.
(N, count) = (1, 1), (3, 2), (5, 4), (8, 6), (60, 68), (99, 124), (500, 808)

I need answer of a question about binomial distribution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Let X be a binomial random variable with mean Np and variance Np(1−p).
Show that the ratio X/N also has a binomial distribution with mean p and
variance p(1 − p)/N.
Let r = X/N. Since X has a binomial distribution, r also has the
same distribution. The mean and variance for r can be computed as follows:
Mean , E[r] = E[X/N] = E[X]/N = (N p)/N = p .
Variance , E[(r − E[r])2] = E[(X/N − E[X/N])2] = E[(X − E[X])2]/N2 = N p(1 − p)/N2 = p(1 − p)/N

How to write a log base 10 function in c++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know that it may seem a duplicate question, but I could not find my answer in previous questions.
I mean how to write a log base 10 function by simple loops and not using built in log function in c++.
The easiest way is to calculate the natural logarithm (ln) with a Taylor series. Once you have found the natural logarithm, just divide it by ln(10) and you get the base-10 log.
The Taylor series is quite simple to implement in C. If z is the number for which you are seeking the log, you just have to loop a few iterations multiplying an accumulator by (z-1) each time. To a limit, the more iterations you run, the more accurate your result will be. Check it a few times against the libC log10() version until you are happy with the precision.
This is a "numeric approach". There are other numeric solutions to finding the logarithm of a number which can give more accurate results. Some of them can be found in that Wikipedia link I gave you.
Assuming by "log base 10" you mean "the number of times n can be divided by 10 before resulting in a value < 10":
log = 0;
// Assume n has initial value N
while ( n >= 10 ) {
// Invariant: N = n * 10^log
n /= 10;
log += 1;
}
You'll get faster convergence with Newton's Method. Use something like this (hand written not compiled or tested uses f(r) = 2**r - x to compute log2(x) ):
double next(double r, double x) {
static double one_over_ln2 = 1.4426950408889634;
return r - one_over_ln2 * (1 - x / (1 << static_cast<int>(r)));
double log2(double x) {
static double epsilon = 0.000000001; // change this to change accuracy
double r = x / 2;. // better first guesses converge faster
double r2 = next(r, x);
double delta = r - r2;
while (delta * delta > epsilon) {
r = r2;
r2 = next(r, x);
delta = r - r2
}
return r2;
}
double log10(double x) {
static double log2_10 = log2(10);
return log2(x) / log2_10;
}

Simplify (a + b) XOR (c + b) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to simplify (a+b)xor(c+b)? What is the contribution of b to the final result?
Note that I'm mixing boolean algebra with arithmetic, xor is a bitwise exclusive or on corresponding bits and + is a standard addition on 8 bits, that wraps around when overflown.
a, b, c are unsigned char;
We can use an SMT solver to test our hypothesis that your formula can be simplified. You can head over to http://rise4fun.com:
x = BitVec('x', 8)
y = BitVec('y', 8)
z = BitVec('z', 8)
print simplify((x + z) ^ (y + z))
and the result, anticlimactically, is:
x + z ^ y + z
Which means your formula cannot be further simplified.
(a+b)xor(c+b)
--------------
=((not(a+b))*(c+b))+((a+b)*(not(c+b)))
-----------------------
=((not a)*(not b)*(c+b))+((a+b)*(not c)*(not b))
----
=((not a)(not b)*c) + (a*(not c)(not b))
----
=(not b)((not a)c + a(not c))
----
=(not b)(a xor c)
----

Calculate modulus for large numbers

Hi I need to calculate (2^n + (-1)^n) % 10000007
where 1 < n < 10^9
How should I go about writing a program for it in c++?
I know this mod property
(a + b)%n = (a%n + b%n)%n but this wont help me.
Given
(a + b)%m = (a%m + b%m)%m
Then, replace both a and b with the same power of 2, and you get the recurrence:
2k+1%m = (2k%m + 2k%m)%m
You probably already figured your formula allows you to break down your problem into:
(2n + (-1)n)%P = (2n%P + (-1)n%P)%P
Then, note that (-1)k is either 1 or -1, and you should be able to calculate your problem in O(n) time.