(IN JAVA) Do the following 5 methods without importing extra functions besides t
ID: 3748834 • Letter: #
Question
(IN JAVA) Do the following 5 methods without importing extra functions besides the default (i.e. System, Math)
METHOD 1: public static boolean isPrime(int i) This method should return true if and only if the input i is a positive prime number. A prime is a number which son y divs b a primes) METHOD 2: public static int numPrimes(int lower, int upper) This method will return the total number of primes in the range between lower and upper, including lower and upper themselves. Hint: it's possible to use isPrime(int i) as part of the computation. METHOD 3: public static int factorial(int n) Find the factorial of n, which is given by n! 1 2.(n-1)*n. The factorial of zero is a special case which is equal to 1 METHOD 4: public static double sumPower(int n, double p) Sums the series 1P+2P+n-1)PnP. METHOD 5: public static int boundedSum(int] list, int low, int high) Sums up the elements in list, but only including the elements x in list for which low Sx S high and e f owe ert nurnberso un lent consideredExplanation / Answer
//java functions
public class Function {
public static void main(String args[]) {
int arr[]= {34,67,23,1,2,90,34};
System.out.println("prime numbers between 1 to 30");
numPrimes(1,30);
System.out.println(" 10! = "+factorial(10));
System.out.println(" powersum(2 , 10) : "+sumPower(2,10));
System.out.println(" boundedsum(arr , 2, 5) : "+boundedSum(arr ,2, 5));
}
public static boolean isPrime(int n) {
if(n==1)return false;
for(int i=2 ;i*i<=n;i++)
if(n%i==0)return false;
return true;
}
public static void numPrimes(int lower ,int upper) {
for(int i=lower ;i<=upper;i++)
if(isPrime(i)==true)System.out.print(i+" ");
}
public static int factorial(int n) {
if(n==0)return 1;
else return n*factorial(n-1);
}
public static double sumPower(int n ,double p) {
if(n==0)return 0;
else return Math.pow(n, p)+ sumPower(n-1,p);
}
public static int boundedSum(int []list ,int low,int high) {
int sum=0;
for(int i=low ;i<=high;i++)
sum+=list[i];
return sum;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.