Calculate Complexity of T(n)? [duplicate] - function

This question already has answers here:
T(n) = T(n/10) + T(an) + n, how to solve this?
(3 answers)
Closed 2 years ago.
Given: T(n) = T(n/10) + T(an) + n for some a (which I know nothing about its value), and that: T(n) = 1 if n < 10.
I want to check if the following is possible (for some a values, and I want to find the smallest possible a):
For every c > 0 there is n0 > 0 such that for every n > n0, T(n) >= c * n Or in other words T(n)=omega(n)
Any help is appreciated.

Suppose that a < 9/10. Let c = max(1/(9/10 - a), 1), so that c ≥ 1 and 1/c ≤ 9/10 - a. Then for 1 ≤ n < 10,
T(n) = 1 ≤ n ≤ cn.
Inductively, for n ≥ 10,
T(n)
= T(n/10) + T(an) + n
≤ cn/10 + can + n
= c(1/10 + a + 1/c)n
≤ c(1/10 + a + 9/10 - a)n
= cn.
Now suppose that a = 9/10. For 1 ≤ n < 10, we know log10 n < 1 and therefore
T(n) = 1 > n log10 n - n.
Inductively, for n ≥ 10,
T(n)
= T(n/10) + T(9n/10) + n
> (n/10) log10 (n/10) - (n/10) + (9n/10) log10 (9n/10) - (9n/10) + n
= (n/10) log10 (n/10) + (9n/10) log10 (9n/10)
> (n/10) log10 (n/10) + (9n/10) log10 ( n/10)
= (n/10) (log10 n - 1) + (9n/10) (log10 n - 1)
= n log10 n - n.
Given c > 0, pick n0 such that log10 n0 - 1 = c, i.e., n0 = exp10(c + 1). Then for all n > n0,
T(n) > n log10 n - n = n(log10 n - 1) > n(log10 n0 - 1) = cn.

For every c > 0 there is n0 > 0 such that for every n > n0, T(n) >= c*n
We assume T(n) >= cn holds for some a and every c > 0.
By substituting the recurrence in the inequality and solving for a, you will get:
T(n) >= c*n
(c*n/10) + (c*a*n) + n >= c*n
a >= (9/10) - (1/c)
Since our desired outcome is to hold for all c (formally apply limit c tends to infinity, which is where RHS will maximize), we get a >= (9/10). Therefore, smallest value of a is (9/10) which will satisfy T(n) >= c*n for all c.
After this, you can prove by induction, that this is indeed the case.

In each function, you need to find the highest impacting variable of the equation. On the big scale, the smallest possible N affecting this function is any Constant.

Related

MySQL Query With Combinatorial Where Clause

Let's say I have a table with Columns A, B, C, D, E and F.
How would I query for entries where (A, B, C, D, E, F) = (1, 2, 3, 4, 5, 6) but only a subset of columns need to match? For example at least 3 out of the 6 columns have to match.
The only solution I can think of is to go through all combinations where (A, B, C) = (1, 2 ,3) or (A, B, D) = (1, 2, 4) or...
But in this example that would already be 20 where clauses, if my math is correct. Is there a better solution, that also works with more columns? Or is my only option to programmatically create a huge, non human-readable query string with hundreds of where clauses?
In MySql boolean expressions are evaluated as 1 for true or 0 for false, so you can add them in the WHERE clause:
WHERE (A = 1) + (B = 2) + (C = 3) + (D = 4) + (E = 5) + (F = 6) >= 3
Just in case any of the 6 columns is nullable, use the NULL-safe equal to operator <=> instead of =:
You can use a score system and then get the rows sorted by score. For example:
select *
from (
select t.*,
case when a = 1 then 1 else 0 end +
case when b = 2 then 1 else 0 end +
case when c = 3 then 1 else 0 end +
case when d = 4 then 1 else 0 end +
case when e = 5 then 1 else 0 end +
case when f = 6 then 1 else 0 end as score
from t
) x
where score >= 3
order by score desc
Of course, this query won't be efficient in terms of execution time, but should work well for small subsets of data.

Can not get variables to pass from function to main

I can not get main to print anything besides the variables are equal to zero which means my greedy1 function is not passing my variable and I do not know what is wrong.
def greedy1(total):
if (total >= 25):
q = total // 25
remainderCentsQ = total % 25
d = remainderCentsQ // 10
remainderCentsD = total % 10
n = remainderCentsD // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
elif (total >= 10):
q = 0
d = total // 10
remainderCentsD = total % 10
n = remainderCentsD // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
elif (total >= 5):
q = 0
d = 0
n = total // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
else:
q = 0
d = 0
n = 0
p = total // 1
return (q, d, n, p)
def printCoins1(total, q, d, n, p):
print("For " + str(total) + " cents change, I give\n" + str(q) + " quarters\n" + str(d) + " dimes\n" + str(n) + " nickels\n" + str(p) + " pennies")
def main():
total = int(input("How many cents change? "))
q = 0
d = 0
n = 0
p = 0
greedy1(total)
printCoins1(total, q, d, n, p)
main()
Inside your main function, you're calling greedy1 function with total that returns the tuple. But you're not storing this return values back to your variables q, d, n, p.
your main() should look like this:
def main():
total = int(input("How many cents change? "))
(q, d, n, p) = greedy1(total)
printCoins1(total, q, d, n, p)
#Sayas has given the correct answer however there is some redundant code , so I will improve it in my answer
def main():
total = int(input("How many cents change? "))
printCoins1(total, *greedy1(total))

Solving inequality leaves CRootOf in answer

when I try to solve this inequality x^3 + 3*x > 3 I get this output:
octave:5> eqn = x^3 + 3*x > 3
eqn = (sym)
3
x + 3⋅x > 3
octave:6> solve(eqn, x)
ans = (sym)
⎛ 3 ⎞
x < ∞ ∧ CRootOf⎝x + 3⋅x - 3, 0⎠ < x
But when I try to solve simpler inequality everything works fine:
eqn = abs(x^2-3)>3
eqn = (sym)
│ 2 │
│x - 3│ > 3
octave:4> solve(eqn, x)
ans = (sym) (-∞ < x ∧ x < -√6) ∨ (√6 < x ∧ x < ∞)
What can I do with this CRoot?

Haskell Instances required for definition

I have the following Haskell code, a function that checks how many digits are the same in a number and based on how many of them were the same takes a value. I keep getting the error:
Instances of (Ord (Int -> Int -> Int), Num (Int -> Int -> Int)) required for definition of digits
digits :: Int->Int->Int
digits x y
|(equal < 2) = 0
|(equal == 2) = 1
|(equal == 3) = 5
|(equal == 4) = 20
|(equal == 5) = 300
|(equal == 6) = 8000
|(equal == 7) = 10000
|otherwise = 100000
where
equal :: Int -> Int -> Int
equal 0 0 = 0
equal a b
|(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
|otherwise = 1 + equal (a `div` 10) (b `div` 10)
What am I doing wrong? Also what does the error mean exactly?
EDIT: Did it like this and it works like a charm!
digits :: Int->Int->Int
digits x y
|(c < 2) = 0
|(c == 2) = 1
|(c == 3) = 5
|(c == 4) = 20
|(c == 5) = 300
|(c == 6) = 8000
|(c == 7) = 10000
|otherwise = 100000
where c = equal x y
where
equal :: Int -> Int -> Int
equal 0 0 = 0
equal a b
|(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
|otherwise = 0 + equal (a `div` 10) (b `div` 10)

Excel VBA UDF Formatting - Change argument values before calculation

I am trying to write a formula in Excel VBA to calculate: RR = ((A / (A + B)) / (C / (C + D)))
When any of the four arguments (A, B, C, D) are 0, I want to change their value to 0.5 in the calculation.
Is there an easy way of doing this? I believe my formatting is wrong or I'm going about it the wrong way. Any helpful tips would be greatly appreciated.
I've tried:
Function RR(A, B, C, D) As Double
If A = 0 And B = 0 Then
A = 0.5
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 Then
A = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf B = 0 Then
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf C = 0 Then
C = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf D = 0 Then
D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 And D = 0 Then
A = 0.5 And D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
Else: RR = ((A / (A + B)) / (C / (C + D)))
End If
End Function
Try this one:
Function RR(A, B, C, D) As Double
If A = 0 Then A = 0.5
If B = 0 Then B = 0.5
If C = 0 Then C = 0.5
If D = 0 Then D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
End Function