R: error when trying to write an equivalent function n choose k










0















I'm taking the class of introduction for R programming.



we were asked to write a function that will be the same as n choose k:



choose(n, k)


we were asked to check if the function works by running n = 200, k = 50.



I wrote the following code:



 select_k <- function(n, k)
sr <- c(log10(log10((factorial(n-1)/factorial(k-1)*factorial(n-k-2)))*(n/k)))
return(sr)



as select_k is supposed to be the " n choose k".



my function works with values such as: 100 choose 25, but it doesn't work with greater values, like n = 200, k = = 50.



select_k( n = 200, k = 50)
[1] NaN
Warning message:
In factorial(n) : value out of range in 'gammafn'


I have no idea what else can be done to fix that.










share|improve this question



















  • 2





    What goes wrong? Do you get an error or the wrong answer?

    – Richard Telford
    Nov 14 '18 at 21:33






  • 2





    Instead of talking the log(factorial()), try using the lfactorial() function which is optimized for that particular operation. (though operates with base e)

    – MrFlick
    Nov 14 '18 at 21:41











  • yeah but when you put in: choose( n = 200 , k =50), it works.

    – Jneven
    Nov 14 '18 at 21:42











  • @MrFlick it doesn't matter that base e is much greater then the base of log10(log10))

    – Jneven
    Nov 14 '18 at 21:42






  • 2





    @Jneven. The problem is that you are taking the log "too late". If you push the logs into the function so log(factorial()/factorial()) becomes lfactorial()-lfactorial(), that's much easier for a computer to calculate.

    – MrFlick
    Nov 14 '18 at 21:46















0















I'm taking the class of introduction for R programming.



we were asked to write a function that will be the same as n choose k:



choose(n, k)


we were asked to check if the function works by running n = 200, k = 50.



I wrote the following code:



 select_k <- function(n, k)
sr <- c(log10(log10((factorial(n-1)/factorial(k-1)*factorial(n-k-2)))*(n/k)))
return(sr)



as select_k is supposed to be the " n choose k".



my function works with values such as: 100 choose 25, but it doesn't work with greater values, like n = 200, k = = 50.



select_k( n = 200, k = 50)
[1] NaN
Warning message:
In factorial(n) : value out of range in 'gammafn'


I have no idea what else can be done to fix that.










share|improve this question



















  • 2





    What goes wrong? Do you get an error or the wrong answer?

    – Richard Telford
    Nov 14 '18 at 21:33






  • 2





    Instead of talking the log(factorial()), try using the lfactorial() function which is optimized for that particular operation. (though operates with base e)

    – MrFlick
    Nov 14 '18 at 21:41











  • yeah but when you put in: choose( n = 200 , k =50), it works.

    – Jneven
    Nov 14 '18 at 21:42











  • @MrFlick it doesn't matter that base e is much greater then the base of log10(log10))

    – Jneven
    Nov 14 '18 at 21:42






  • 2





    @Jneven. The problem is that you are taking the log "too late". If you push the logs into the function so log(factorial()/factorial()) becomes lfactorial()-lfactorial(), that's much easier for a computer to calculate.

    – MrFlick
    Nov 14 '18 at 21:46













0












0








0








I'm taking the class of introduction for R programming.



we were asked to write a function that will be the same as n choose k:



choose(n, k)


we were asked to check if the function works by running n = 200, k = 50.



I wrote the following code:



 select_k <- function(n, k)
sr <- c(log10(log10((factorial(n-1)/factorial(k-1)*factorial(n-k-2)))*(n/k)))
return(sr)



as select_k is supposed to be the " n choose k".



my function works with values such as: 100 choose 25, but it doesn't work with greater values, like n = 200, k = = 50.



select_k( n = 200, k = 50)
[1] NaN
Warning message:
In factorial(n) : value out of range in 'gammafn'


I have no idea what else can be done to fix that.










share|improve this question
















I'm taking the class of introduction for R programming.



we were asked to write a function that will be the same as n choose k:



choose(n, k)


we were asked to check if the function works by running n = 200, k = 50.



I wrote the following code:



 select_k <- function(n, k)
sr <- c(log10(log10((factorial(n-1)/factorial(k-1)*factorial(n-k-2)))*(n/k)))
return(sr)



as select_k is supposed to be the " n choose k".



my function works with values such as: 100 choose 25, but it doesn't work with greater values, like n = 200, k = = 50.



select_k( n = 200, k = 50)
[1] NaN
Warning message:
In factorial(n) : value out of range in 'gammafn'


I have no idea what else can be done to fix that.







r error-handling binomial-coefficients






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:41







Jneven

















asked Nov 14 '18 at 21:25









JnevenJneven

118110




118110







  • 2





    What goes wrong? Do you get an error or the wrong answer?

    – Richard Telford
    Nov 14 '18 at 21:33






  • 2





    Instead of talking the log(factorial()), try using the lfactorial() function which is optimized for that particular operation. (though operates with base e)

    – MrFlick
    Nov 14 '18 at 21:41











  • yeah but when you put in: choose( n = 200 , k =50), it works.

    – Jneven
    Nov 14 '18 at 21:42











  • @MrFlick it doesn't matter that base e is much greater then the base of log10(log10))

    – Jneven
    Nov 14 '18 at 21:42






  • 2





    @Jneven. The problem is that you are taking the log "too late". If you push the logs into the function so log(factorial()/factorial()) becomes lfactorial()-lfactorial(), that's much easier for a computer to calculate.

    – MrFlick
    Nov 14 '18 at 21:46












  • 2





    What goes wrong? Do you get an error or the wrong answer?

    – Richard Telford
    Nov 14 '18 at 21:33






  • 2





    Instead of talking the log(factorial()), try using the lfactorial() function which is optimized for that particular operation. (though operates with base e)

    – MrFlick
    Nov 14 '18 at 21:41











  • yeah but when you put in: choose( n = 200 , k =50), it works.

    – Jneven
    Nov 14 '18 at 21:42











  • @MrFlick it doesn't matter that base e is much greater then the base of log10(log10))

    – Jneven
    Nov 14 '18 at 21:42






  • 2





    @Jneven. The problem is that you are taking the log "too late". If you push the logs into the function so log(factorial()/factorial()) becomes lfactorial()-lfactorial(), that's much easier for a computer to calculate.

    – MrFlick
    Nov 14 '18 at 21:46







2




2





What goes wrong? Do you get an error or the wrong answer?

– Richard Telford
Nov 14 '18 at 21:33





What goes wrong? Do you get an error or the wrong answer?

– Richard Telford
Nov 14 '18 at 21:33




2




2





Instead of talking the log(factorial()), try using the lfactorial() function which is optimized for that particular operation. (though operates with base e)

– MrFlick
Nov 14 '18 at 21:41





Instead of talking the log(factorial()), try using the lfactorial() function which is optimized for that particular operation. (though operates with base e)

– MrFlick
Nov 14 '18 at 21:41













yeah but when you put in: choose( n = 200 , k =50), it works.

– Jneven
Nov 14 '18 at 21:42





yeah but when you put in: choose( n = 200 , k =50), it works.

– Jneven
Nov 14 '18 at 21:42













@MrFlick it doesn't matter that base e is much greater then the base of log10(log10))

– Jneven
Nov 14 '18 at 21:42





@MrFlick it doesn't matter that base e is much greater then the base of log10(log10))

– Jneven
Nov 14 '18 at 21:42




2




2





@Jneven. The problem is that you are taking the log "too late". If you push the logs into the function so log(factorial()/factorial()) becomes lfactorial()-lfactorial(), that's much easier for a computer to calculate.

– MrFlick
Nov 14 '18 at 21:46





@Jneven. The problem is that you are taking the log "too late". If you push the logs into the function so log(factorial()/factorial()) becomes lfactorial()-lfactorial(), that's much easier for a computer to calculate.

– MrFlick
Nov 14 '18 at 21:46












2 Answers
2






active

oldest

votes


















2














This doesn't work for larger n because factorial(n) is too big:



> factorial(199)
[1] Inf
Warning message:
In factorial(199) : value out of range in 'gammafn'


This should return 200, but the computer only sees that you are trying to divide Inf by Inf:



> factorial(200)/factorial(199)
[1] NaN
Warning messages:
1: In factorial(200) : value out of range in 'gammafn'
2: In factorial(199) : value out of range in 'gammafn'


Obviously a lot of the multiplications in "n choose k" cancel out, so you'll need to avoid using regular factorial and only multiply the numbers that don't cancel out (?prod might be useful for you). Or (probably better) use the log version lfactorial to avoid running into numbers your computer can't store.



Edit: Added lfactorial recommendation from @MrFlick's comment






share|improve this answer




















  • 3





    Though exp(lfactorial(200)-lfactorial(199)) would work.

    – MrFlick
    Nov 14 '18 at 21:44











  • how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

    – Jneven
    Nov 14 '18 at 21:49


















1














Have a look at this {



a <- function(n, k) 
exp(lgamma(n+1) - lgamma(n - k + 1) - lgamma(k + 1) )






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53308960%2fr-error-when-trying-to-write-an-equivalent-function-n-choose-k%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This doesn't work for larger n because factorial(n) is too big:



    > factorial(199)
    [1] Inf
    Warning message:
    In factorial(199) : value out of range in 'gammafn'


    This should return 200, but the computer only sees that you are trying to divide Inf by Inf:



    > factorial(200)/factorial(199)
    [1] NaN
    Warning messages:
    1: In factorial(200) : value out of range in 'gammafn'
    2: In factorial(199) : value out of range in 'gammafn'


    Obviously a lot of the multiplications in "n choose k" cancel out, so you'll need to avoid using regular factorial and only multiply the numbers that don't cancel out (?prod might be useful for you). Or (probably better) use the log version lfactorial to avoid running into numbers your computer can't store.



    Edit: Added lfactorial recommendation from @MrFlick's comment






    share|improve this answer




















    • 3





      Though exp(lfactorial(200)-lfactorial(199)) would work.

      – MrFlick
      Nov 14 '18 at 21:44











    • how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

      – Jneven
      Nov 14 '18 at 21:49















    2














    This doesn't work for larger n because factorial(n) is too big:



    > factorial(199)
    [1] Inf
    Warning message:
    In factorial(199) : value out of range in 'gammafn'


    This should return 200, but the computer only sees that you are trying to divide Inf by Inf:



    > factorial(200)/factorial(199)
    [1] NaN
    Warning messages:
    1: In factorial(200) : value out of range in 'gammafn'
    2: In factorial(199) : value out of range in 'gammafn'


    Obviously a lot of the multiplications in "n choose k" cancel out, so you'll need to avoid using regular factorial and only multiply the numbers that don't cancel out (?prod might be useful for you). Or (probably better) use the log version lfactorial to avoid running into numbers your computer can't store.



    Edit: Added lfactorial recommendation from @MrFlick's comment






    share|improve this answer




















    • 3





      Though exp(lfactorial(200)-lfactorial(199)) would work.

      – MrFlick
      Nov 14 '18 at 21:44











    • how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

      – Jneven
      Nov 14 '18 at 21:49













    2












    2








    2







    This doesn't work for larger n because factorial(n) is too big:



    > factorial(199)
    [1] Inf
    Warning message:
    In factorial(199) : value out of range in 'gammafn'


    This should return 200, but the computer only sees that you are trying to divide Inf by Inf:



    > factorial(200)/factorial(199)
    [1] NaN
    Warning messages:
    1: In factorial(200) : value out of range in 'gammafn'
    2: In factorial(199) : value out of range in 'gammafn'


    Obviously a lot of the multiplications in "n choose k" cancel out, so you'll need to avoid using regular factorial and only multiply the numbers that don't cancel out (?prod might be useful for you). Or (probably better) use the log version lfactorial to avoid running into numbers your computer can't store.



    Edit: Added lfactorial recommendation from @MrFlick's comment






    share|improve this answer















    This doesn't work for larger n because factorial(n) is too big:



    > factorial(199)
    [1] Inf
    Warning message:
    In factorial(199) : value out of range in 'gammafn'


    This should return 200, but the computer only sees that you are trying to divide Inf by Inf:



    > factorial(200)/factorial(199)
    [1] NaN
    Warning messages:
    1: In factorial(200) : value out of range in 'gammafn'
    2: In factorial(199) : value out of range in 'gammafn'


    Obviously a lot of the multiplications in "n choose k" cancel out, so you'll need to avoid using regular factorial and only multiply the numbers that don't cancel out (?prod might be useful for you). Or (probably better) use the log version lfactorial to avoid running into numbers your computer can't store.



    Edit: Added lfactorial recommendation from @MrFlick's comment







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 21:59

























    answered Nov 14 '18 at 21:43









    Taiki SakaiTaiki Sakai

    35114




    35114







    • 3





      Though exp(lfactorial(200)-lfactorial(199)) would work.

      – MrFlick
      Nov 14 '18 at 21:44











    • how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

      – Jneven
      Nov 14 '18 at 21:49












    • 3





      Though exp(lfactorial(200)-lfactorial(199)) would work.

      – MrFlick
      Nov 14 '18 at 21:44











    • how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

      – Jneven
      Nov 14 '18 at 21:49







    3




    3





    Though exp(lfactorial(200)-lfactorial(199)) would work.

    – MrFlick
    Nov 14 '18 at 21:44





    Though exp(lfactorial(200)-lfactorial(199)) would work.

    – MrFlick
    Nov 14 '18 at 21:44













    how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

    – Jneven
    Nov 14 '18 at 21:49





    how would you recommend on defining this function for the general case? i mean, this is obviously no problem of definfg it specificly for 200 choose 50, but i wouldn't know how to do it for n choose k greater then some N and some K, suppose.

    – Jneven
    Nov 14 '18 at 21:49













    1














    Have a look at this {



    a <- function(n, k) 
    exp(lgamma(n+1) - lgamma(n - k + 1) - lgamma(k + 1) )






    share|improve this answer



























      1














      Have a look at this {



      a <- function(n, k) 
      exp(lgamma(n+1) - lgamma(n - k + 1) - lgamma(k + 1) )






      share|improve this answer

























        1












        1








        1







        Have a look at this {



        a <- function(n, k) 
        exp(lgamma(n+1) - lgamma(n - k + 1) - lgamma(k + 1) )






        share|improve this answer













        Have a look at this {



        a <- function(n, k) 
        exp(lgamma(n+1) - lgamma(n - k + 1) - lgamma(k + 1) )







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 10:42









        MikeMike

        614




        614



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • 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.

            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%2fstackoverflow.com%2fquestions%2f53308960%2fr-error-when-trying-to-write-an-equivalent-function-n-choose-k%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto