Multiplying elements of a list












7












$begingroup$


Given I have a following list of numbers:



l={2,3,4,5,6}


I wonder how to multiply each number by the one before of it,
such that I get



{2*3,2*3*4,2*3*4*5,...}


Also how can I chose a level for this operation?
by level I mean,



level 1 : 2*3



level 2 : 2*3*4



and so on.










share|improve this question









$endgroup$

















    7












    $begingroup$


    Given I have a following list of numbers:



    l={2,3,4,5,6}


    I wonder how to multiply each number by the one before of it,
    such that I get



    {2*3,2*3*4,2*3*4*5,...}


    Also how can I chose a level for this operation?
    by level I mean,



    level 1 : 2*3



    level 2 : 2*3*4



    and so on.










    share|improve this question









    $endgroup$















      7












      7








      7





      $begingroup$


      Given I have a following list of numbers:



      l={2,3,4,5,6}


      I wonder how to multiply each number by the one before of it,
      such that I get



      {2*3,2*3*4,2*3*4*5,...}


      Also how can I chose a level for this operation?
      by level I mean,



      level 1 : 2*3



      level 2 : 2*3*4



      and so on.










      share|improve this question









      $endgroup$




      Given I have a following list of numbers:



      l={2,3,4,5,6}


      I wonder how to multiply each number by the one before of it,
      such that I get



      {2*3,2*3*4,2*3*4*5,...}


      Also how can I chose a level for this operation?
      by level I mean,



      level 1 : 2*3



      level 2 : 2*3*4



      and so on.







      list-manipulation multiplcation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 21 at 13:11









      WilliamWilliam

      84658




      84658






















          5 Answers
          5






          active

          oldest

          votes


















          7












          $begingroup$

          FoldList[Times, l] (* or *)
          FoldList[Times]@l



          {2, 6, 24, 120, 720}




          Also:



          Exp @ Accumulate @ Log @ l



          {2, 6, 24, 120, 720}







          share|improve this answer











          $endgroup$





















            4












            $begingroup$

            list = Range[2, 6]

            (* {2, 3, 4, 5, 6} *)


            To keep the factors separate, use Inactive with kglr's solution



            list2 = Rest@FoldList[Inactive@Times, list[[1]], Rest@list]


            enter image description here



            Activate produces the result



            list3 = list2 // Activate

            (* {6, 24, 120, 720} *)


            Or use NonCommutativeMultiply to hold the factors



            list4 = Rest@FoldList[NonCommutativeMultiply, list[[1]], Rest@list]

            (* {2 ** 3, 2 ** 3 ** 4, 2 ** 3 ** 4 ** 5, 2 ** 3 ** 4 ** 5 ** 6} *)


            Then Apply Times to get the final result



            list5 = Times @@@ list4

            (* {6, 24, 120, 720} *)


            For this specific case (sequential numbers), the result is just Factorial



            list3 == list5 == Factorial /@ Rest@list

            (* True *)


            Use Part to access any element of the result, e.g.,



            list3[[1]]

            (* 6 *)





            share|improve this answer









            $endgroup$





















              3












              $begingroup$

              This can also be solved using recursion. The function cumProd (cumulative product) can be defined as:



              list = Range[10];
              cumProd[n_] := cumProd[n - 1]*list[[n]];
              cumProd[1] = list[[1]];


              To use:



              cumProd[6]
              720


              gives the 6th "level" of the product. Of course, list can be any set of numbers. Applying this to the whole list:



              cumProd/@Range[Length[list]]
              {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}





              share|improve this answer









              $endgroup$





















                2












                $begingroup$

                We can use the MapIndexed function



                 list = {2, 3, 4, 5, 6}
                f[x_, {i_}] := Times @@ list[[1 ;; i]]
                Rest[MapIndexed[f, list, {1}]]


                (* 6, 24, 120, 720} *)






                share|improve this answer









                $endgroup$





















                  1












                  $begingroup$

                  here is your level function



                  level[x_] := Times @@ l[[;; x + 1]];

                  level[1]
                  level[2]



                  6

                  24







                  share|improve this answer









                  $endgroup$













                    Your Answer





                    StackExchange.ifUsing("editor", function () {
                    return StackExchange.using("mathjaxEditing", function () {
                    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                    });
                    });
                    }, "mathjax-editing");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "387"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: false,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: null,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191937%2fmultiplying-elements-of-a-list%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    7












                    $begingroup$

                    FoldList[Times, l] (* or *)
                    FoldList[Times]@l



                    {2, 6, 24, 120, 720}




                    Also:



                    Exp @ Accumulate @ Log @ l



                    {2, 6, 24, 120, 720}







                    share|improve this answer











                    $endgroup$


















                      7












                      $begingroup$

                      FoldList[Times, l] (* or *)
                      FoldList[Times]@l



                      {2, 6, 24, 120, 720}




                      Also:



                      Exp @ Accumulate @ Log @ l



                      {2, 6, 24, 120, 720}







                      share|improve this answer











                      $endgroup$
















                        7












                        7








                        7





                        $begingroup$

                        FoldList[Times, l] (* or *)
                        FoldList[Times]@l



                        {2, 6, 24, 120, 720}




                        Also:



                        Exp @ Accumulate @ Log @ l



                        {2, 6, 24, 120, 720}







                        share|improve this answer











                        $endgroup$



                        FoldList[Times, l] (* or *)
                        FoldList[Times]@l



                        {2, 6, 24, 120, 720}




                        Also:



                        Exp @ Accumulate @ Log @ l



                        {2, 6, 24, 120, 720}








                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 21 at 14:02

























                        answered Feb 21 at 13:18









                        kglrkglr

                        189k10206424




                        189k10206424























                            4












                            $begingroup$

                            list = Range[2, 6]

                            (* {2, 3, 4, 5, 6} *)


                            To keep the factors separate, use Inactive with kglr's solution



                            list2 = Rest@FoldList[Inactive@Times, list[[1]], Rest@list]


                            enter image description here



                            Activate produces the result



                            list3 = list2 // Activate

                            (* {6, 24, 120, 720} *)


                            Or use NonCommutativeMultiply to hold the factors



                            list4 = Rest@FoldList[NonCommutativeMultiply, list[[1]], Rest@list]

                            (* {2 ** 3, 2 ** 3 ** 4, 2 ** 3 ** 4 ** 5, 2 ** 3 ** 4 ** 5 ** 6} *)


                            Then Apply Times to get the final result



                            list5 = Times @@@ list4

                            (* {6, 24, 120, 720} *)


                            For this specific case (sequential numbers), the result is just Factorial



                            list3 == list5 == Factorial /@ Rest@list

                            (* True *)


                            Use Part to access any element of the result, e.g.,



                            list3[[1]]

                            (* 6 *)





                            share|improve this answer









                            $endgroup$


















                              4












                              $begingroup$

                              list = Range[2, 6]

                              (* {2, 3, 4, 5, 6} *)


                              To keep the factors separate, use Inactive with kglr's solution



                              list2 = Rest@FoldList[Inactive@Times, list[[1]], Rest@list]


                              enter image description here



                              Activate produces the result



                              list3 = list2 // Activate

                              (* {6, 24, 120, 720} *)


                              Or use NonCommutativeMultiply to hold the factors



                              list4 = Rest@FoldList[NonCommutativeMultiply, list[[1]], Rest@list]

                              (* {2 ** 3, 2 ** 3 ** 4, 2 ** 3 ** 4 ** 5, 2 ** 3 ** 4 ** 5 ** 6} *)


                              Then Apply Times to get the final result



                              list5 = Times @@@ list4

                              (* {6, 24, 120, 720} *)


                              For this specific case (sequential numbers), the result is just Factorial



                              list3 == list5 == Factorial /@ Rest@list

                              (* True *)


                              Use Part to access any element of the result, e.g.,



                              list3[[1]]

                              (* 6 *)





                              share|improve this answer









                              $endgroup$
















                                4












                                4








                                4





                                $begingroup$

                                list = Range[2, 6]

                                (* {2, 3, 4, 5, 6} *)


                                To keep the factors separate, use Inactive with kglr's solution



                                list2 = Rest@FoldList[Inactive@Times, list[[1]], Rest@list]


                                enter image description here



                                Activate produces the result



                                list3 = list2 // Activate

                                (* {6, 24, 120, 720} *)


                                Or use NonCommutativeMultiply to hold the factors



                                list4 = Rest@FoldList[NonCommutativeMultiply, list[[1]], Rest@list]

                                (* {2 ** 3, 2 ** 3 ** 4, 2 ** 3 ** 4 ** 5, 2 ** 3 ** 4 ** 5 ** 6} *)


                                Then Apply Times to get the final result



                                list5 = Times @@@ list4

                                (* {6, 24, 120, 720} *)


                                For this specific case (sequential numbers), the result is just Factorial



                                list3 == list5 == Factorial /@ Rest@list

                                (* True *)


                                Use Part to access any element of the result, e.g.,



                                list3[[1]]

                                (* 6 *)





                                share|improve this answer









                                $endgroup$



                                list = Range[2, 6]

                                (* {2, 3, 4, 5, 6} *)


                                To keep the factors separate, use Inactive with kglr's solution



                                list2 = Rest@FoldList[Inactive@Times, list[[1]], Rest@list]


                                enter image description here



                                Activate produces the result



                                list3 = list2 // Activate

                                (* {6, 24, 120, 720} *)


                                Or use NonCommutativeMultiply to hold the factors



                                list4 = Rest@FoldList[NonCommutativeMultiply, list[[1]], Rest@list]

                                (* {2 ** 3, 2 ** 3 ** 4, 2 ** 3 ** 4 ** 5, 2 ** 3 ** 4 ** 5 ** 6} *)


                                Then Apply Times to get the final result



                                list5 = Times @@@ list4

                                (* {6, 24, 120, 720} *)


                                For this specific case (sequential numbers), the result is just Factorial



                                list3 == list5 == Factorial /@ Rest@list

                                (* True *)


                                Use Part to access any element of the result, e.g.,



                                list3[[1]]

                                (* 6 *)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 21 at 14:58









                                Bob HanlonBob Hanlon

                                61k33597




                                61k33597























                                    3












                                    $begingroup$

                                    This can also be solved using recursion. The function cumProd (cumulative product) can be defined as:



                                    list = Range[10];
                                    cumProd[n_] := cumProd[n - 1]*list[[n]];
                                    cumProd[1] = list[[1]];


                                    To use:



                                    cumProd[6]
                                    720


                                    gives the 6th "level" of the product. Of course, list can be any set of numbers. Applying this to the whole list:



                                    cumProd/@Range[Length[list]]
                                    {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}





                                    share|improve this answer









                                    $endgroup$


















                                      3












                                      $begingroup$

                                      This can also be solved using recursion. The function cumProd (cumulative product) can be defined as:



                                      list = Range[10];
                                      cumProd[n_] := cumProd[n - 1]*list[[n]];
                                      cumProd[1] = list[[1]];


                                      To use:



                                      cumProd[6]
                                      720


                                      gives the 6th "level" of the product. Of course, list can be any set of numbers. Applying this to the whole list:



                                      cumProd/@Range[Length[list]]
                                      {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}





                                      share|improve this answer









                                      $endgroup$
















                                        3












                                        3








                                        3





                                        $begingroup$

                                        This can also be solved using recursion. The function cumProd (cumulative product) can be defined as:



                                        list = Range[10];
                                        cumProd[n_] := cumProd[n - 1]*list[[n]];
                                        cumProd[1] = list[[1]];


                                        To use:



                                        cumProd[6]
                                        720


                                        gives the 6th "level" of the product. Of course, list can be any set of numbers. Applying this to the whole list:



                                        cumProd/@Range[Length[list]]
                                        {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}





                                        share|improve this answer









                                        $endgroup$



                                        This can also be solved using recursion. The function cumProd (cumulative product) can be defined as:



                                        list = Range[10];
                                        cumProd[n_] := cumProd[n - 1]*list[[n]];
                                        cumProd[1] = list[[1]];


                                        To use:



                                        cumProd[6]
                                        720


                                        gives the 6th "level" of the product. Of course, list can be any set of numbers. Applying this to the whole list:



                                        cumProd/@Range[Length[list]]
                                        {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Feb 21 at 15:20









                                        bill sbill s

                                        54.7k377157




                                        54.7k377157























                                            2












                                            $begingroup$

                                            We can use the MapIndexed function



                                             list = {2, 3, 4, 5, 6}
                                            f[x_, {i_}] := Times @@ list[[1 ;; i]]
                                            Rest[MapIndexed[f, list, {1}]]


                                            (* 6, 24, 120, 720} *)






                                            share|improve this answer









                                            $endgroup$


















                                              2












                                              $begingroup$

                                              We can use the MapIndexed function



                                               list = {2, 3, 4, 5, 6}
                                              f[x_, {i_}] := Times @@ list[[1 ;; i]]
                                              Rest[MapIndexed[f, list, {1}]]


                                              (* 6, 24, 120, 720} *)






                                              share|improve this answer









                                              $endgroup$
















                                                2












                                                2








                                                2





                                                $begingroup$

                                                We can use the MapIndexed function



                                                 list = {2, 3, 4, 5, 6}
                                                f[x_, {i_}] := Times @@ list[[1 ;; i]]
                                                Rest[MapIndexed[f, list, {1}]]


                                                (* 6, 24, 120, 720} *)






                                                share|improve this answer









                                                $endgroup$



                                                We can use the MapIndexed function



                                                 list = {2, 3, 4, 5, 6}
                                                f[x_, {i_}] := Times @@ list[[1 ;; i]]
                                                Rest[MapIndexed[f, list, {1}]]


                                                (* 6, 24, 120, 720} *)







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Feb 21 at 17:50









                                                user63033user63033

                                                211




                                                211























                                                    1












                                                    $begingroup$

                                                    here is your level function



                                                    level[x_] := Times @@ l[[;; x + 1]];

                                                    level[1]
                                                    level[2]



                                                    6

                                                    24







                                                    share|improve this answer









                                                    $endgroup$


















                                                      1












                                                      $begingroup$

                                                      here is your level function



                                                      level[x_] := Times @@ l[[;; x + 1]];

                                                      level[1]
                                                      level[2]



                                                      6

                                                      24







                                                      share|improve this answer









                                                      $endgroup$
















                                                        1












                                                        1








                                                        1





                                                        $begingroup$

                                                        here is your level function



                                                        level[x_] := Times @@ l[[;; x + 1]];

                                                        level[1]
                                                        level[2]



                                                        6

                                                        24







                                                        share|improve this answer









                                                        $endgroup$



                                                        here is your level function



                                                        level[x_] := Times @@ l[[;; x + 1]];

                                                        level[1]
                                                        level[2]



                                                        6

                                                        24








                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Feb 21 at 13:42









                                                        J42161217J42161217

                                                        3,968323




                                                        3,968323






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Mathematica Stack Exchange!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            Use MathJax to format equations. MathJax reference.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191937%2fmultiplying-elements-of-a-list%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            Index of /

                                                            Tribalistas

                                                            Listed building