Talk:Special factorials: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Reverse factorial algorithm: added a couple of comments.)
(→‎Reverse factorial algorithm: Fixed syntax highlighting)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Reverse factorial algorithm ===
=== Reverse factorial algorithm ===
I took a stab at translating the reverse factorial algorithm used in the Factor entry to Java. It should be almost as efficient as taking the factorial itself.
I took a stab at translating the reverse factorial algorithm used in the Factor entry to Java. It should be almost as efficient as taking the factorial itself.
<syntaxhighlight lang="java">
<lang java>public static int rf(int n) {
public static int rf(int n) {
if (n == 1)
return 0; //1 has two answers -- return the lower one

int a = 1;
int a = 1;
int b = 1;
int b = 1;
Line 11: Line 15:
return b;
return b;
else return -1; //undefined
else return -1; //undefined
}</lang>
}
</syntaxhighlight>
--[[User:Chunes|Chunes]] ([[User talk:Chunes|talk]]) 17:06, 16 March 2021 (UTC)
--[[User:Chunes|Chunes]] ([[User talk:Chunes|talk]]) 17:06, 16 March 2021 (UTC)


: Note that the &nbsp; ''factorial inverse'' &nbsp; (or &nbsp;''reverse factorial'') &nbsp; of &nbsp; '''unity''' &nbsp; has two possible answers: &nbsp; '''zero''' &nbsp; and &nbsp; '''unity'''.
: Note that the &nbsp; ''factorial inverse'' &nbsp; (or &nbsp;''reverse factorial'') &nbsp; of &nbsp; '''unity''' &nbsp; has two possible answers: &nbsp; '''zero''' &nbsp; and &nbsp; '''unity'''.


: It is normal when searching a series &nbsp; (in this case, the series of factorial products) &nbsp; to use the first match found in the series. &nbsp; &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 17:37, 16 March 2021 (UTC)
: It is normal when searching a series &nbsp; (in this case, the series of factorial products) &nbsp; to use the first match found in the series. &nbsp; &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 17:37, 16 March 2021 (UTC)

:: Good catch. I revised the algorithm above and will make a note about it in the task description. --[[User:Chunes|Chunes]] ([[User talk:Chunes|talk]]) 17:58, 16 March 2021 (UTC)


=== Why is af(0) 0? ===
=== Why is af(0) 0? ===
Is it that 0 is the additive identity the way that factorial of 0 is 1 is the multiplicative identity? If so why should it be the identity anyway?
Is it that 0 is the additive identity the way that factorial of 0 is 1 is the multiplicative identity? If so why should it be the identity anyway?
: I think it's just that the formula literally produces 0 for n = 0.
: I think it's just that the formula literally produces 0 for n = 0.
: First run through, k is 1, this produces -1. Second run through, k is 0, this produces 1 and their sum is 0. --[[User:Chunes|Chunes]] ([[User talk:Chunes|talk]]) 17:36, 16 March 2021 (UTC)
: First run through, i is 1, this produces -1. Second run through, i is 0, this produces 1 and their sum is 0. --[[User:Chunes|Chunes]] ([[User talk:Chunes|talk]]) 17:36, 16 March 2021 (UTC)

So the math summation notation goes backwards automatically when i > n? This may make a good task -- C will do one iteration and stop, etc. --[[User:Wherrera|Wherrera]] ([[User talk:Wherrera|talk]]) 21:11, 16 March 2021 (UTC)

Latest revision as of 20:42, 10 October 2022

Reverse factorial algorithm

I took a stab at translating the reverse factorial algorithm used in the Factor entry to Java. It should be almost as efficient as taking the factorial itself.

public static int rf(int n) {
    if (n == 1)
      return 0; //1 has two answers -- return the lower one

    int a = 1;
    int b = 1;
    while (n > a) {
      b++;
      a = a * b;
    }
    if (a == n)
      return b;
    else return -1; //undefined
  }

--Chunes (talk) 17:06, 16 March 2021 (UTC)

Note that the   factorial inverse   (or  reverse factorial)   of   unity   has two possible answers:   zero   and   unity.
It is normal when searching a series   (in this case, the series of factorial products)   to use the first match found in the series.     -- Gerard Schildberger (talk) 17:37, 16 March 2021 (UTC)
Good catch. I revised the algorithm above and will make a note about it in the task description. --Chunes (talk) 17:58, 16 March 2021 (UTC)

Why is af(0) 0?

Is it that 0 is the additive identity the way that factorial of 0 is 1 is the multiplicative identity? If so why should it be the identity anyway?

I think it's just that the formula literally produces 0 for n = 0.
First run through, i is 1, this produces -1. Second run through, i is 0, this produces 1 and their sum is 0. --Chunes (talk) 17:36, 16 March 2021 (UTC)

So the math summation notation goes backwards automatically when i > n? This may make a good task -- C will do one iteration and stop, etc. --Wherrera (talk) 21:11, 16 March 2021 (UTC)