# How to find the product of all elements in a list?

4 messages
Open this post in threaded view
|

## How to find the product of all elements in a list?

 Is there a neat hack to multiply all the numbers in a list other than recursive methods? -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: How to find the product of all elements in a list?

 TLC123 wrote > Is there a neat hack to multiply all the numbers in a list other than > recursive methods? If you're goal is "avoid recursion" I think you could do it with a C-style for loop.  However, in my experience, the C-style for loops are slower than recursion, so their only potential advantage would be avoiding recursion depth limits. You could take the log of the items in the list, multiply by a vector of all ones to sum, and then apply exp.   function prod(v) =   let(        logv = [for(item=v) ln(item)],        ones = [for(i=[0:1:len(v)-1]) 1]   )   exp(logv*ones); -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org