[CAS] listLCM Help
09-22-2016, 09:49 PM (This post was last modified: 09-23-2016 07:37 PM by compsystems.)
Post: #1
 compsystems Senior Member Posts: 1,193 Joined: Dec 2013
[CAS] listLCM Help
Hello Earthlings =)

listLCM is a recursive function to calculate the least common multiple (LCM) of a list of integers numbers, using the internal function of the CAS LCM
/!\ The HP-PRIME CAS is limited to 100 recursions =(

listLCM(arg) Where the function accepts as argument a vector [...] or a list {...}

Code:
 #cas   listLCM(lst):=   BEGIN     print(""); print([type(lst),TYPE(lst)]); halt;     if not(TYPE(lst)==6 or type(lst)==DOM_LIST or TYPE(lst)==4) then        return( [ "Error: /!\\ Invalid Data Type", lst ] );     end;     if dim(lst)==0 then       return( [ "Error: Bad Argument type. Empty list", lst ] );     end;     if dim(lst)==1 then        return( lst[1] );     end;     if dim(lst)>100 then        return( [ "/!\\ Maximum recursion: 100 elements" , lst ]);     end;     if dim(lst)==2 then        return( lcm(lst[1],lst[2]) );     end;     if dim(lst)≥3 then        // return( listLCM(concat({lcm(lst[1],lst[2])},right(lst,dim(lst)-2) ) ) );        return( listLCM(concat({lcm(lst[1],lst[2])},tail(tail(lst)) ) ) );     end;   END; #end

listLCM({}); returns [ "Error: Bad Argument type. Empty list",{} ] OK
listLCM([]); returns [ "Error: Bad Argument type. Empty list",[] ] OK
listLCM("abc"); returns ["Error: /!\ Invalid Data Type","abc"] OK

listLCM({1,2,3}); returns 6 // OK

listLCM({1,2,3,4}); returns 12 // OK

listLCM({x-1,x-2}); returns x^2-3*x+2 // ok

listLCM({x-1,x-2,x-3}) ; returns x^3-6*x^2+11*x-6 // ok

listLCM({x-1,x-2,x-3,x-3,x-3}) ; returns x^3-6*x^2+11*x-6 // ok

listLCM({x-1,x-2,x-3,x-4}) ; returns x^4-10*x^3+35*x^2-50*x+24 // ok

listLCM([1,2,3,4,5]); returns 60 // ok

Now generating vectors, lists, and matrices with commands

lst0:=MAKELIST(I,I,1,100,1) ->{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30​,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,5​7,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,​84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100}

[type(lst0),TYPE(lst0)]; returns [DOM_LIST,6]

listLCM(lst0); returns 69720375229712477164533808935312303556800 OK

lst1:=(MAKEMAT(abs(I-J)+1,1,100))[1]; returns [1,2,3,...,96,97,98,99,100]

[type(lst1),TYPE(lst1)]; returns [DOM_LIST,4]

listLCM(lst1); returns 69720375229712477164533808935312303556800 OK

lst2:=seq(k,k,1,100); returns [1,2,3,...,96,97,98,99,100]

[type(lst2),TYPE(lst2)]; returns [DOM_LIST,4]

listLCM(lst2); returns 69720375229712477164533808935312303556800 OK

lst2a:=seq(k,k,1,101,1) ; returns [1,2,3,4,5, ..., 101]
listLCM(lst2a); returns "/!\ Maximum recursion: 100" OK

Using the double dot operator (..) on seq cmd
lst3:=seq(k, k = 1 .. 100,1); returns
[ 1,2,3,...,96,97,98,99,100 ]
with conteiners [ ] on the history view

now, goto the entry line
[^](key up) {copy} (menu), disappear the conteiners [] WHY?
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30​,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,5​7,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,​84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100

type(lst3); returns DOM_LIST
dim(lst3); returns 100 // objet list of 100 elements
 « Next Oldest | Next Newest »

 Messages In This Thread [CAS] listLCM Help - compsystems - 09-22-2016 09:49 PM RE: [CAS] listLCM Help - zahi48g - 09-22-2016, 10:45 PM RE: [CAS] listLCM Help - compsystems - 09-23-2016, 01:53 PM RE: [CAS] listLCM Help - toshk - 09-22-2016, 10:51 PM RE: [CAS] listLCM Help - zahi48g - 09-23-2016, 04:58 PM

User(s) browsing this thread: 1 Guest(s)