Kitonum

21530 Reputation

26 Badges

17 years, 86 days

MaplePrimes Activity


These are answers submitted by Kitonum

Suppose you are looking for the lowest four-digit prime number whose sum of digits is divisible by 17. Write the program as a procedure without parameters:

restart;

Check:=proc()

local i, j, k, l, b;

for i from 1 to 9 do

for j from 0 to 9 do

for k from 0 to 9 do

for l from 0 to 9 do

b:=1000*i+100*j+10*k+l;

if isprime(b) and irem(i+j+k+l, 17)=0 then return i, j, k, l  fi;

od: od: od: od:

end proc:

Check();

                                       1, 0, 9, 7

 

Addition.  Using a procedure also has the advantage that all of the variables, to which you give any values in the procedure's body by default  will be local, and outside the body of the procedure they are ordinary symbols:

P:=proc()

local i, j;

i:=1; j:=2;

end proc:

 

i;  j;

                                 i

                                 j

See help on  Student[LinearAlgebra][GaussianEliminationTutor]  command.

If I understand you're looking for minimax optimization. DirectSeach  package provides a slightly better solution:

DirectSearch[GlobalOptima](max(abs(pa^2 + 2*pa*po - 0.44), abs(pb^2 + 2*pb*po - 0.10), abs(po^2 - 0.42), abs(2*pa*pb - 0.04)), [pa = 0.279246445735439, pb = 0.0728480349005200, po = 0.648114970425358]);

0.000685054125967331, [pa =0 .27924642145279, pb = 0. 07284794181838, po =0 .64811497042536], 925]

 

because  0.000685054125967331<0.000685109649562879

Sys := {diff(x(t),t) = y(t)-2*(x(t))^2, diff(y(t),t) = -8*(x(t))^3+4*x(t)*y(t)-5*(x(t))^4-(x(t))^5};

DETools[phaseportrait](Sys, [x(t), y(t)], t=-1..1, [[x(0)=1, y(0)=0]]);

                        

 

 

 

R:=solve(f, x, explicit):

`*`(seq(x-R[i], i=1..4));

 

 

 

I pretty often use it. The operator  ~  seems to appear beginning with Maple 15 or Maple 16. It replaces not only  map  operator, but also  zip  operator. Here is an example in which you want to equate each element of the first list to the square of the corresponding element of second list:

X:=[x,y,z]:  A:=[a,b,c]:

zip(`=`, X, map(t->t^2,A))  # First way

X=~A^~2;  # Second way

                                 [x = a^2, y = b^2, z = c^2]

                                 [x = a^2, y = b^2, z = c^2]

 

This operator provides a more compact (short) syntax. I do not know his other advantages.

 

This works in one session until  restart  command is not used.

local I;

I:=2;

I+1;

                                      I := 2

                                         3

1) See  LinearAlgebra[HilbertMatrix]  command.

2) Your matrix  (2*2 matrix which its transpose is egual to its inverse)  is a rotation matrix or composition of a rotation and a reflection:  

Matrix([[cos(alpha), -sin(alpha)], [sin(alpha), cos(alpha)]]);  # A rotation matrix

Matrix([[1,0], [0,-1]]) . Matrix([[cos(alpha), -sin(alpha)], [sin(alpha), cos(alpha)]]);  # Composition of a rotation and a reflection

 

We have a system with 27 unknowns and 54 variables, that is 27 variables are parameters. If you give some values to these parameters, fsolve  easy solves the system. Replace the last line of your code by

ABC := fsolve(eval(map(t->Equate(op(t))[], [eq2 = AA1, eq3 = AA2, eq4 = B2]), {aaa1 = 1, aaa2 = 2, aaa3 = 3, aaa4 = 4, aaa5 = 5, aaa6 = 6, aaa7 = 7, aaa8 = 8, aaa9 = 9, aab1 = 10, aab2 = 11, aab3 = 12, aab4 = 13, aab5 = 14, aab6 = 15, aab7 = 16, aab8 = 17, aab9 = 18, aabb1 = 19, aabb2 = 20, aabb3 = 21, aabb4 = 22, aabb5 = 23, aabb6 = 24, aabb7 = 25, aabb8 = 26, aabb9 = 27}));

{a1 = 1.461172682, a2 = 0.4611726819, a3 =0 .4611726819, a4 = 8.077654636, a5 = 9.077654636, a6 = 8.077654636, a7 = -8.538827318, a8 = -8.538827318, a9 = -7.538827318, b1 = -3.221842064, b2 = -2.273083473, b3 = -2.324324882, b4 = 10.46946092, b5 = 8.571943737, b6 = 8.674426555, b7 = -7.673550001, b8 = -6.724791410, b9 = -6.776032819, c1 = 31.22184206, c2 = 31.27308347, c3 = 32.32432488, c4 = 20.53053908, c5 = 23.42805626, c6 = 24.32557344, c7 = 41.67355000, c8 = 41.72479141, c9 = 42.77603282}

 

This is an answer to the second question. The answer to the first question is obvious.

Basic variables corresponds to the maximum set of linearly independent columns of the leading matrix of the system. The remaining variables are the free variables, which we consider as parameters. Selection of basic variables is ambiguous. I think the default Maple selects the first set in lexicographical order. For example in solving of  {x-y + z = 2, 2 * x + y-z = 3}  Maple expresses  x  and  y  through  z . But for the system  {x-y+z=2, 2*x-2*y-3*z=5}  x  and  z  are expressed through  , because the first 2 columns are linearly dependent. 

You can choose yourself the basic variables (if the corresponding columns are linearly independent) as in the example:

solve({x-y+z=2, 2*x+y-3*z=5}, {x,z});

                                   {x = 11/5+2/5*y, z = -1/5+3/5*y} 

Maple does not directly plot the set. Some simplification is necessary to this end:

evalc(eval(abs(z+1+I)>=abs(z-1-I), z=x+I*y));

map(t->t^2, %);

plots[inequal](%, x=-5..5, y=-5..5, color="LightGreen");

                              

 

 

 

twinprimes:=proc(a,Lim)

local a0, b;

a0:=`if`(a::prime, a, nextprime(a));  b:=nextprime(a0);

while b<=Lim do

if b-a0=2 then  lprint([a0,b])  fi;

a0:=b; b:=nextprime(a0);

od;

NULL;

end proc:

 

Example of use:

twinprimes(10,1000);

 

Edited.

Consider the graphical solution, using the continuity of the function  (a,x)->log[2*abs(x-a)](abs(x+a)+abs(x-a))-1  in some regions. To find these regions we consider the lines on which no solutions:

log[2*abs(x-a)](abs(x+a)+abs(x-a)) = 1  is equivalent to  abs(x+a) = abs(x-a)

2*abs(x-a) = 0  is equivalent to  x=a

2*abs(x-a) = 1  is equivalent to  x=a+1/2  or  x=a-1/2

abs(x+a)+abs(x-a)=0  is equivalent to  one point  a=0  and  x=0

 

The plot of these lines:

 plots[implicitplot]([abs(x+a)=abs(x-a), x-a=0, 2*abs(x-a)=1], a=-1..1, x=-1..1, color=[red,blue,green], thickness=4, gridrefine=5, axes=normal);

                                    

 

It is enough to consider only the regions for  a>0  because the mapping  (a,x)->(-a,-x)  does not change the inequality. 

Consider one point in each of the 6 regions and the truthfulness of the inequality in these regions:

R1, R2, R3, R4, R5, R6 := [0.5,-0.5], [0.2,-0.2], [0.5,0.2], [1,0.2], [0.5,0.7], [0.5,2]:

seq(is(eval(log[2*abs(x-a)](abs(x+a)+abs(x-a))<1, [a,x]=~(R||i))), i=1..6) ;

                                  true, false, true, false, true, false

 

So the final solution:

If  a<=-1/2  then  x>0  or  a<x<a+1/2  or  a-1/2<x<a

If  -1/2<a<0  then  x>a+1/2  or  a<x<0  or  a-1/2<x<a

If  0<a<=1/2  then  x<a-1/2  or  0<x<a  or  a<x<a+1/2

If  a>1/2  then  x<0  or  a-1/2<x<a  or  a<x<a+1/2

 

Addition: visualization of all the solutions (yellow regions):

P:=plots[implicitplot]([abs(x+a)=abs(x-a), x-a=0, 2*abs(x-a)=1], a=-1..1, x=-1..1, color=[red,blue,green], thickness=2, gridrefine=3, axes=normal):

A:=plots[inequal](x>piecewise(a<-1/2,0,a>-1/2 and a<0,a+1/2), a=-1..0, x=0..1, color=yellow):

B:=plots[inequal]({x<piecewise(a<-1/2,a+1/2,a>-1/2 and a<0,0), x>a-1/2}, a=-1..0, x=-1..0, color=yellow):

C:=plots[inequal]({x>piecewise(a>0 and a<1/2,0,a>1/2,a-1/2),x<a+1/2}, a=0..1, x=0..1, color=yellow):

E:=plots[inequal](x<piecewise(a>0 and a<1/2,a-1/2,a>1/2,0), a=0..1, x=-1..0, color=yellow):

plots[display](P,A,B,C,E);

                                     

 

Clarification: on the blue line there are no solutions. 



 

                                                                       

The following code finds all the fours of integer points on your sphere (for which all 12 coordinates are different) and which form a square:

restart;

n := 0:

for x from -13 to 17 do

for y from -11 to 19 do

for z from -9 to 21 do

if (x-2)^2+(y-4)^2+(z-6)^2 = 225 then n := n+1; S[n] := [x, y, z] fi;

od: od: od:

S := convert(S, set):

S1 := combinat[choose](S, 3):

n:=0:

for s in S1 do

P:=combinat[permute](s);

for p in P do

if add((p[2]-p[1])*~(p[3]-p[1]))=0 and add((p[2]-p[1])^~2)=add((p[3]-p[1])^~2) then n:=n+1;

L[n]:={op(p),p[2]+p[3]-p[1]}; break fi;

od: od:

L:=convert(L, set):

Squares:=select(p->nops(op~(p))=12, L); nops(%);

 

 Squares_on_sphere.mw

 

restart;

k:=x->piecewise(type(x,even),x/2, 3*x+1);

for m from 1 to 60 do

s:='s';  m1:=m;  n:=1;  s[1]:=m1;

while m1>1 do n:=n+1;  m1:=k(m1); s[n]:=m1;  od:

print(op(convert(s, list)));

od: 

 

Addition: If you need to check the conjecture for any set of numbers, these numbers are convenient to specify as a list or a set.

Example for 2 numbers  10^6  and  10^9 :

restart;

k:=x->`if`(type(x,even),x/2, 3*x+1):

for m in [10^6, 10^9] do

s:='s';  m1:=m;  n:=1;  s[1]:=m1;

while m1>1 do n:=n+1;  m1:=k(m1); s[n]:=m1; od:

print(op(convert(s,list)));

od:

First 196 197 198 199 200 201 202 Last Page 198 of 290