Jabz

433 Reputation

5 Badges

10 years, 302 days

MaplePrimes Activity


These are answers submitted by Jabz

Hey i solved the problem found out what the cause waas.

 

Thanks to every1 who had a look at my proc.

I have found the problem but i don't know how to solve it??
The reason why i get all the ' yes ' is because of the code below but i can't see why maple is printing ' yes ' all the time . Individually this proc works fine (prints ' yes ' only once)  but in a loop like the one above it prints yes everytime.
 

RownColumn:=proc(n)                  #
local m, en, q, w,r,s, Arr:
m:=n:
en:=min(op(1,m)):
Arr:=Array(1..en,fill = 1):
q:=AddAlongDimension(m,1):

w:=AddAlongDimension(m,2):

r:=convert(q,Array);
s:=convert(w,Array);

if IsEqual(r,Arr) and IsEqual(s,Arr) then

print(yes):   #################
else
return(NO)  
fi:
end proc:

 

TRUE FOR 1 BUT NOT FOR THE OTHER


f:=x->ln(x):
g:=x->(x):
Better:=proc(n,m,c)
local r,d,f,g,i,w:
f:=n:
g:=m:
d:=[solve(f(x)=c*g(x),x)];
if d = [0] then
printf("%a and %a do not cross each other",f(x),g(x)):
plot({f(x),c*g(x)},x=0..100);
return():
else
for i from 1 to nops(d) do
r:=op(i,d);
if r = 0  then NULL else
w:=r:
lprint(f(x), `is better than`,g(x),`for lists greater than`,evalf(w)):

fi:
od:

plot({f(x),c*g(x)},x=0..100);
fi:
end proc:
Better(f,g,0.1);


ln(x), `is better than`, x, `for lists greater than`, 1.118325592           # TRUE
ln(x), `is better than`, x, `for lists greater than`, 35.77152064            #NOT TRUE
 

How do i add the elements of a particular row of a matrix. For example if i had a matrix [1,2,3],[234],[375]. The total of the first row would be 6, second row 9 and third 15.

The first column would be 6, second column 12 and third 12.
 

To add the diagonal of a matrix would it it be best to convert it to a list then total the list or is there a better way of adding the diagonal of a matrix??

Thank you everyone for your kind help i timed all the procedures just to see how efficient the procs wer. I think array's would be much more efficient than lists and the computation time wont be as long list , for a 2 * 10^6 element size array
 

restart:

AltSort:=proc(L)
local z, b, cnt:
z:=NULL:
b:=nops(L):
for cnt from 0 to ceil(b/2) do
if cnt = floor(b/2) and type(b,even) then
break;
end if:
z:=z,L[1+cnt]:
if cnt=floor(b/2)then
break;
end if:
z:=z,L[b-cnt]:
end do:
return([z]);
end proc:
L:=['ran()'$20000]
AltSort(L):
time(AltSort(L));

                                    2.714
ran:=rand(1..6):
L:=['ran()'$2000000]:

LgSm := proc( L )
  local n, S;
  S := sort(L);
  n := nops(L);
  [ seq( [S[i],S[-i]][], i=1..floor(n/2) ),
    `if`( n::odd, S[ceil(n/2)], NULL ) ];
end proc:
LgSm( L ):
time(LgSm(L));

                                    3.588

Alt:=proc(L)
  local n,S,i;
S:=sort(L);
n:=nops(L);

for i from 1 to floor(n/2) do
[S[i],S[-1]]:
if type(n,odd) then
S[ceil(n/2)]:
NULL
fi:
od:
return(S);
end proc:

Alt(L):
time(Alt(L));
                                    3.603
Thank u again everyone for ur kind help. I am very grateful.

I am not sure how to apply the counter to my program? To sort the list i think maple's ' sort ' command would be better than Bubble sort or cocktailsort. What about the following code its not a procedure but i think it can be made into a procedure. I'm not sure if the procedure would be efficient though.
 

L:=[1,2,4,6,7]:

z:=[]:

z:=[op(min(L)),op(max(L))];

t:=remove(has,L,min(L));

s:=remove(has,t,max(L));

L:=s:

r:=[op(min(L)),op(max(L))];

t:=remove(has,L,min(L));

s:=remove(has,t,max(L));

L:=s:

z:=[z,r,s];

map(op,z);
                              z : =     [1, 7]
                          t : =       [2, 4, 6, 7]
                           s : =        [2, 4, 6]
                            r : =       [2, 6]
                             t : =       [4, 6]
                              s : =       [4]
                      z : =      [[1, 7], [2, 6], [4]]
                               [1, 7, 2, 6, 4]
 

Thank you very much for your kind help. Unfortunately i am still an amature on maple but i did read everything you wrote and tried my best to understand it but because of the lack of knowledge in programming i did not grasp the whole concept but i am still grateful that you replied beause the information maybe useful to someone else. thank you

I just finished coding the Miller_Rabin code it seems to work fine but i am not sure if it will work for large primes.

 

Could someone check if my code is correct and if i could improve it.

restart:
Miller_Rabin:=proc(a,n)
local minus1, k, test:
minus1:=n-1:
k:=minus1:

while ((k mod 2) = 0) do
k:=k/2:
end do;

test:=Power(a,k) mod n:

if (test = 1) then
printf("%a to the power of %a = 1 mod n. Therefore %a is either a Prime or %a is a Miller Rabin Liar",a,k,n,a):
elif
(test = minus1) then
printf("%a to the power of %a  = -1 mod n.Therefore %a is either a Prime or %a is a Miller Rabin Liar",a,k,n,a):
else
printf("%a to the power of %a DOES NOT = 1 or -1.Therefore %a is most likely a composite",a,k,a)
end if:
end proc:
 

Thanx

 

I didnt know about the 'irem' command thank you.

I am trying to code a naive method for checking if a number is prime. where it checks if a number is prime by checking if a number 'k' divdes into n.

 

This is what i have done so far but it dosen't seem to be working properly any help would be greatly appriciated?


restart:
NaivePrimetest:=proc(n)
local t,k,prime:
prime:=true:
for k from 2 to (n)^1/2 do
t:=gcd(k,n):
if t > 1 then
prime:=false:
printf("%a is not a prime",n):

fi:
od:
printf("%a is probably a prime",n):
end proc:
 

What's the difference between a recursive bisection method and the following code???
 

 


Bi_section:=proc(A,B,N,f)
local a,b,c,s,Err:
a:=A:
b:=B:
s:=evalf[1](10^(-N)):
if a >= b then
      printf("Incorrect Values %a",[a,b]);
 else
   if sign(f(a)) = sign(f(b)) then
       printf("No Roots In The Interval %a",[a,b]);
  else
    Err:=s+1:
           while Err>s do
               c:=evalf((a+b)/2):
                #print('f'(c)=f(c)):
               if sign(f(a)) * sign(f(c)) < 0 then
                      a:=a:
                      b:=c:
                      Err:=(abs(f(c))):
                   elif sign(f(c))* sign(f(b)) < 0 then
                      a:=c:
                      b:=b:
                      Err:=(abs(f(c))):
                  
                   if f(c)=0                     then
                      Err:=0:
                 fi:
               fi:
          end do:
         fi:
        fi:
           printf("A solution to the function %a is %f to 6 dp",f(x),evalf(c)):
 end proc:

 

Is this method not recursive???

 

I can't  code merge sort i have tried over n over again but can't seem to succeed.i really need help.

i tried using rtable but i couldn't split the rtable. and also maple produces an error if i ' nops() ' a rtable. That is why i converted the array into a list.

Is my code correct for finding  the smallest element  in any lists above ?

i tried to compute so it reads the list from from right to left but nothing happens not even an error.

 

 

restart:
L:=rtable(1..6,random(1..10));
f:=convert(%,'list'):


for j from nops(f) to 1 do
for i from nops(f) to 1 do
if f[i] < f[i-1] then
( f[i-1],f[i] ) := ( f[i],f[i-1]);
else
(f[i-1],f[i] ) := (f[i-1], f[i]);
fi:od:od: print(f);
 

                            L:=[4 5 8 7 3 5]
                             [4, 5, 8, 7, 3, 5]
 

Initial conditions,     x(0)=2, D(x(0))= -2

D= differential

D2x +5Dx +4x = delta( t - 2 )
 

 

I want to check my answer in maple but i cant get it to work.

 

This is the answer i gt by hand

 

x = H( t - 2 ) ( ( e ^ - 4 ( t - 2 ) + e ^ - ( t - 2) ) / 3 ) - 4 / 3 e ^ - 4 ( t - 2 ) + 10 / 3 e ^ - ( t - 2 )

 

1 2 Page 1 of 2