You must implement three functions from the “Required Functions,” . In some case
ID: 3604989 • Letter: Y
Question
You must implement three functions from the “Required Functions,” . In some cases, these functions are already defined in Racket (DrRacket). To avoid confusion, you can name yours differently, for example “myreverse” for “reverse”. Or you can just override the built-in definition. You may use other builtin functions, such as if, cond, car (first), cdr (rest), and cons in your functions. But you may not, of course, use a builtin function in your answer for that same function. (must be in real code)
You must implement all three of these mathematical functions on integers.
1. Check if a number is perfect: a number is perfect if the sum of its factors other than itself is equal to itself. (perfect? 5) => #f (perfect? 6) => #t
2. Check if a number is abundant: an abundant number’s sum of factors other than itself is greater than the number. (abundant? 5) => #f (abundant? 12) => #t
3. Check if a number is deficient: a deficient number’s sum of factors is less than itself. (deficient? 5) => #t (deficient? 12) => #f
Explanation / Answer
Please find my solution in java for the above problem.Let me know if you have any doubts.Thanks.
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number : ");
int x=sc.nextInt();
System.out.println("Result of checkPerfect method for number "+ x+" is "+checkPerfect(x));
System.out.println("Result of checkAbundant method for number "+ x+" is "+checkAbundant(x));
System.out.println("Result of checkDeficient method for number "+ x+" is "+checkDeficient(x));
}
static boolean checkPerfect(int n){
// To store sum of divisors
long sum = 1;
// Find all divisors and add them
for (long i=2; i*i<=n; i++){
if (n%i==0)
sum = sum + i + n/i;
}
// If sum of divisors is equal to
// n, then n is a perfect number
if (sum == n && n != 1)
return true;
return false;
}
static boolean checkAbundant(int n){
// To store sum of divisors
long sum = 1;
// Find all divisors and add them
for (long i=2; i*i<=n; i++){
if (n%i==0)
sum = sum + i + n/i;
}
// If sum of divisors is equal to
// n, then n is a perfect number
if (sum > n)
return true;
return false;
}
static boolean checkDeficient(int n){
// To store sum of divisors
long sum = 1;
// Find all divisors and add them
for (long i=2; i*i<=n; i++){
if (n%i==0)
sum = sum + i + n/i;
}
// If sum of divisors is equal to
// n, then n is a perfect number
if (sum < n)
return true;
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.