Question

In: Computer Science

Write a recursive methodcalledpermutationthataccepts two integersnandras parameters and returns the number of unique permutations ofritems from...

Write a recursive methodcalledpermutationthataccepts two integersnandras parameters and returns the number of unique permutations ofritems from a group ofnitems. For given values ofnandr, this valuepermutation(n,r)can be computed as follows:

permutation(n,r)= n!/ (n-r)!

hint: permutation(6, 4)should return360. It may be helpful to note thatpermut(5, 3)returns60, or 360/6.

Solutions

Expert Solution

The recursive function to find permutation can be defined as follows:

int permutation(int n, int r)
{
    if(r == 1) 
           return n; //stops recursion
    return n*permutation(n-1, r-1);//recursive call
}

The required program and the corresponding output are as follows:

import java.io.*;
import java.lang.*;

class Testing{  
   
        static int permutation(int n, int r)
        {
                if(r == 1) 
                   return n; //stops recursion
                return n*permutation(n-1, r-1);//recursive call
        }    
  
    public static void main(String args[]){  
         
                int nv,rv,p; 
                try{
                DataInputStream dis = new DataInputStream(System.in);
                System.out.println("Enter value for n : ");
                nv=Integer.parseInt(dis.readLine());//reading n value
                System.out.println("Enter value for r : ");
                rv=Integer.parseInt(dis.readLine());//reading r value
                p=permutation(nv,rv);//function call
                System.out.println("Nunber of unique permutations : " +p);
        }
     catch(Exception e){        System.out.println("Error : " +e);      }
  
   }  
}

Output:


Related Solutions

Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
In Java Write a method called findMax that accepts three floating-point number as parameters and returns...
In Java Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method)
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int)...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int) of the longest String in a List of Strings. For each recursive call, remove the first String from the list and return the greater of the length of the String you removed or the result of calling the method on the remainder of the list. The base case should be that the size of the list is 0. Write a driver to verify that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT