[CAS] listLCM Help - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: [CAS] listLCM Help (/thread-6903.html) [CAS] listLCM Help - compsystems - 09-22-2016 09:49 PM 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 RE: [CAS] listLCM Help - zahi48g - 09-22-2016 10:45 PM Try MAKELIST(K,K,1,99,1), the output will be a list ,not a vector then lcm will work . RE: [CAS] listLCM Help - toshk - 09-22-2016 10:51 PM without the container [], {} your are inputting a bunch of arguments into the function call which is intended for a single input argument or (passing max intended arguments) for a function . the arguments will fail your test. typo in the first input: listLCM(list1)---> shd be lst1. it works with listLCM(lst1) RE: [CAS] listLCM Help - compsystems - 09-23-2016 01:53 PM (09-22-2016 10:45 PM)zahi48g Wrote:  Try MAKELIST(K,K,1,99,1), the output will be a list, not a vector then lcm will work . OK, I have adhered to the code TYPE(lst)==4 for support vectors, please check the code updated above Now appears the following: Using the double dot operator (..) on SEQ CMD, works to 97 elements, but the maximum number of recursion is 100, therefore, why not operate between 98..100? lst3:=seq(k,k = (1 .. 101), 1) listLCM(lst3) -> error // OK lst4:=seq(k,k = (1 .. 100), 1) listLCM(lst4) -> error WHY? lst4a:=seq(k,k = (1 .. 99), 1) listLCM(lst4a) -> error WHY? lst4b:=seq(k,k = (1 .. 98), 1) listLCM(lst4b) -> error WHY? lst5:=seq(k,k = (1 .. 97), 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 listLCM(lst5) -> 69720375229712477164533808935312303556800 RE: [CAS] listLCM Help - zahi48g - 09-23-2016 04:58 PM Hi I'm not an expert ! I am here to learn . I said "Try " !! To me , as the output of seq uses square-bracket , I would rather use MAKELIST whose output uses curved bracket . But I've checked , and you may be right since lcm uses any type of bracket so I can't be more helpful .