Hello everyone, I have task "Write a program that calculates N!/K! for given N and K (1<N<K)."
I want to ask is this the right solution ?

``````using System;

class NandKFactorials
{
public static double fact(double x)
{

double result = 1;
for (int i = 1; i <= x; i++)
result *= i;
return result;
}

public static double calculating(double n, double k)
{
return fact(n) / fact(k);
}

static void Main()
{

Console.WriteLine(calculating(5,6));

}
}
``````

This is probably not the solution your instructors are looking for. I imagine your instructors are looking for a solution where you caclulate n!/k! without calculating n!.

Note that if you expand n!/k!, it will look like this:

``````1 * 2 * ... * n
--------------------------
1 …``````

## All 3 Replies

This is probably not the solution your instructors are looking for. I imagine your instructors are looking for a solution where you caclulate n!/k! without calculating n!.

Note that if you expand n!/k!, it will look like this:

``````1 * 2 * ... * n
--------------------------
1 * 2 * ... * n *  ... * k
``````

See how the `1 * 2 * ... * n` part repeats in both the numerator and the denominator? If you were to calculate this by hand, you wouldn't calculate that part, right? You'd just cancel it out and only calculate the part that doesn't repeat. I imagine your program is suppsed to do the same.

That will not only lead to a better running time, but also to less rounding errors for large n and k.

liked sepp2k responce ...

the statement you have says "program"

"Write a program that calculates N!/K! for given N and K (1<N<K)."

so i think it implies more then just the simple subroutines that you have that are one method (could be enough for C-) and but don't show enought i think ...

but i was looking at it differently and have noted some missing points

first you have not proved the values are within specs asked for

for given N and K (1<N<K)."

second your use of double seems you automatic use it but may not understand its implications .... because you failed to make the value i a double

which means that it will cause error before it can reach the N or K value if they are larger then the interger max size

and what if the values of N & K being larger then the double max size

also your test sample is simple but i hope you expand to show the specs ...

ie what if you did (6,5) what would happen ... or (-5 * 10 ** 4 , 5 * 10 ** 100)

its the black box testing that will show that you understand the proccess and not just clone book examples ...

our main problem will be that we don't know at what level of math & programing you are doing this at ... there are many ways to approch this ...

double result = 1;
for (int i = 1; i <= x; i++)
result *= i;
return result;

``````if ( x > 1  )
return x * fact( x - 1 )
else
return double(1)
``````

first you have not proved the values are within specs asked for

Usually if the assignment says that a variable will be in a given range, I'd think that means that you can assume it will be, not that you have to verify it.

second your use of double seems you automatic use it but don't understand its implications .... because you failed to make the value i a doubl

I think it's safe to assume that the values given for n and k (and thus the possible values for i) will be within the range of an int (if they're not, you'll be waiting a long time for the function to return) while the result might not be.

``````if ( x > 1 )
return x * fact( x - 1 )
else
return double(1)
``````

Please don't recommend using recursion to calculate the factorial. It's less efficient than using a loop and can cause a stack overflow for large arguments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.