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 8 years ago.
Improve this question
Kind of a math question, but very programming related. Doing some Big-O problems and I have an algorithm where a for loop will run n times, where k = input size, n = max power of 4 where (k)/(4^n) >= 1. How can I represent max power of 4 where (k)/(4^n) >= 1 in one mathematic statement?
floor ( (log k)/(log 4) ).
Or something along those lines.
Mathematic statement: [log_4(k)]
Code: floor( log(k) / log(4) )
log base 4 of k? Can take the floor if you only care about integer n.
Taking (k)/(4^n) >= 1, multiply both sides by 4^n to get k >= 4^n, and then take the log base 4 (log_4) of both sides to get log_4 k >= n, or n <= log_4 k. (Equivalently, take log of both sides and get log k >= log(4^n), then note log(4^n) = n log(4), and divide to get (log k)/(log 4) >= n). Choose the largest integer n satisfying this inequality, which is floor(log_4 k).
Related
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 7 years ago.
Improve this question
Apparently the correct answer to the following question is (C), but why are the other options not correct until we know the value of n?
If n=1, all of these seem correct except (B)! Where am I going wrong?
Which of the following is not O(n2)?
(A) 1510 * n + 12099
(B) n1.98
(C) n3 / √n
(D) 220 * n
Wikipedia says :-
Big O
notation describes the limiting behavior of a function when the
argument tends towards a particular value or infinity, usually in
terms of simpler functions. A description of a function in terms of big O notation usually only
provides an upper bound on the growth rate of the function.
An upper bound means that f(n) = n can be expressed as O(n), O(n2), O(n3), and others but not in other functions like O(1), O(log n).
So, going in the same direction, now you can easily eliminate options as shown below :-
1510 * n + 12099 < c * n2, for some n > √12100 i.e. n > 110 --- hence is O(n2).
n1.98 < n2, for all n > 1 --- and is O(n2).
n3 / √n = n5/2 > n2 for all n > 1 --- hence, isn't O(n2).
220 * n = 1024*1024*n < c* n2 for all n > 1 ,where c = 1024*1024 --- hence, is O(n2).
Hence, the only option which doesn't satisfy O(n2) is option C,i.e., f(n) = (n^3 / (sqrt(n))). Here, so, (n3 / (sqrt(n))) isn't equal to O(n2).
While Shekhar Suman’s answer explains why the official answer is right in each case, it does not answer this part of the question: “Apparently the correct answer to the following question is (C), but why are the other options not correct until we know the value of n? If n = 1, all of these seem correct except (B)! Where am I going wrong?” (my highlighting).
I would suggest that the first two parts I have put in bold indicate what the questioner – at the time of asking – had not grasped, namely that O-notation is used to express information about functions, not about particular values of expressions.
This means it makes no sense to say “If n = 1, f(n) is O(n2)”, as that is a statement about one value, f(1), and not about the function f.
Similarly, “until we know the value of n” makes no sense when talking about functions, because n is a bound variable, i.e. is only used to relate n to an expression involving n in order to define a function.
None of the options are of order n^2.
(15^10) * n + 12099 is of order n
n^1.98 is of order n^1.98
n^3 / (sqrt(n)) is of order n^2.5
(2^20) * n is of order n
You can check whether two functions are of the same order, by dividing one over the other. It should tend to go to a constant when n goes to infinity.
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 8 years ago.
Improve this question
Here's an equation:
d is the multiple inverse of 3 modulo K.
Assuming I have d, can I find K?
Also, K is not necessarily prime.
Thanks!
You know that
d*3 = 1 (mod K)
this means
d*3 = 1 + n*K
independently of K this however means
d*3 = 1 (mod n)
i.e. that d is the inverse of 3 modulo n too, thus the answer is in general not unique (actually you can use any divisor of nK as answer).
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 9 years ago.
Improve this question
I am working on a histogram problem, and then I ran into the following math problem:
Given N numbers, where the value of each number is different, denoted as v1, v2, ..., vn, and the probability of selecting each number is p1, p2, ..., pn, respectively.
Now if I select K numbers based on the given probabilities, where K <= N, what is the expectation of the sum of those K numbers? Note that the selection is without replacement, so that the K numbers cannot involve duplicate numbers. I understand that if the selection is with replacement, the expectation of the sum of the K numbers equals K * E(V), where E(V) = v1*p1 + v2*p2 + ... + vn*p2.
Furthermore, what about the expectation of the variance of those K numbers?
This question is better formulated at here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What would be a computationally feasible pseudocode of any prime-counting function implementation?
I initially attempted coding the Hardy-Wright algorithm, but its factorials began generating miserable overflows, and many others appear bound to yield similar problems. I've scoured Google for practical solutions, but, at best, have found very esoteric mathematics which I haven't ever seen implemented in conventional programs.
The prime-counting function pi(x) computes the number of primes not exceeding x, and has fascinated mathematicians for centuries. At the beginning of the eighteenth century, Adrien-Marie Legendre gave a formula using an auxiliary function phi(x,a) that counts the numbers not greater than x that are not stricken by sieving with the first a primes; for instance, phi(50,3) = 14 for the numbers 1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 and 49. The phi function can be computed as phi(x,a) = phi(x,a-1) - phi(x/P(a),a-1), where phi(x,1) is the number of odd integers not exceeding x and P(a) is the a-th prime number (counting from P(1)=2).
function phi(x, a)
if (phi(x, a) is in cache)
return phi(x, a) from cache
if (a == 1)
return (x + 1) // 2
t := phi(x, a-1) - phi(x // p[a], a-1)
insert phi(x, a) = t in cache
return t
An array p stores the a-th prime for small a, calculated by sieving. The cache is important; without it, run time would be exponential. Given phi, Legendre's prime-counting formula is pi(x) = phi(x,a) + a - 1, where a = pi(floor(sqrt(x))). Legendre used his formula to calculate pi(10^6), but he reported 78526 instead of the correct answer of 78498, which, even though wrong, was astonishingly close for an intricate manual calculation.
In the 1950s, Derrick H. Lehmer gave an improved algorithm for counting primes:
function pi(x)
if (x < limit) return count(primes(x))
a := pi(root(x, 4)) # fourth root of x
b := pi(root(x, 2)) # square root of x
c := pi(root(x, 3)) # cube root of x
sum := phi(x,a) + (b+a-2) * (b-a+1) / 2
for i from a+1 to b
w := x / p[i]
lim := pi(sqrt(w))
sum := sum - pi(w)
if (i <= c)
for j from i to lim
sum := sum - pi(w / p[j]) + j - 1
return sum
For example, pi(10^12) = 37607912018. Even with these algorithms, and their modern variants, and very fast computers, it remains appallingly tedious to calculate large values of pi; at this writing, the largest known value is pi(10^24) = 18435599767349200867866.
To use this algorithm to calculate the n-th prime, a corollary to the Prime Number Theorem bounds the n-th prime P(n) between n log n and n(log n + log log n) for n > 5, so compute pi at the bounds and use bisection to determine the n-th prime, switching to sieving when the bounds are close.
I discuss prime numbers in several entries at my blog.
Wikipedia might help too. The article on prime counting contains a few pointers. For starters I'd recommend the algorithm by Meissel in the section "Algorithms for evaluating π(x)", which is one of simplest algorithm that does not generate all primes.
I also find the book by Pomerance and Crandall "Prime numbers a computational perspective" helpful. This book has a detailed and quite accessible description of prime counting methods. But keep in mind that the topic by its nature is a bit too advanced for most of the reader here.
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 9 years ago.
Improve this question
T(1) = 1
T(n) = T(n^1/3) + 1
How can I solve it? By "solve" I mean to found it's "complexity" (I don't really know how to say it in English), such as O(nlogn) ecc.
I couldn't guess the substitution method; i go nowhere with the iteration method, and I can't apply Master Theorem.
I'm arrived here, but I'm not sure:
T(n) = T(n^(1/3^k))) +k
Can you give me and advice please?
I'll try to formulate some possible solutions. You can pick one depending on further constraints.
The recursion will run until n becomes 1. This is:
1 = n^(1/3^k)
or more generally
b = n^(1/3^k)
where k is the recursion depth. Solving this for k yields:
ln(b) = 1/3^k * ln(n)
ln(ln(b) / ln(n)) = k * ln(1/3)
-ln(ln(b) / ln(n)) / ln(3) = k
If we set b to 1, then the equation becomes unsolvable, because ln(0) is not specified. This would be equivalent to an endless recursion.
However, we can say that in the last recursion n should be "roughly 1". So we actually have a b != 1. Then k is:
k = -ln(ln(b) / ln(n)) / ln(3)
= -ln(c1 / ln(n)) / c2
= -(ln(c1) - lnln(n)) / c2
= (-c3 + lnln(n)) / c2
This should be O(log log n).
If you want to truncate n to its integer part, the calculation becomes pretty messy, because you have special cases after each step. However, we could approximate the result by specifying b = 1.999999. This would yield the same complexity as above.
If this is a recursive function, so what i did understand is that
T(n)= T(integerpart(cubicsquare(n)) +1 ;
in this case :
S=0;
if (n>=1){
S++;
N= n;
while (N>1){
N=integerpart(N^1/3);
S++;
}
}
T(n)= S ;
that's mean that T(n) is a simple function : with integer bound, and the width of the keme interval is 2^(3^k) - 2^(3^(k-1))
you can see, first interval is if n in ]1,8[ T(n)=2; then if n in [8,252[ ,T(n)=3...
so, as we can say then that t(2^(3^k)) = k+1 ;
then t(n) ~O(ln(ln(n))/ln(3)) (consider suite 2^3^k)