I tried to solve a recurrence relation.
$$
\def\r #1{\langle r^{#1} \rangle}
0 = 8 n E \r{n - 1}
+ (n - 1)[n(n - 2) - 4l(l + 1)] \r{n - 3}
+ 4(2n - 1) \r{n - 2}
$$
This equation is from quantum mechanics. It derived from the Hamitonian with Coloumb potential.
$$
H = \frac{1}{2} p^2_r
+ \frac{l(l + 1)}{2 r^2}
- \frac{1}{r}
$$
E and l are symbols, <r^n> is the expected value of r^n repect to the energy eigenstate.
I have a Sage script using SymPy to solve the recurrence relation. My goal is to express <r^n> as a function of E and l. Here is my script.
import sympy
from sympy import Function, rsolve
from sympy.abc import n
l = sympy.symbols('l',integer=True)
energy = sympy.symbols('E')
r = Function('r')
f = 8 * n * energy * r(n - 1) \
+ 4 * (2 * n - 1) * r(n - 2) \
+ (n - 1) * (n * (n - 2) - 4 * l * (l + 1)) * r(n - 3)
print(rsolve(f, r(n), {r(0): 1}))
I don't know why the output result is None. I have tried to set l and energy to explicit interger, for example, 1. But it didn't help.
Expected result
I am sorry. I don't know. The recurrence relation is too hard for my brain. I am not good at math.
Output from print
There is no error, and below is the output.
None
Extra question
If my recurrence relation doesn't have a general solution, is it possible to get the results of specific r(n), for example r(10)?
Reply for my extra question
I figured out the recursive method to generate the result of specific r(n).
from functools import cache
#cache
def expected_distance(n, l):
if n <= -2:
return 0
if n == -1:
return -2 * energy
if n == 0:
return 1
return simplify((
- 4 * (2 * (n + 1) - 1) * expected_distance(n - 1, l) \
- n * ((n + 1) * (n - 1) - 4 * l * (l + 1)) * expected_distance(n - 2, l)
) / (8 * (n + 1) * energy))
Related
I'm trying to understand how complex numbers get multiplied. when I multiply two numbers it always seems to give me an arbitrary amount. for example,
complex(10,9)*complex(11,13) equals complex(-7,229)and complex(10,1)*complex(10,2) equals complex(98,30). is there a mathematical way to figure out 2 complex numbers multiplied and if so, what is is it?
The result is not arbitrary, it is following the definition of complex multiplication:
For example if you have
x = a + j * b
y = c + j * d
then the expression for multiplication is
x * y = (a * c - b * d) + j (a * d + b * c)
For your example complex(10,9) * complex(11,13) that would evaluate to
(10 * 11 - 9 * 13) + j * (10 * 13 + 9 * 11)
(-7 + 229j)
which is exactly what Python shows
>>> complex(10,9) * complex(11,13)
(-7+229j)
Complex number multiplication operates in this way:
(a + ib) * (c + id) = a * c + a * id + ib * c + ib * id
= a * c - b * d + i(a * d + b * c)
So, in Python, the result is like this:
complex(a, b) * complex(c, d) = complex(a * c - b * d, a * d + b * c)
Example:
complex(10, 9) * complex(11, 13) = complex(10 * 11 - 9 * 13, 10 * 13 + 9 * 11)
= complex(-7, 227)
If you have 2 complex numbers, the first a + bj, and the second c + dj, then the product (a + bj) * (c + dj) = a*c - b*d + (a*d + b*c)j. The way to think about it is that j is equal to the square root of -1, so j*j = -1, and then just multiply out the brackets as normal. See below:
a, b = 10, 9
c, d = 11, 13
print(complex(a, b)*complex(c, d))
print(a*c - b*d, a*d + b*c)
Output:
(-7+229j)
-7 229
Another way to understand complex number multiplication is geometrically. We can think of complex numbers as two dimensional vectors, things with a length and direction. Then when multiplying a complex number with length r and direction a by another with length s and direction b, you get a complex number with length r*s and direction a+b, i.e. a complex number with length r and direction a acts on others by multiplication by scaling them by r and rotating them through a.
If you work out the lengths and directions of your (10,9) and (11,13) and combine them as above, you will get the length and direction of (-7,229)
import sys
t=(int(sys.stdin.readline()))
for i in range(0,t):
n=int(sys.stdin.readline())
c=0
s=n*(n+1)/2
if s%2!=0:
print(0)
else:
c=0
i=-1
a=[i for i in range(1,n+1)]
h=s//2
m=0
s1=0
for i in range(n-1,-1,-1):
s1+=a[i]
c+=1
if s1==h:
m=1
break
if s1>h:
break
if m==1:
s1=((c+1)*(2+((c-1)-1)))//2+((n-c-1)*(2+((n-c-1)-1)))//2
print(s1)
else:
print(c)
I am new to python , How can i write this code with using for loop? i don't want to use for loop because i get TLE error. Thanks in advance
Here is the question :
N. Consider the sequence sequence=(1,2,…,N). You should choose two elements of this sequence and swap them.
A swap is perfect if there is an integer o (1≤o<N) such that the sum of the first M elements of the resulting sequence is equal to the sum of its last N−o elements. Find the number of perfect swaps.
i got interested in the problem and found this so far:
a slow version that creates list and really does swap elements is this:
from itertools import combinations
def slow(N):
found = 0
for i, j in combinations(range(N), 2):
lst = list(range(1, N + 1))
lst[i], lst[j] = lst[j], lst[i]
for m in range(1, N):
a = m * (m + 1) // 2
b = (N - m) * (N + m + 1) // 2
if i < m <= j:
a = a - i + j
b = b - j + i
assert a == sum(lst[:m])
assert b == sum(lst[m:])
if sum(lst[:m]) == sum(lst[m:]):
found += 1
if i < m <= j:
assert 2 * m * (m + 1) + 4 * j == N * (N + 1) + 4 * i
else:
assert 2 * m * (m + 1) == N * (N + 1)
else:
if i < m <= j:
assert 2 * m * (m + 1) + 4 * j != N * (N + 1) + 4 * i
else:
assert 2 * m * (m + 1) != N * (N + 1)
return found
as you see i found criteria the indices have to fulfill in order for the sum to be correct:
if i < m <= j:
assert 2 * m * (m + 1) + 4 * j == N * (N + 1) + 4 * i
else:
assert 2 * m * (m + 1) == N * (N + 1)
i also found the direct formula to calculate the sum up to m and the one starting from m:
a = m * (m + 1) // 2
b = (N - m) * (N + m + 1) // 2
if i < m <= j:
a = a - i + j
b = b - j + i
all of that can can be calculated using some basic mathematics.
starting from that you can do some more maths and see that there are 2 cases to consider:
there is an m such that the sum of the original list [1, 2, 3, ..., m, m+1, ..., N] up to m equals the sum of the rest of the list (e.g. N = 20; m = 14). two cases again:
all the swaps that do not cross the m boundary are valid (there are comb(m, 2) + comb((N - m), 2)) of them.
when you split at m-1 you will find more swaps; this time you have to swap accross the m-1 boundary.
the m in that case is calculated from
m = - 1 + sqrt(1 + 2 * N * (N + 1)) / 2
the calculation for m in the first case is not an integer (i.e. 1 + 2 * N * (N + 1) is not a perfect square). the m to consider is then then the floor of the result of the formula above (i use int instead of math.floor). two cases again for the difference of the sum of the two splits:
the difference is even: there are more swaps that need to go over the m boundary.
the difference is odd: no additional swaps (swapping will always result in an even difference)
this is the code:
from math import sqrt, comb
def fast(N):
found = 0
arg = (1 + 2 * N * (N + 1))
sq = round(sqrt(arg))
if sq ** 2 == arg and sq & 1:
m = (-1 + sq) // 2
found += comb(m, 2) + comb((N - m), 2)
m -= 1
found += N - m - 1
else:
m = int((-1 + sqrt(arg)) // 2)
diff = ((m + 1 + N) * (N - m) - m * (m + 1)) // 2
if diff & 1 == 0:
found += N - m
return found
How to print this pattern? I have done regular patterns but I am unable to get odd stars.
You could use a simple list comprehension:
n = 5
print("\n".join((a := [("* "*i).center(n*2 + 1) for i in range(1, n + 1) if i&1]) + a[::-1][1:]))
*
* * *
* * * * *
* * *
*
You can change n to anything you want
I wrote a code for finding the 'j' for the General Annuity
from __future__ import division
R = float(38973.76)
n = int(3)
r = int(8)
m = int(4)
mSubj = int(1)
t = int(3)
ans = ((1 + r / m) ** m)**(1 / mSubj) - 1
print(ans)
now the answer is 80.0
instead of 0.008243216 that I solved in Scientific Calculator.
Your R variable is the capital one, not r.
so it should be ans = ((1 + R / m) ** m)**(1 / mSubj) - 1
I am trying to solve this problem in Python. Noting that only the first kiss requires the alternation, any kiss that is not a part of the chain due to the first kiss can very well have a hug on the 2nd next person, this is the code I have come up with. This is just a simple mathematical calculation, no looping, no iteration, nothing. But still I am getting a timed-out message. Any means to optimize it?
import psyco
psyco.full()
testcase = int(raw_input())
for i in xrange(0,testcase):
n = int(raw_input())
if n%2:
m = n/2;
ans = 2 + 4*(2**m-1);
ans = ans%1000000007;
print ans
else:
m = n/2 - 1
ans = 2 + 2**(n/2) + 4*(2**m-1);
ans = ans%1000000007
print ans
You're computing powers with very large exponents, which is extremely slow if the results are not reduced in process. For example, a naive computation of 10**10000000 % 11 requires creating a 10000000-digit number and taking modulo 11. A better way is modular exponentiation where you reduce modulo 11 after each multiplication and the integer never gets larger.
Python provides built-in modular exponentiation. Use pow(a,b,c) to compute (a**b) % c.
This is under assumption that your algorithm is correct, which I did not verify.
The answer to this is a pretty simple recursion. F(1) = 2 and for F(n) we have two choices:
n = H, then the number of ways to kiss the remaining guests is simply F(n-1)
n = K, then the number of ways to kiss the remaining guests is 2 ** k where k is the number of remaining guests that the princess is not forced to kiss. Since she has to kiss every second remaining guest, k = ceil((n - 1) / 2)
Putting them together, we get F(n) = F(n - 1) + 2 ** ceil((n - 1) / 2)
My attempt, including taking everything mod 1000000007:
from math import ceil
def F(n):
m = 1000000007
a = 2
for i in range(2, n+1):
a = (a + pow(2, int(ceil((i - 1.0) / 2)), m)) % m
return a
EDIT: Updated (much faster and more unreadable! F(1e9) takes about 3 minutes):
def F(n):
m = 1000000007
a = 2
z = 1
for i in xrange(2, n, 2):
z = (z * 2) % m
a = (a + z + z) % m
if (n & 1 == 0):
z = (z * 2) % m
a = (a + z) % m
return a
EDIT 2: After further thought, I realised the above is actually just:
F(n) = (1 + 1) + (2 + 2) + (4 + 4) + ... + (2 ** n/2 + 2 ** n/2)
= 2 * (1 + 2 + 4 + ... + 2 ** n/2)
= 2 * (2 ** (n/2 + 1) - 1)
= 2 ** (n/2 + 2) - 2
But if n is even, the last 2 ** n/2 only occurs once, so we have:
def F(n):
m = 1000000007
z = pow(2, n/2, m)
if (n % 2 == 0):
return (z * 3 - 2) % m
else:
return (z * 4 - 2) % m
Which runs much faster! (Limited by the speed of pow(x, y, z), which I think is O(lg n)?)
And just because, here is the one-liner:
def F(n):
return (pow(2, n/2, 1000000007) * (3 + n % 2) - 2) % 1000000007
Results:
1 => 2
2 => 4
3 => 6
4 => 10
5 => 14
6 => 22
7 => 30
8 => 46
9 => 62
10 => 94
1e6 => 902893650
1e7 => 502879941
1e8 => 251151906
1e9 => 375000001