Design a recursive algorithm f(n1, n2, delta) that multiplies specific natural numbers between n1
and n2 (n1 > n2) as follows.
Starting with n1 multiply all natural numbers with a distance of delta from the previous natural
number.
Example: n1 = 35, n2 = 15, delta = 5
f(35, 15, 5) = 35 * 30 * 25 * 20 * 15
Solution:
Input: n1, n2 ∈ ℕ, n1 > n2, ∃ x ∈ ℕ: n2 + (x * delta) = n2
Output: n1 * (n1 - delta ) * (n1 – 2 * delta) * … * n2
f(n2, n2, delta) = n2
f(n1, n2, delta) = n1 * f((n1-delta), n2, delta)
Can someone explain this solution?
This solution implies a recursive algorithm (pseudo-code below). The idea in the solution is that you can "diminish" the problem by decreasing n1 by delta, and the stopping condition is that n1 is smaller than n2.
Note that the explanation you mentioned is a bit inaccurate as it does assumes that after some steps n2 is gotten, while you may reach some value < n2 (for example, if you change the 15 in the call f(35,15,5) to 14).
The pseudo-code:
f(n1, n2, delta):
if n1 < n2:
return 1
return n1 * f(n1 - delta, n2, delta)
Running example on your case (f(n1) instead of f(n1,n2,delta) is used for brevity as only n1 is changes):
f(35) = 35 * f(30) =
35 * 30 * f(25) =
35 * 30 * 25 * f(20) =
35 * 30 * 25 * 20 * f(15) =
35 * 30 * 25 * 20 * 15 * f(10) =
35 * 30 * 25 * 20 * 15 * 1
Related
I have a sigma example:
And I don't have any idea how to solve it. Can you help me with the code, please?
(Code pascal, java or c++)
Expanding the inner term, you get m^3 - 3m^2n + 3mn^2 - n^3, which yields a double summation of m^5, -3m^4n, 3m^3n^2 and -m^2n^3. These summations are separable, meaning that they are the product of a sum on m of a power of m and a sum on n of a power of n.
You can evaluate these sums by means of the Faulhaber formulas up to degree five, which are polynomial expressions. Evaluate them by Horner's method.
int F1(int n) { return (n + 1) * n / 2; }
int F2(int n) { return ((2 * n + 3) * n + 1) * n / 6; }
int F3(int n) { return ((n + 2) * n + 1) * n * n / 4; }
...
int S= F5(20) * 30 - 3 * F4(20) * F1(30) + 3 * F3(20) * F2(30) - F2(20) * F3(30);
Using the obvious method of summation, the inner loop will evaluate 30 cubes of a difference, for a total of 60 additions and 60 multiplications, and the outer loop will repeat this 20 times, with extra multiplications and additions, for a total of 1220 + and 1240 *.
Compare to the above method, performing 18 +, 30 * and 7 divisions in total (independently of the values of m and n).
I am wondering if there is any package which allows us to use the Lanczos filter. I found other filters such as butterworth but I am looking for Lanczos low pass filter.
How different is Lanczos filter from butterworth filter ? Any suggestions or hints is appreciated.
Thanks.
Using the web I find this MATLAB implementation.
If you skipped the first part(arguments check), it looks simple to write its R equivalent.
# Cf - Cut-off frequency (default: half Nyquist)
# M - Number of coefficients (default: 100)
lanczos_filter_coef <- function(Cf,M=100){
lowpass_cosine_filter_coef <- function(Cf,M)
coef <- Cf*c(1,sin(pi*seq(M)*Cf)/(pi*seq(M)*Cf))
hkcs <- lowpass_cosine_filter_coef(Cf,M)
sigma <- c(1,sin(pi*seq(M)/M)/(pi*seq(M)/M))
hkB <- hkcs*sigma
hkA <- -hkB
hkA[1] <- hkA[1]+1
coef <- cbind(hkB, hkA)
coef
}
To test it for example:
dT <- 1
Nf <- 1/(2*dT)
Cf <- Nf/2
Cf <- Cf/Nf
lanczos_filter_coef(Cf,5)
hkB hkA
[1,] 5.000000e-01 5.000000e-01
[2,] 2.977755e-01 -2.977755e-01
[3,] 1.475072e-17 -1.475072e-17
[4,] -5.353454e-02 5.353454e-02
[5,] -4.558222e-18 4.558222e-18
[6,] 2.481571e-18 -2.481571e-18
PS I don't know very well MATLAB(used it many years ago), so I I used this link For the R/MATLAB analogy. I hope that someone with more R/MATLAB/Scilab knowledge can test my code.
I used the method provided in this link https://www.atmos.umd.edu/~ekalnay/syllabi/AOSC630/METO630ClassNotes13.pdf and wrote this function:
`
lanczos_weights<-function(window=101,sampl_rate=1,type="lowpass",low_freq=1/100,high_freq=1/10){
low_freq=sampl_rate*low_freq
high_freq=sampl_rate*high_freq
if (type=="lowpass"){
order = ((window - 1) %/% 2 ) + 1
nwts = 2 * order + 1
fc=low_freq
w = seq(0,0,length=nwts)
n = nwts %/% 2
w[n+1] = 2 * fc
k = seq(1, n-1)
sigma = sin(pi * k / n) * n / (pi * k)
firstfactor = sin(2 *pi * fc * k) / (pi * k)
w[n:2] = firstfactor * sigma
w[(n+2):(length(w)-1)] = firstfactor * sigma
w=w[-c(1,length(w))]}
else if (type=="highpass"){
order = ((window - 1) %/% 2 ) + 1
nwts = 2 * order + 1
fc=high_freq
w = seq(0,0,length=nwts)
n = nwts %/% 2
w[n+1] = 2 * fc
k = seq(1, n-1)
sigma = sin(pi * k / n) * n / (pi * k)
firstfactor = sin(2 *pi * fc * k) / (pi * k)
w[n:2] = firstfactor * sigma
w[(n+2):(length(w)-1)] = firstfactor * sigma
w=w[-c(1,length(w))]
w=-w
w[order]=1-2*fc }
else if (type=="bandpass"){
order = ((window - 1) %/% 2 ) + 1
nwts = 2 * order + 1
fc=low_freq
w = seq(0,0,length=nwts)
n = nwts %/% 2
w[n+1] = 2 * fc
k = seq(1, n-1)
sigma = sin(pi * k / n) * n / (pi * k)
firstfactor = sin(2 *pi * fc * k) / (pi * k)
w[n:2] = firstfactor * sigma
w[(n+2):(length(w)-1)] = firstfactor * sigma
w1=w[-c(1,length(w))]
order = ((window - 1) %/% 2 ) + 1
nwts = 2 * order + 1
fc=high_freq
w = seq(0,0,length=nwts)
n = nwts %/% 2
w[n+1] = 2 * fc
k = seq(1, n-1)
sigma = sin(pi * k / n) * n / (pi * k)
firstfactor = sin(2 *pi * fc * k) / (pi * k)
w[n:2] = firstfactor * sigma
w[(n+2):(length(w)-1)] = firstfactor * sigma
w2=w[-c(1,length(w))]
w=w2-w1}
else {print("Please specify a valid filter type: either 'lowpass', 'highpass' or 'bandpass'")}
return(w)}
`
#### the inputs are:
#### window: Filter length=number of weights. Corresponds to the total number of points to be lost. Should be odd: window=2N-1. The formula for N is taken from Poan et al. (2013)
#### sampl_rate: sampling rate=number of observation per time unit. ( eg: if time unit is one day, hourly data have sampl_rate=1/24)
#### type= one of "lowpass", "highpass" and "bandpass"
#### low_freq: the lowest frequency
#### high_freq: the highest frequency
I have compared my weights to those obtained using NCL filwgts_lanczos and they are exactly the same.
Question is about the modulo operator on very large numbers.
For example consider a question where the total number of permutations are to be calculated.
Consider a number of 90 digits with each of the 9 numbers (1 to 9) repeating 10 times
so 90!/(10!)^9) is to be calculated
After reading many answers on StackOverflow I used logarithms to do it.
Now consider the log value to be 1923.32877864.
Now my question is how can I display the answer (i.e. 10 ^ log10(value) ) modulo of "m"?
And is this the best method for calculating the possible number of permutations?
Edit
Got the solution :)
Thanks to duedl0r.
Did it the way you specified using Modular Multiplicative Inverse.Thanks :)
I'm not sure whether this is actually possible and correct, but let me summarize my comments and extend the answer from Miky Dinescu.
As Miky already wrote:
a × b ≣m am × bm
You can use this in your equality:
90! / 10!^9 ≣m x
Calculate each term:
90!m / 10!^9m ≣m x
Then find out your multiplicative inverse from 10!^9m. Then multiplicate the inverse with 90!m.
update
This seems to be correct (at least for this case :)). I checked with wolfram:
(90!/10!^9) mod (10^9+7) = 998551163
This leads to the same result:
90! mod (10^9+7) = 749079870
10!^9 mod (10^9+7) = 220052161
do the inverse:
(220052161 * x) mod(10^9+7) = 1 = 23963055
then:
(749079870*23963055) mod (10^9+7) = 998551163
No proof, but some evidence that it might work :)
I would argue that the way to compute the total number of permutations modulo m, where m is an arbitrary integer (usually chosen to be a large prime number) is to use the following property:
(a * b) % m = ((a % m) * (b % m)) % m
Considering that the total number of permutations of N is N! = 1 * 2 * 3 * .. * N, if you need to compute N! % m, you can essentially apply the property above for multiplication modulo m, and you have:
((((1 * (2 % m)) % m) * (3 % m)) % m) * ..
EDIT
In order to compute the 90! / (10! ^ 9) value you could simplify the factors and then use multiplication modulo m to compute the final result modulo m.
Here's what I'm thinking:
90! = 10! * (11 * 12 * .. * 20) * (21 * 22 * .. * 30) * .. * (81 * 82 * .. * 90)
You can then rewrite the original expression as:
(10! * (11 * 12 * .. * 20) * (21 * 22 * .. * 30) * .. * (81 * 82 * .. * 90)) / (10! * 10! * ... * 10!)
At the numerator, you have a product of 9 factors - considering each expression in parenthesis a factor. The same is true for the denominator (you have 9 factors, each equal to 10!).
The first factor at the denominator is trivial to simplify. After that you still have 8 pairs that need simplification.
So, you can factor each term of the products and simplify the denominator away. For example:
11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 <=> 11 * 2 * 2 * 3 * 13 * 2 * 7 * 3 * 5 * 2 * 2 * 2 * 2 * 17 * 2 * 9 * 2 * 2 * 5
The denominator will always be: 2 * 3 * 2 * 2 * 5 * 2 * 3 * 7 * 2 * 2 * 2 * 2 * 3 * 3 * 2 * 5
After the simplification the second pair reduces to : 2 * 2 * 11 * 13 * 17 * 19
The same can be applied to each subsequent pair and you will end up with a simple product that can be computed modulo m using the formula above.
Of course, efficiently implementing the algorithm to perform the simplification will be tricky so ultimately there has to be a better way that eludes me now.
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 11 years ago.
Improve this question
Evaluate
(z x^-1 y)^5 y^5
~~~~~~~~~~~~~~~~~~~~~~~ OVER
x^-4 z^-4
How would I evaluate this if X = 10, y = -3 and z = 3? I would like a step-by-step solution to help me fully understand it.
Numerator evaluates as (z*y*x^-1)^5 * y^5
further rewriting ((z^5*y^5)*y^5)/x^5
Denominator ((1/x^4)*(1/z^4))
Final Answer would be ((y^10)*(z^9))/x
as per your values it (3^19)/10
Exponentials have higher priority in most computer languages, so adding parentheses like this should make it clearer. I'm assuming that you're dividing the first polynomial by the second. It's simple algebra.
(z x^-1 y)^5 y^5
---------------- =
x^-4 z^-4
(y^10)(z^9)/x
You substitute the numbers.
Start with:
((z * x^-1 * y)^5 * y^5)/(x^-4 * z^-4)
Commute the exponent to the z factor: (A * B)^N => A^N * B^N
(z^5 * (x^-1 * y)^5 * y^5)/(x^-4 * z^-4)
Commute the exponent to the x and y factors: (A * B)^N => A^N * B^N
(z^5 * (x^-1)^5 * y^5 * y^5)/(x^-4 * z^-4)
Simplify the exponenet on the x factor: (A^N)^M => A^(N*M)
(z^5 * x^-5 * y^5 * y^5)/(x^-4 * z^-4)
Combine the y factors: A^N * A^M => A^(N+M)
(z^5 * x^-5 * y^10)/(x^-4 * z^-4)
Remove the negative exponent on x: 1/A^-N => A^N
(z^5 * x^-5 * y^10 * x^4) / (z^-4)
Remove the negative exponenet on z: 1/A^-N => A^N
z^5 * x^-5 * y^10 * x^4 * z^4
Combine the z factors: A^N * A^M => A^(N+M)
z^9 * x^-5 * y^10 * x^4
Combine the x factors: A^N * A^M => A^(N+M)
z^9 * x^-1 * y^10
Remove the negative exponent on x: A^(-N) => 1/A^N
(z^9 * y^10)/(x^1)
Simplify the x factor: A^1 => A
(z^9 * y^10)/(x)
And that's the algebraic form of your answer.
Next, subsitute the values:
3^9 * (-3)^10 / 10
Factor the exponents:
(3^3)^3 * (-3)^10 / 10
(3^3)^3 * ((-3)^2)^5 / 10
Evalutate the innermost exponents:
(3 * 3 * 3)^3 * ((-3)^2)^5 / 10
(9 * 3)^3 * ((-3)^2)^5 / 10
27^3 * ((-3)^2)^5 / 10
27^3 * 9^5 / 10
Continue evaluation exponents, breaking them down for simplicity:
27 * 27 * 27 * 9^5 / 10
27 * 27 * 27 * 9^5 / 10
729 * 27 * 9^5 / 10
19683 * 9^5 / 10
19683 * 9^2 * 9^2 * 9 / 10
19683 * 81 * 81 * 9 / 10
Then multiply the factors:
19683 * 81 * 729 / 10
19683 * 59049 / 10
1162261467 / 10
116226146.7
And there's your final answer.
You could also take advantage of the fact that X^N = (-X)^N if N is even by replacing -3 with 3 since 10 is even.
3^9 * (-3)^10 / 10
3^9 * 3^10 / 10
3^19 / 10
3 * 3^18 / 10
3 * (3^9)^2 / 10
3 * (3 * 3^8)^2 / 10
3 * (3 * (3^2)^4)^2 / 10
3 * (3 * ((3^2)^2)^2)^2 / 10
3 * (3 * (9^2)^2)^2 / 10
3 * (3 * 81^2)^2 / 10
3 * (3 * 6561)^2 / 10
3 * (19683)^2 / 10
3 * 387420489 / 10
1162261467 / 10
116226146.7
Its available for me only log(base "e"), sin, tan and sqrt (only square root) functions and the basic arithmetical operators (+ - * / mod). I have also the "e" constant.
I'm experimenting several problems with Deluge (zoho.com) for these restrictions. I must implement exponentiation of rational (fraction) bases and exponents.
Say you want to calculate pow(A, B)
Consider the representation of B in base 2:
B = b[n] * pow(2, n ) +
b[n-1] * pow(2, n - 1) +
...
b[2] * pow(2, 2 ) +
b[1] * pow(2, 1 ) +
b[0] * pow(2, 0 ) +
b[-1] * pow(2, -1 ) +
b[-2] * pow(2, -2 ) +
...
= sum(b[i] * pow(2, i))
where b[x] can be 0 or 1 and pow(2, y) is an integer power of two (i.e., 1, 2, 4, 1/2, 1/4, 1/8).
Then,
pow(A, B) = pow(A, sum(b[i] * pow(2, i)) = mul(pow(A, b[i] * pow(2, i)))
And so pow(A, B) can be calculated using only multiplications and square root operations
If you have a function F() that does e^x, where e is the constant, and x is any number, then you can do this: (a is base, b is exponent, ln is log-e)
a^b = F(b * ln(a))
If you don't have F() that does e^x, then it gets trickier. If your exponent (b) is rational, then you should be able to find integers m and n so that b = m/n, using a loop of some sort. Once you have m and n, you make another loop which multiples a by itself m times to get a^m, then multiples a by itself n times to get a^n, then divide a^m/a^n to get a^(m/n), which is a^b.