-1

For calculating nCr [ i.e. n ! / ( r ! * ( n-r )! ) ], written the below code.

Perl code:

($i,$j)=(1000,100);
print fact($i)/(fact($j)*(fact($i-$j)));
sub fact{
    return 1 if $_[0]<=1;
    return $_[0]*fact($_[0]-1);
}

which is giving output as "-NaN" but,

same logic code in python gives correct result.

Python code:

def fact(x):
    if x <= 1:
        return 1
    return x*f(x-1)
v,y = 1000,100
print fact(v)/(fact(y)*fact(v-y))

Kindly let me know what changes I have to made in perl code to make it work for bigger numbers.(And also I tried to use "bigint" too, But didnot worked)

Edit:

Thank you all for the response.

Sorry that, I have missed ; and -1.

I think bigint is dependent on machine configuration

http://www.perlmonks.org/?node_id=906757

karsas
  • 1,231
  • 4
  • 12
  • 24
  • Your code does not print `-NaN`. It doesn't even compile – ikegami Jun 19 '15 at 02:45
  • 1
    Your recursion in Perl looks infinite; you forgot to subtract 1 before recursing (unless there's some extremely subtle way of subtracting 1 in your code that I've missed). – Jonathan Leffler Jun 19 '15 at 02:52
  • @Jonathan Leffler, Yeah, like I said, he didn't post the code he used, and he said he did everything right in the code he used. What can we do? Voted to close since he didn't demonstrate his problem. – ikegami Jun 19 '15 at 02:54
  • @ikegami: since there is a reasonable duplicate for the surface (stated) problem, we don't have to nitpick the details of what code is supplied too much. In other contexts, where there isn't a good duplicate, then closure as unclear or other related reasons might be more reasonable. If the OP comes back and complains and adds his non-working code using **"bigint"** (whatever that means precisely) then we reassess the situation. – Jonathan Leffler Jun 19 '15 at 02:56
  • @ikegami: That's the way it goes; if you flag for closure and the closure is acted on, you get credited with the majority decision (or gold badge decision), even if you don't agree. Take it up on MSO if you want — I'm not sure whether it has been discussed before, but it probably has. – Jonathan Leffler Jun 19 '15 at 02:58
  • 1
    If you were doing this by hand, you would never calculate all those factorials...you would cancel out what you could first, then multiply out the remaining terms. – Jim Lewis Jun 19 '15 at 03:13

2 Answers2

0

I don't know what you did, but use bigint works fine

As ikegami said, the code that you posted doesn't even compile, so you're making it as hard as you can to help you

use strict;
use warnings;

use bigint;

my ($i, $j) = (1000, 100);

no warnings 'recursion';

print fact($i) / ( fact($j) * fact($i-$j) ), "\n";

sub fact{
    $_[0] <= 1 ? 1 : $_[0] * fact($_[0]-1);
}

output

63850511926305130236698511142022274281262900693853331776286816221524376994750901948920974351797699894319420811933446197797592213357065053890

Why would you want that number, anyway?

Borodin
  • 126,100
  • 9
  • 70
  • 144
0

try this I think that will resolve the problem

use bignum;
($i,$j)=(1000,100);
print fact($i)/(fact($j)*(fact($i-$j)));
sub fact{
    return 1 if $_[0]<=1;
    return $_[0]*fact($_[0]-1);
}
anuj rana
  • 1,092
  • 1
  • 7
  • 2