How to write pseudocode to compute n!!(double factorial) recursively? - discrete-mathematics

n!!= 1*3*...*n, if n is odd.
n!!= 2*4*...*n, if n is even.
I've been trying, but I'm having a tough time starting this problem.

if n = 1, n!! = 1
if n = 2, n!! = 2
else n!! = n * (n-2)!!

Related

How do I increase stepsize in MATLAB?

For my computing course, I am asked to solve an ODE, using Euler's method.
My code runs, however, I am now asked the following:
"Increase N a number of times, according to N=100,200,400,800.... so you can obtain the answers y100,y200,y400,y800...."
Here's my code:
function [xar,yar] = eulsol(a,b,ybouco,N)
h=(b-a)/N;
T=a:h:b;
y(1)=ybouco;
for i = 100:N
f(i) = -8*y(i) + 0.5*T(i) + (1/16);
y(i+1) = y(i)+h*f(i);
end
xar=T;
yar=y;
end
Can someone help me with obtaining a nice table in MATLAB, which shows me the arrays x and y, according to an increasing N (100,200,400,800....)?
Let's define K as the number of steps. In your example, K=4 (N=100,200,400,800). If N=100,200,400,800,1600,3200 then K = 6
Note that the ith element of N correspond to 100*2^(i-1):
i = 1 => N = 100 * 2^(1-1) = 100
i = 2 => N = 100 * 2^(2-1) = 200
i = 3 => N = 100 * 2^(3-1) = 400
and so on...
So if you want to calculate for N=100,200,400,800, your code should be:
function [xar,yar] = eulsol(a,b,ybouco,K)
N_max = 100 * 2^(K-1)
h=(b-a)/N_max;
T=a:h:b;
y(1)=ybouco;
for i = 1:K
N = 100 * 2^(i-1)
f(N) = -8*y(N) + 0.5*T(N) + (1/16);
y(N+1) = y(N)+h*f(N);
end
xar=T;
yar=y;
end
This answer if for creating the correct N inside the for loop, but you should review your code! As you can see: for i = 1, you have N = 100 and to calculate F(100) you need y(100), but you don't have y(100), just y(1).
Maybe the correct answer is F(i) = -8*y(i) + 0.5*T(N) + (1/16);
But again, want is T(N)?
Please, as noted by #Argyll , explain what you want, you shouldn't expect people to understand your question from a wrong code.

Matlab: All combinations of binary matrix

I am looking for a simple way to get all combinations of an binary matrix. I tried already the function perms() but did not get a proper result.
I have for example an matrix N x N filled up with 1 and -1. With N=2 there would be 2^4 possible combinations of 1 and -1 like
(1 1) (1 1) (-1 -1)
M(1) = (1 1) , M(2) = (1 -1) , M(3) = ( 1 1) and so on...
When I use perms() I don't get for example the first matrix.
How can I fix that?
You can represent all numbers between 0 and 2^(N^2)-1 as binary numbers, and then reshape:
N = 2;
v = (1:2^(N^2))-1;
A = dec2bin(v)' - '0'; %'// Or use: decimalToBinaryVector(v)';
A(A==0) = -1;
A = reshape(A,N,N,2^(N^2));
A simple hack is as follows:
v = [1 -1 1 -1];
P = perms(v);
for ii = 1:size(P,1)
A = reshape(P(ii,:),2,2)
end
which results in:
A =
-1 -1
1 1
...
Still there are some identical matrices in the result which should be removed.
I think that I found an solution to my problem
L = 2;
N = L^2;
v = cell(N,1);
for k = 1:N
v{k} = linspace(-1,1,2);
end
ne=numel(v);
x=cell(ne,1);
[x{1:ne,1}]=ndgrid(v{end:-1:1});
p=reshape(cat(ne+1,x{:}),[],ne);
F = cell(length(p),1);
for k=1:length(p)
F{k} = reshape(p(k,:),L,L);
end

Counting Number of Specific Outputs of a Function

If I have a matrix and I want to apply a function to each row of the matrix. This function has three possible outputs, either x = 0, x = 1, or x > 0. There's a couple things I'm running into trouble with...
1) The cases that output x = 1 or x > 0 are different and I'm not sure how to differentiate between the two when writing my script.
2) My function isn't counting correctly? I think this might be a problem with how I have my loop set up?
This is what I've come up with. Logically, I feel like this should work (except for the hiccup w/ the first problem I've stated)
[m n] = size(matrix);
a = 0; b = 0; c = 0;
for i = 1 : m
x(i) = function(matrix(m,:));
if x > 0
a = a + 1;
end
if x == 0
b = b + 1;
end
if x == 1
c = c + 1;
end
end
First you probably have an error in line 4. It probably should be i instead of m.
x(i) = function(matrix(i,:));
You can calculate a, b and c out of the loop:
a = sum(x>0);
b = sum(x==0);
c = sum(x==1);
If you want to distinguish x==1 and x>0 then may be with sum(xor(x==1,x>0)).
Also you may have problem with precision error when comparing double values with 0 and 1.

matlab - find index for when values exceeds a threshold n number of times

I would like to find the index for when an array exceeds a certain value, and this value is value is exceeded for a duration, n. For examples:
n = 5;
dat = [1,2,2,1.5,2,4,2,1,1,3,4,6,8,4,9];
Here, I would like to find when 'dat' exceeds 2 for a duration greater than n for the first time. So, the solution here should lead to an answer:
ans = 10
Another example:
n = 7;
dat = [1,1,2,3,4,5,6,7,8,9,9,6,4,3,2,4,6,7,7,5];
find the first time that 'dat' exceeds or equals 5 for more than or equal to n times.
ans = 6
n = 5;
m = 2;
dat = [1,2,2,1.5,2,4,2,1,1,3,4,6,8,4,9];
c = conv(double(dat >= m), ones(1, n))
%I think you can also do
% c = conv((dat >= m)*1, ones(1, n))
min(find(c == n)) - n + 1
n=5
x=2;
dat = [1,2,2,1.5,2,4,2,1,1,3,4,6,8,4,9];
vec= cumsum(dat>=x);
ind=find(vec>=n);
ind=dat(ind(1));
ind will contain the answer 10

sum the first n prime reciprocals such that the sum exceeds k (Matlab)

I am trying to write a program in matlab, such that the sum of the reciprocals of the n first prime numbers exceeds a given value k. To clearify, I am trying to make a function
SumPrime(k)
And it is supposed to return an integer n such that
\sum_{i=1}^{n} 1/p_i > k
sum of primes and reciprocals of them and plot in matlab?
I tried looking here, but this does not quite answer my question. Neither did the command
sumInversePrimes = sum(1./primes(n));
Here is my attempt. First i define a function for finding the n`th prime number.
function Y = NthPrime(n)
if n==1
Y = 2;
return
end
if n < 1 || round(n)~=n
return
end
j = 2;
u = 0;
while u < n
T = primes(j);
u = numel(T);
j = 1 + j;
end
Y = T(numel(T));
After doing this (lengthy?) code for finding the n`th prime number, the rest is a cakewalk.
function Y = E(u)
sum = 0
n = 0
while sum < u
n = n + 1
sum = sum + 1/( NthPrime(n) )
end
Y = n;
Return the proper values. This somewhat works. Alas it is very slow, and I guess this is very bad code. I have merely started learning coding in matlab, Could someone please help me either write a better code or optimize mine ?
XOXOXOX
Nebby
Here's how to precompute the sums then find the first that exceeds a threshold:
>> p = primes(1000);
>> cs = cumsum(1./p);
>> find(cs > 1.8, 1)
ans = 25