public class Recurrences { //Implements the basic laws of positive exponents ove
ID: 3835209 • Letter: P
Question
public class Recurrences {
//Implements the basic laws of positive exponents over the integers. Note: for this implementation, anything to the 0- power is 1.
For example: 0^0 = 1. But 0^1 = 0.
public static int power( int base, int exponent ) {
throw new UnsupportedOperationException();
}
//pmod stands for pseudo-mod. The real modulus function needs to deal with negative integers, but this one assumes positive integers only. The modulus operator, from our perspective, looks just like the % (remainder) operator in Java— except that you're implementing it recursively, and you will use it to implement the gcd function, which is defined imme- diately after this one. For example: pmod(4, 2) = 0, but pmod(4, 3) = 1, etc. You should look up "modulus" and/or review the definition of the "%" operator in Java for clarification.
public static int pmod( int a, int modulus ) {
throw new UnsupportedOperationException();
}
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Namburi Ramesh
*/
public class Recurrences {
public static int power( int base, int exponent ) {
if(exponent == 0){
return 1;
}else{
return base*power(base,exponent-1);
}
}
public static int pmod( int a, int modulus ) {
if(a==modulus){
return 0;
}else if(modulus>a){
return a;
}else{
return pmod(a-modulus,modulus);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.