Question

Write a c++ program where it computes n choose k by using the formula: n(n-1)(n-2)...(n-k+1)/k!. But...

Write a c++ program where it computes n choose k by using the formula: n(n-1)(n-2)...(n-k+1)/k!. But do not use algorithms where you can use only use integer arithmetic. How is this better than writing a program that uses the formula n!/k!(n-k)!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>

using namespace std;

#define ll long long

ll factorial(ll n)

{

                if(n==0||n==1) return 1;

                else return n*factorial(n-1);

}

ll nCr(ll n,ll k)

{

                ll prod=1;

                for(int i = n;i>=n-k+1;i--)

                {

                                prod*=i;

                }

                prod/=factorial(k);

}

int main()

{

                ll n,k;

                cout<<"Enter n, k"<<endl;

                cin>>n>>k;

                cout<<n<<"C"<<k<<" = "<<nCr(n,k)<<endl;

                return 0;

}

OUTPUT:

Enter n, k 10 5 10C5 252 Process exited after 1.745 seconds with return value e Press any key to continue .. .

BENEFIT OVER METHOD USING n!/k!(n-k)!

Computation of factorial(n) itself takes linear time in n.So, the number of computations are significantly reduced when we use n(n-1)(n-2)...(n-k+1)/k! to compute nCk over n!/k!(n-k)! because we have to calculate only k! while in the second method we had to calculate 3 factorials.

Add a comment
Know the answer?
Add Answer to:
Write a c++ program where it computes n choose k by using the formula: n(n-1)(n-2)...(n-k+1)/k!. But...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ARM assembly language Write a program "fibonacci.s" that computes the Nth Fibonacci number where N is...

    ARM assembly language Write a program "fibonacci.s" that computes the Nth Fibonacci number where N is not so large that overflow of integer arithmetic is a concern. When your assembly language program is called it should expect the value of N to be passed using register r0 and your program should return the Nth Fibonacci number in register r0. Please include comments as well. Do not just use the output generated by gcc -S

  • using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100....

    using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100. If n is out of range then print out an error message and ask for another input. This process repeats until a valid value for n is obtained. b) Inputs two 1D arrays of doubles A and B (of size n) from the keyboard. c) Inputs an integer k (from 1 to 3) from the keyboard. d) If k = 1 then it calculates...

  • C++: Write a program that computes and displays a full binomial expansion, such as (x+y)^n where...

    C++: Write a program that computes and displays a full binomial expansion, such as (x+y)^n where only n is asked for by the user.

  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

  • As you know, a numeric type in C is only an approximation to the mathematical entity...

    As you know, a numeric type in C is only an approximation to the mathematical entity due to bit-size limitations. The goal of this lab is to deal with real problems caused by such differences. Along the way, you will also get more practice using loops. Recall that n! (read n factorial) is defined to be (1)(2)(3)(4)...(n-1)(n). Many useful computer science problems require the computation of C(n,k) (read n choose k) which is defined as: n! / (k! * (n-k)!),...

  • please code in basic c++ program Write a program that uses the following formula: n (ax...

    please code in basic c++ program Write a program that uses the following formula: n (ax - ib) i=1 Your program will prompt the user for the number of iterations for the calculation. input values for n, x, a, b, and c. n must be a positive integer, but x, a, b, and c can be any real numbers. Then it will perform the calculation and output the result. Then, it will ask the user if they would like to...

  • Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to...

    Problem1. Write a C program, called cos.approx.c, that computes the approximate value of cos(x) according to its Tavlor series expansion: (-1)"r2n (2n)! x2 x4x6x8x10 cos(x)-Σ 1 This series produces the exact value of cos(x) for any real number x, but contains an infinite number of terms. Obviously, a computer program can compute only a finite number of terms. Thus, you will have to truncate the infinite series in (1). Your program should be able to do so in two different...

  • (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...

    (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • IN C++ Pleaseeeee Write a program to find the sum of the series 1/n+2/n+3/n+4/n+...+n/n, where 1...

    IN C++ Pleaseeeee Write a program to find the sum of the series 1/n+2/n+3/n+4/n+...+n/n, where 1 <= n <= 8 using a function. The user is asked to enter an integer value n. Then the program calls the appropriate function to calculate the sum of the series and then prints the sum to the screen.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT