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++
​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)
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.
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...
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.
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED Description: Mimic the code for N-queen problem (https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html), develop a program that generates all permutations for the set {1, 2, 3, 4, 5}. The output should contain all 5! = 120 permutations. Output Sample: P#1: 1 2 3 4 5 P#2: 1 2 3 5 4 P#3: 1 2 4 3 5 ... P#120: 5 4 3 2 1 Hint: - Thoroughly study the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
numUnique returns the number of unique values the list * Precondition: the list is not empty...
numUnique returns the number of unique values the list * Precondition: the list is not empty and is sorted from low to high. * { your solution can assume this is true } * * For full credit, your solution must go through the list exactly once. * Your solution may not call any other functions. * * Examples: * { abcdef }.numUnique() == 6 * { aaabcd }.numUnique() == 4 * { bccddee }.numUnique() == 4 * { abcddd...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT