How is it going guys ?
I've written a program that "draws" diamond in the command line (it's a part of my homework). For spaces inside the diamond I was given a formula "1 + 2(k-2) or 2k -3 , where k is line number", but I don't understand how this formula was created. Could anyone explain it ?
program diamond;
var
n, k, h, i: integer;
begin
repeat
write('Enter the diamond''s height (positive odd): ');
readln(h);
until (h > 0) and (h mod 2 = 1);
n := h div 2;
for k := 1 to n + 1 do
begin
for i := 1 to n + 1 - k do
write(' ');
write('*');
if k > 1 then
begin
for i := 1 to 2*k - 3 do
write(' ');
write('*')
end;
writeln
end;
for k := n downto 1 do
begin
for i := 1 to n + 1 - k do
write(' ');
write('*');
if k > 1 then
begin
for i := 1 to 2*k - 3 do
write(' ');
write('*')
end;
writeln
end
end.
I've already figured it out. It's a simple, but modified arithmetic progression An=A1-d(n-2). Usually we would use (n-1), but because we need to substract 2 stars from each line (starting from the second one, as this formula works for k>1), we use (n-2)
Related
I'M TRYIN TO CREATE A PROGRAM USING MAPLE FOR GAUSSING ELIMINATION BUT I KEEP GETTING THIS ERROR
Gauss := proc (n::posint, A::matrix, c::Vector)
local a, i, k, j, p;
with(MTM):
n := linalg[rowdim](A);
if det(A) = 0 then print('matrice*doit*etre*caree')
else if det(A) <> 0
then a := `<|>`(A, c);
for k to n-1 do
for i from k+1 to n do
if a[i, i] = 0 then swaprow(a, k, i)
else p = a[i, k]/a[k, k];
for j from k+1 to n+1 do a[i, j] = a[i, j]-p*a[k, j]
end do;
end if;
end do;
end do;
else print('rien')
end if; end if; end proc;
Error, (in Gauss) illegal use of a formal parameter
restart;
Gauss := proc(A::Matrix, c::Vector)
local a, i, k, j, m, n, p;
n := linalg[rowdim](A);
m := linalg[coldim](A);
if m <> n then
print("matrice doit etre caree");
else
a := `<|>`(A, c);
for k to n-1 do
for i from k+1 to n do
if a[i, i] = 0 then
a := linalg[swaprow](a, k, i);
else
p := a[i, k]/a[k, k];
for j from k to n+1 do
a[i, j] := a[i, j]-p*a[k, j];
end do;
end if;
end do;
end do;
end if;
return a;
end proc:
c := Vector([2, 3, 4]);
A := Matrix(3, 3, [4, 1, 2, 3, 6, 5, 2, 1, 9]);
Gauss(A, c);
LinearAlgebra:-LUDecomposition(<A|c>, output=U);
There were quite a few mistakes, so let's hope I get most of them.
I didn't bother doing 7. You should do it.
You cannot use with inside a procedure.
Your code uses commands from thelinalg
package, not the MTM package.
Ideally you'd use Matrix&Vector&LinearAlgebra
(instead of your mix of matrix&Vector&linalg(.
Your procedure has n as one of its
parameters, but inside it you also try to
assign a value to n, the argument for which
you passed in as the number 3. That's where
your error message is coming from. You can't
do that.
Several of you lines have just = instead of
:= for assignments. The = does nothing.
The test against det(A)=0 is wrong is wrong
in several ways. I'll just say that it doesn't
actually test whether the A is square.
Compare the row & column dimensions if you
want to test that A is square.
You should be using LinearAlgebra
equivalents instead of the linalg commands
commands swaprow, coldim.
You forgot to have your procedure actually
return the Matrix a.
When your code calls swaprow is was not
actually updating a. It was just throwing
way the result.
It's a sin to not indent your code. It will
lead you to overlook mistakes.
I have a set of experimental data (P), and I want to obtain plot "experimental vs predicted". In order to do so, I use another set of data which depend on P (Q), plot ScatterPlot, use appropriate fit, then obtain regression line, and use its coefficients in appropriate differential equation. Plot of P looks good, but I need to add there experimental data. For simplicity, I've used interval t=0..150.
How can I plot experimental data so that P(0) = Pvals[1], P(10)=Pvals[2], etc.? Besides, how can I distribute data (say, I have t=0..800 and want to plot Pvals so that P(0) = Pvals[1] and P(800) = Pvals[16])?
Pvals := [3.929, 5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.433, 38.558, 50.156, 62.948,
75.996, 91.972, 105.711, 122.775, 131.669]:
for i to 15 do Qval[i] := .1*(Pvals[i+1]/Pvals[i]-1); end do:
Qvals := [seq(Qval[i], i = 1 .. 15), 0.144513895e-1]:
with(Statistics);
ScatterPlot(Pvals, Qvals, fit = [a*v^2+b*v+c, v], thickness = 3,
legend = [points = "Point data", fit = typeset("fit to a", 2^nd, "degree polynomial")]);
with(CurveFitting);
LeastSquares(Pvals, Qvals, v, curve = a*v^2+b*v+c);
de := diff(P(t), t) = (0.370152282598477e-1-0.272504103112702e-3*P(t))*P(t);
sol := dsolve({de, P(0) = 3.929}, P(t));
P := plot(rhs(sol), t = 0 .. 160);
I'm not sure that I entirely follow your methodology. But is this something like what you are trying to accomplish?
restart;
with(Statistics):
Pvals := [3.929, 5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.433,
38.558, 50.156, 62.948, 75.996, 91.972, 105.711, 122.775, 131.669]:
for i to 15 do Qval[i] := .1*(Pvals[i+1]/Pvals[i]-1); end do:
Qvals := [seq(Qval[i], i = 1 .. 15), 0.144513895e-1]:
form := a*v^2+b*v+c:
CF := CurveFitting:-LeastSquares(Pvals, Qvals, v, curve = form);
CF := 0.0370152282598477 - 0.000272504103112702 v
-7 2
+ 5.60958249026713 10 v
Now I use CF in the DE (since I don't understand why you dropped the v^2 term),
#de := diff(P(t), t) = (0.370152282598477e-1-0.272504103112702e-3*P(t))*P(t);
de := diff(P(t), t) = eval(CF, v=P(t))*P(t);
d /
de := --- P(t) = \0.0370152282598477 - 0.000272504103112702 P(t)
dt
-7 2\
+ 5.60958249026713 10 P(t) / P(t)
I'll use the numeric option of the dsolve command, and obtain a procedure that computes P(t) for numeric t values.
sol := dsolve({de, P(0) = 3.929}, P(t), numeric, output=listprocedure ):
Pfunc := eval(P(t), sol);
Pfunc := proc(t) ... end;
Pfunc(0.0), Pvals[1];
3.92900000000000, 3.929
Now some rescaling (which, again, is my guess as to your goal),
endpt := fsolve(Pfunc(t)-Pvals[16]);
endpt := 135.2246055
Pfunc(endpt), Pvals[16];
131.669000003321, 131.669
plot(Pfunc(t), t=0 .. endpt, size=[500,200]);
a,b,N := 0.0, 800.0, nops(Pvals);
a, b, N := 0., 800.0, 16
Pfuncscaled := proc(t)
if not t::numeric then
return 'procname'(args);
end if;
Pfunc(t*endpt/b);
end proc:
Pfuncscaled(0), Pvals[1];
3.92900000000000, 3.929
Pfuncscaled(800), Pvals[N];
131.669000003321, 131.669
PLscaled := plot( Pfuncscaled(t), t=a .. b,
color=red, size=[500,200] );
Now to display the Pdata against 0 .. 800 as well,
V := Vector(N, (i)->a+(i-1)*(b-a)/(N-1)):
V[1], V[-1];
0., 800.0000000
Pdatascaled := plot( < V | Vector(Pvals) >,
color=blue, size=[500,200],
style=pointline, symbol=solidcircle );
And, displaying the rescaled data together with the rescaled procedure from dsolve,
plots:-display( PLscaled, Pdatascaled, size=[500,500] );
Please help me to convert this PL/SQL into PostgreSQL. Thank you very much.
Prime Numbers
CREATE TABLE n (n NUMBER);<br/>
CREATE OR REPLACE PROCEDURE prime_number (n NUMBER)<br/>
IS <br/>
prime_count NUMBER := 0;<br/>
y VARCHAR2 (1) := 'N';<br/>
BEGIN<br/>
IF n >= 1
THEN
prime_count := 1;
INSERT INTO n
VALUES (2);
END IF;
IF n >= 2
THEN
prime_count := 2;
INSERT INTO n
VALUES (2);
END IF;
IF n >= 3
THEN
FOR i IN 4 .. n * n * n
LOOP
y := 'N';
FOR j IN 2 .. CEIL (SQRT (i))
LOOP
IF (MOD (i, j) = 0)
THEN
y := 'Y';
EXIT;
END IF;
END LOOP;
IF (y = 'N')
THEN
INSERT INTO n
VALUES (i);
COMMIT;
prime_count := prime_count + 1;
EXIT WHEN prime_count = n;
END IF;
END LOOP;
END IF;<br/>
END;
BEGIN<br/>
prime_number (1000000);<br/>
END;
I'm implementing RSA encryption and need to generate random 1024-bit primes.
I can't use INTEGER signals because of the limit, and so I'm using STD_LOGIC_VECTOR and convert it to UNSIGNED when I need to perform arithmetic operations.
I began by using UNIFORM to generate 32 random 32-bit numbers which I then copy over into a 1024-bit STD_LOGIC_VECTOR.
I then set the most significant and least significant bits to '1' to ensure it is 1024-bits and odd.
I then check for primality using an implementation of the Miller Rabin algorithm, which is where my problem lies.
This is where I generate the random primes:
function GEN_1024_PRIME return STD_LOGIC_VECTOR is
VARIABLE s1, s2 : POSITIVE;
VARIABLE random : REAL;
VARIABLE small_random : STD_LOGIC_VECTOR (31 downto 0);
VARIABLE large_random : STD_LOGIC_VECTOR (1023 downto 0);
VARIABLE prime : STD_LOGIC := '0';
begin
while prime /= '1' loop
for I in 0 to 31 loop
UNIFORM(s1, s2, random);
small_random := STD_LOGIC_VECTOR(to_unsigned(INTEGER(TRUNC(random * REAL(2147483647))), 32));
large_random (I*32 + 31 downto I*32) := small_random;
end loop;
large_random(0) := '1';
large_random(1023) := '1';
prime := MILLER_RABIN (large_random);
end loop;
return large_random;
end function;
And my implementation of Miller Rabin:
function MILLER_RABIN (prime : STD_LOGIC_VECTOR (1023 downto 0)) return STD_LOGIC is
VARIABLE t : INTEGER := 4;
VARIABLE temp, r, a, x, j, n: UNSIGNED (1023 downto 0);
VARIABLE small_random : UNSIGNED (31 downto 0);
VARIABLE large_random : UNSIGNED (1023 downto 0);
VARIABLE s1, s2 : POSITIVE;
VARIABLE random : REAL;
begin
n := UNSIGNED(prime);
if n MOD 2 = 0 OR n MOD 3 = 0 then
return '0';
else
-- calculate n - 1 = 2^s * r such that r is odd
r := n - 1;
while r MOD 2 = 0 loop
r := r / 2;
end loop;
for I in 1 to t loop
-- choose random a, 2 <= a <= n-2
for I in 0 to 31 loop
UNIFORM(s1, s2, random);
small_random := to_unsigned(INTEGER(TRUNC(random * REAL(2147483647))), 32);
large_random (I*32 + 31 downto I*32) := small_random;
end loop;
a := large_random;
temp := r;
x := MOD_3(a, temp, n);
while (temp /= (n - 1) AND x /= 1 AND x /= (n - 1)) loop
x := (x * x) MOD n;
temp := temp * 2;
end loop;
if x /= (n - 1) AND temp MOD 2 = 0 then
return '0';
end if;
end loop;
return '1';
end if;
end function;
function MOD_3 (a, b, c : UNSIGNED (1023 downto 0)) return UNSIGNED is
VARIABLE x : UNSIGNED (1023 downto 0) := TO_UNSIGNED(1, 1024);
VARIABLE y : UNSIGNED (1023 downto 0) := a;
VARIABLE b_temp : UNSIGNED (1023 downto 0) := b;
begin
while b_temp > 0 loop
if b_temp MOD 2 = 1 then
x := (x * y) MOD c;
end if;
y := (y * y) MOD c;
b_temp := b_temp / 2;
end loop;
return x MOD c;
end function;
I convert the input to UNSIGNED in order to perform arithmetic operations, which seemed like it would work until I realized there will be instances in which the product of 2 values will be larger than 1024 bits. For example, in this while loop:
while (temp /= (n - 1) AND x /= 1 AND x /= (n - 1)) loop
x := (x * x) MOD n;
temp := temp * 2;
end loop;
The resultant of temp := temp * 2; is 2048 bits.
I feel like I could get this to work by messing around with the sizes of my UNSIGNED variables, but I think it is getting messier than it needs to be, so I'm wondering if I am approaching this the wrong way? Is there a simpler way to generate 1024-bit primes? Is there another primality test that would be better suited for my problem?
Visit http://www.mapleprimes.com/questions/35644-Multiply-2-Matrices-In-Maple-Using-A-Procedure
restart:
mmm:=proc(a::Matrix,b::Matrix)
local c, i, j, k, m, n, p;
(m,n,p):=op([1,1],a), op([1,2],b), op([1,2],a);
if op([1,1],b) <> p then error "incompatible dimensions"; end if;
c:=Matrix(m,n);
for i from 1 to m do
for j from 1 to n do
c[i,j] := add(a[i,k]*b[k,j],k=1..p);
end do;
end do:
c;
end proc:
a:=LinearAlgebra:-RandomMatrix(2,3):
b:=LinearAlgebra:-RandomMatrix(3,5):
mmm(a,b);
a.b; # check
mmm(a,a^%T);
a.a^%T; # check
mmm(b,a); # test for dimension mismatch
b.a; # test for dimension mismatch
a:=LinearAlgebra:-RandomMatrix(33,33):
b:=LinearAlgebra:-RandomMatrix(33,33):
LinearAlgebra:-Norm(a.b - mmm(a,b));