calculating recurrence relation T(n) = n+ T(n/2) [closed] - recurrence

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 7 years ago.
Improve this question
T(n) = n + T(n/2)
= n + n/2 + T(n/4)
= n + n/2 + n/4 + T(n/8)
= n + n/2 + n/4 + ... + n/(2^(k-1)) + T(n/2^k)
->>>
and I don't know how to go on to get big Oh formula.
please help me

I'm assuming there's some kind of initial condition like T(1) = 0 that you are not telling us.
If so, the answer is O(log n).
Think about how you would work out T(2), T(4), T(8), T(16) etc. Each one requires just one extra step.
T(1) = T(2^0) calls the method recursively 0 times.
T(2) = T(2^1) calls the method recursively 1 time
T(4) = T(2^2) calls the method recursively 2 times
T(8) = T(2^3) calls the method recursively 3 times
In other words, the number of steps is the power. This means that you have to take logarithms to get the answer.

Related

Numbers equal to the sum of powers of its digits, need faster python code [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
In Python, create a function finding all integers between 1 and 1.000.000.000, which are equal to the sum of k'th power of its digits for some k.
Example: 4150 = 4^5 + 1^5 + 5^5 + 0^5.
Here is my code working perfect until 1.000.000. However, it becomes so slow for large numbers. I try Numpy but it does not work. Any improvement would be welcome. ^^ (Any library or python function you may use)
def powers(n):
s = []
for number in range(1, 10 ** n):
number_list = [int(x) for x in str(number)]
if max(number_list) > 1:
total = 0
power = 1
while total < number:
total = sum([y ** power for y in number_list])
if total == number:
s.append((number, power))
power += 1
return s

How do I find the memory location of items on the stack? [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 8 years ago.
Improve this question
In an interview, I was asked:
You are given a stack with starting address of 0th. The value of the stack is 1000 and each location can store 8 bytes of data. What is the memory location of 42nd element ?
Let's look at some values and try to find a pattern.
0 -> 1000
1 -> 1008
2 -> 1016
It starts at 1000 and goes up by 8 each time, so
n -> 1000 + 8*n
42 -> 1000 + 8*42
42 -> 1336
It is quite simple.
Element[0] = Memory[1000]
sizeof(Element) = 8
Then:
Element[42] = Memory[1000 + 8*42]
#Time S.
The statement "it grows up" is not necessarily true.
It can also grow down.
So you second example is more correct. N can be negative in that case.

Exponential Moving Average - Ruby [closed]

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.
Improve this question
I've been using the simple_statistics gem, but im looking to calculate the EMA on the last x records. For example (when calculating WMA)
#stockticker.ema10 = s.stocktickers.last(10).map(&:current_price).map{|f| f.to_f}.wma
I was wondering if anyone can provide advise on how I got about calculating the EMA in rails?
It looks like the Moving Averages gem might be what you're looking for. Here is a copy of their exponential_moving_average method for reference as well.
class Array
def exponential_moving_average(idx=nil, tail=nil)
idx, tail = idx_and_tail_or_defaults(idx, tail)
valid_for_ma(idx, tail)
alpha = 2.0 / (tail + 1)
n = (1..tail).to_a.map{|tidx| (1 - alpha) ** (tidx - 1) * self[idx - tidx + 1]}.sum
d = (1..tail).to_a.map{|tidx| (1 - alpha) ** (tidx - 1)}.sum
n / d
end
alias_method :ema, :exponential_moving_average
end
I am the developer of Statsample-timeseries gem which is an extension of Statsample, an advance statistical suite in Ruby. It has quite many statistical methods (including EMA) which you can perform on your data.
If you need any assistance, I will be very happy to help out. :)

Recurrence Relation working [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 8 years ago.
Improve this question
I am doing a data structures and algorithms paper in which recurrence relations are being taught.
The question is as follows:
From what I understand from this question, n will keep on being halved over and over again. So what you are left with is 1/32n^2 + 1/16n^2 + 1/8n^2 + 1/4n^2 + 1/2n^2 + n^2. All the fractions sum to 1. So you're left with n^2 +n^2 = 2n^2.
However this is not a possible solution.
Can somebody please help me understand how to calculate these recurrence relations correctly, or point me in the right direction because I am having a lot of trouble with this topic and any help with be greatly appreciated.
Thank you for your time.
You might want to look at the Master Theorem
In the wiki, a = 1, b = 2, c = 2, where T(n) = aT(n/b) + n^c
Case 3 applies, since 2 > 0 = log_2(1)
Thus, by the master theorem, T = Big-Theta(n^c) = Big_Theta(n^2).
Choice B has a n^2 term, so that should be your answer.

Maximum number of elems on stack for infix -> postfix translation

I got this as an interview question.
What is the maximum number of elements that can be on stack at a
specific moment while doing a translation from infix form to reversed
postfixed Polish form?
I know that the principle is that on the stack there cannot be elements with higher priority (usually * and /) under the ones with smaller priority (+ and -). I tried making an algorithm keeping track of a global and local maximum number, but I didn`t found out a certain rule.
For example if i have infix: 2 - 3 * 4 * 5 / 1 + 10
Stack 1: - * * / => maxLocal = 4 maxGlobal = 4
Stack 2: (After eliminating /, * and * because + has lower priority) - +
=> maxLocal = 2 maxGlobal = 4
Can you please help me?
I think there is no limit. For example, take the following infix expression: (1 + (1 + (1 + (1 + (1 + (1 + (1 + … It is very deep, every time you push yet another element to the stack. Of course there is usually some limit to the number of parentheses you parser accepts, but such limit is purely practical (to prevent stack overflow), not theoretical.

Resources