c1 = ([message] + b'good')^e mod N
c2 = ([message] + b'hello')^e mod N
we got c1,c2 and e, N.how to get message recover
e.g:
from Crypto.Util.number import *
N = getPrime(1024) * getPrime(1024)
e = 65537
m = b'asdasdasdasdasdasdasdad'
print(N)
print(pow(bytes_to_long(m) + bytes_to_long(b'hello'), e, N))
print(pow(bytes_to_long(m) + bytes_to_long(b'good'), e, N))
# 13824259202707707704698945750555650188923092989268878762501795757989658824989810874884266999337165342953213211887061514825958553609144117703703258701326022555786135220477315399712884125614387173381827614381256300829440707734494820382614289703908717491613414182027679499244735432109060499149532381212541977764000110184519399366525605760733146179772941429335256079914560952785133589850388762981579475642697758871309468524199989272654149503691316057495226800070232198670430646266325131194854572267338487885035101469575839387407242537184807389659155489522800266906762308283506648079055576871122888512049475856330890001343
# 8276659109712793815299578561719138578174632968651765806041202217454912208960034861689360987329311934883429921704719276734783502306194357901541231165498953390016674194748506249298549911771734899753148678798552104754999924151283524844665822574622839869787064551474267788166961708626565174688777456346982873550222301030207711832604295851347969452338879329997396351375967501075989830884016503399111439211560006966836981816154790123070674190106461105668194452404913979853606956887928831700424160777167891676755674297360337554607504753418365643045588301072415879765662558357415118404821285358454161314770144838910923623504
# 12062529732270479369398408885987121015035665116730823919415100374357168027361875295561106591018597251449600246188613002097103212665616638658219288950827495971784194429969249895507132940672081924143164246070195738418425526032150020095433941736897502046253164728866258960451123211291798117834562763926759386287252564794804678055732450470110455829664228632583208321124476416193489413745132190993800532396985948065271318259684439260205668490905514290180662532835602885190966567491432319734167361123863401995933808462898545858708457817527307705396477763226807528727397866105985550723953556595591642168706497014266952344649
Related
I want to use the limits (Ef-e*V) and Ef as lower and upper limits in my Definite integral. where Ef is given 10 electron volts , e is the electronic charge and I want the ans in terms of V
How would i DO THAT????
PLEASE HELP
from scipy.integrate import quad
import sympy as sp
import math
e = 1.6 * (10 ** -19)
L = 10 ** -9
h = 6.626 * (10 ** -34)
h_cut = 1.05 * (10 ** -34)
m = 9.11 * (10 ** -31)
V0 = 4.0*e # in J
EF = 10.0*e # in J
E = sp.Symbol('E')
V = sp.Symbol('V')
def f(E):
j = (4 * E * (V0 - E)) / (4 * E * (V0 - E) + V0 ** 2 * ((2 * m * (V0 - E)) * ((L / h_cut) ** 2)))
return j
i, err = quad(f, EF - e * V, EF)
print('i= ', i)
I = (2 * e * i) / h
print(I)
```
THE error is as follows:
Traceback (most recent call last):
File "C:/Users/Subham/Desktop/Integration/integration.py", line 22, in <module>
i, err = quad(f, EF - e * V, EF)
File "C:\Users\Subham\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\integrate\quadpack.py", line 337, in quad
flip, a, b = b < a, min(a, b), max(a, b)
File "C:\Users\Subham\Desktop\Integration\venv\lib\site-packages\sympy\core\relational.py", line 304, in __nonzero__
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
Since you are using SymPy, you could do the integral there. It works best if you work with Rational numbers, however, so we do this
>>> gx = nsimplify(f(x), rational=True)
Then compute the integral
>>> i = integrate(gx, (x,EF-e*V,EF))
And display it at whatever precision you desire. Here is the result to 2 sigfigs on each number:
>>> nfloat(i, 2)
1.6e-19*V + 1.7e-17*log(1.9e-17 - 1.6e-19*V) + 6.5e-16
I have two arrays:
L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))
I am trying to get an array C such that:
C = B * A
C has dimension [L, M, N]
I tried answer posted at this link but it hasn't given me the desired output.
A for loop version of above code is:
L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))
z1 = []
for j in range(M):
a = np.squeeze(A[:, j, :])
z1.append(np.dot(B, a))
z2 = np.stack(z1)
I think you are looking for numpy.tensordot() where you can specify along which axes to sum:
np.tensordot(B,A,axes=(1,0))
I need to calculate Gamma cumulative distribution, and it seems this is fairly equivalent to calculating the incomplete beta function.
Excel does have an inculded calculator, but I found no trace of the used algorithm.
Do any of you know an accurate way to calculate this function?
I tried the following, translated into VB.NET from a website, but it gives stupid results:
Function IncompleteBetaFunc(x As Double, a As Double, b As Double) As Double
If x <= 0 Or x >= 1 Then Return 0
Dim bt As Double
bt = Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x))
If x < (a + 1.0) / (a + b + 2.0) Then
Return bt * betacf(a, b, x) / a
Else
Return 1.0 - bt * betacf(b, a, 1.0 - x) / b
End If
End Function
Function betacf(x As Double, a As Double, b As Double) As Double
Const MAXIT As Integer = 100
Const EPS As Double = 0.0000003
Const FPMIN As Double = 1.0E-30
Dim aa, c, d, del, h, qab, qam, qap As Double
Dim m, m2 As Integer
qab = a + b
qap = a + 1.0
qam = a - 1.0
c = 1.0
d = 1.0 - qab * x / qap
If (Math.Abs(d) < FPMIN) Then d = FPMIN
d = 1.0 / d
h = d
For m = 1 To MAXIT
m2 = 2 * m
aa = m * (b - m) * x / ((qam + m2) * (a + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
h *= d * c
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
del = d * c
h *= del
If (Math.Abs(del - 1.0) < EPS) Then Exit For
Next
Return h
End Function
Thanks!
Meta.Numerics includes well-tested and performant code for this any many other special functions. Its incomplete Beta function is documented here. The underlying code can be studied here. It also has a full-on Gamma distribution object, which will give moments, generate random variates, and do other distribution-related stuff in addition to computing the CDF. The package available via NuGet; just search for Meta.Numerics in the VS NuGet interface.
I created a program, using VBA, to calculate the European Call option price, as follows:
Private Sub CallPrice_Click()
Dim K As Single
Dim So As Single
Dim r As Single
Dim T As Single
Dim sigma As Single
Dim u As Single
Dim d As Single
Dim p As Single
Dim CP As Single
Dim M As Single
Dim S As Single
Dim CB As Double
Dim n As Integer
Dim i As Integer
K = Cells(2, 2)
So = Cells(3, 2)
r = Cells(4, 2)
T = Cells(5, 2)
sigma = Cells(6, 2)
n = Cells(7, 2)
u = Exp(sigma * Sqr(T / n))
d = 1 / u
p = (Exp(r * T / n) - d) / (u - d)
CP = 0
For i = 0 To n Step 1
M = WorksheetFunction.Max(So * (u ^ i) * d ^ (n - i) - K, 0)
CB = WorksheetFunction.Combin(n, i)
S = M * CB * (p ^ n) * (1 - p) ^ (n - i)
CP = CP + S
Next i
Cells(9, 2) = CP / (1 + r) ^ n
End Sub
Here is the layout of the spreadsheet:
When I ran the program, the error occurred.
Could someone here explain what is wrong in my program and how to fix it?
Is your equation for S correct? It seems like it should be:
S = M * CB * (p ^ i) ...
instead of
S = M * CB * (p ^ n) ...
If your equation is indeed wrong, then you can use BINOMDIST instead of COMBIN, because by definition:
Binom_Dist(i, n, p, False) = (p ^ i) * (1 - p) ^ (n - i) * Combin(n, i)
So your code would be:
S = M * WorksheetFunction.Binom_Dist(i, n, p, False)
instead of
CB = WorksheetFunction.Combin(n, i)
S = M * CB * (p ^ n) * (1 - p) ^ (n - i)
BINOMDIST is not as sensitive to large n, i.
You are getting an overflow error. If you check on a work sheet:
COMBIN(5000, 161) = 3.3E+307
COMBIN(5000, 162) = #NUM!
COMBIN(5000, 4838) = #NUM!
COMBIN(5000, 4839) = 3.3E+307
Remember that the number of combinations increases exponentially up until the halfway point in which it will start to go down at an inverse rate.
how to calculate (A*B*C)%10000007 where A,B,C can be maximum 10^18
Let I = 10000007, so
A = n1 * I + X1
B = n2 * I + X2
C = n3 * I + X3
A * B => (n1 * I + X1) (n2 * I + X2) => n1 * n2 * I^2 + n1 * X2 * I + n2 * X1 * I + X1 * X2
Only X1 * X2 can't div by I
Hence, A * B % I === X1 * X2 % I === (A % I) * (B % I) % I
Therefore (A * B * C) % I === [(A % I) * (B % I) % I] * (C % I) % I