<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to calculate the factorial of a number in a Splunk search? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200995#M58266</link>
    <description>&lt;P&gt;By the way, if you find a different answer that works for you please post it here so that others can benefit from it.&lt;/P&gt;</description>
    <pubDate>Mon, 15 Feb 2016 09:45:15 GMT</pubDate>
    <dc:creator>javiergn</dc:creator>
    <dc:date>2016-02-15T09:45:15Z</dc:date>
    <item>
      <title>How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200990#M58261</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I want to calculate factorial of a number in &lt;CODE&gt;eval&lt;/CODE&gt; for calculating Poisson value.&lt;BR /&gt;
Please let me know if it is possible.&lt;/P&gt;

&lt;P&gt;Thanks, &lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 09:18:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200990#M58261</guid>
      <dc:creator>shrirangphadke</dc:creator>
      <dc:date>2016-02-11T09:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200991#M58262</link>
      <description>&lt;P&gt;You can use a lookup table. Precompute as many factorials as you think you may need (probably not many, considering how quickly they grow) and then look them up as you need.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 11:43:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200991#M58262</guid>
      <dc:creator>ichaer_splunk</dc:creator>
      <dc:date>2016-02-11T11:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200992#M58263</link>
      <description>&lt;P&gt;The splunk eval functions dont offer factorial computator.&lt;/P&gt;

&lt;P&gt;You could create a custom command &amp;amp; offload the factorial generation logic to the python code.&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/AdvancedDev/Searchscripts"&gt;http://docs.splunk.com/Documentation/Splunk/6.2.3/AdvancedDev/Searchscripts&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;A href="http://blogs.splunk.com/2014/04/14/building-custom-search-commands-in-python-part-i-a-simple-generating-command/"&gt;http://blogs.splunk.com/2014/04/14/building-custom-search-commands-in-python-part-i-a-simple-generating-command/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 13:14:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200992#M58263</guid>
      <dc:creator>stanwin</dc:creator>
      <dc:date>2016-02-11T13:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200993#M58264</link>
      <description>&lt;P&gt;You could try an approximation by using the natural logarithm (see formula in picture below).&lt;BR /&gt;
That should work fine when n is not too big, if n is big enough you might have approximation errors. Using the Stirling's approximation is more accurate in this case but it won't be easy to implement with the Splunk built-in commands.&lt;/P&gt;

&lt;P&gt;See an example below for the natural logarithm approximation when n=5:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| fields - count
| eval n = 5
| eval ki = mvrange(1, n+1)
| mvexpand ki
| eval ln_ki = ln(ki)
| eventstats sum(ln_ki) as sum_ln_ki by n
| eval n_factorial = round(exp(sum_ln_ki))
| stats first(n_factorial) as n_factorial by n
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Alternative that performs the exponential calculations at the end and might improve performance. Give it a go too:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| fields - count
| eval n = 5
| eval ki = mvrange(1, n+1)
| mvexpand ki
| eval ln_ki = ln(ki)
| eventstats sum(ln_ki) as sum_ln_ki by n
| stats count by n, sum_ln_ki
| eval n_factorial = round(exp(sum_ln_ki))
| fields - count, sum_ln_ki
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/1050i2D624880FF608425/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2016 13:23:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200993#M58264</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-02-12T13:23:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200994#M58265</link>
      <description>&lt;P&gt;You could try the App for R.  It doesn't seem to be on Splunkbase any more, but it's apparently available from a link the_wolverine supplies in this answer for &lt;A href="https://answers.splunk.com/answers/316994/what-happened-to-the-r-project-app-1.html"&gt;what happened to the R project&lt;/A&gt;.  To help prevent double-hopping, here's the link they supplied: &lt;A href="https://github.com/rfsp/r"&gt;https://github.com/rfsp/r&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2016 13:43:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200994#M58265</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2016-02-12T13:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200995#M58266</link>
      <description>&lt;P&gt;By the way, if you find a different answer that works for you please post it here so that others can benefit from it.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2016 09:45:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200995#M58266</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-02-15T09:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200996#M58267</link>
      <description>&lt;P&gt;Hey thanks ! and sorry for late reply. Yes it did work for me. Also custom command option seems to be good&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 07:07:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/200996#M58267</guid>
      <dc:creator>shrirangphadke</dc:creator>
      <dc:date>2016-02-16T07:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the factorial of a number in a Splunk search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/519295#M146228</link>
      <description>&lt;P&gt;Here's Stirling's Approximation in SPL: `| eval n! = sqrt(2*pi()*n)*pow(n/exp(1), n)`&lt;/P&gt;</description>
      <pubDate>Sun, 13 Sep 2020 11:49:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-factorial-of-a-number-in-a-Splunk-search/m-p/519295#M146228</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2020-09-13T11:49:44Z</dc:date>
    </item>
  </channel>
</rss>

