0% found this document useful (0 votes)
7 views

Code

code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Code

code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class PrimeChecker {

// Method to check if a number is prime


public static boolean isPrime(int number) {
// Handle edge cases
if (number <= 1) {
return false;
}
if (number == 2) {
return true;
}
if (number % 2 == 0) {
return false;
}

// Check for factors from 3 to the square root of the number


for (int i = 3; i <= Math.sqrt(number); i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}

public class PerfectSquareChecker {

// Method to check if a number is a perfect square


public static boolean isPerfectSquare(int number) {
if (number < 0) {
return false; // Negative numbers cannot be perfect squares
}

int sqrt = (int) Math.sqrt(number);


return (sqrt * sqrt == number);
}

public static boolean isPerfectNumber(int number) {


if (number <= 1) {
return false; // There are no perfect numbers less than or equal to 1
}

int sum = 1; // 1 is a divisor for all integers greater than 1

// Find all divisors and add them


for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
if (i == (number / i)) {
sum += i;
} else {
sum += i + (number / i);
}
}
}

// Check if the sum of divisors is equal to the number


return sum == number;
}

public static int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

public static int lcm(int a, int b) {


if (a == 0 || b == 0) {
return 0; // LCM of 0 with any number is 0
}
return Math.abs(a * b) / gcd(a, b);
}
//fibo
public static boolean isFibonacci(int number) {
if (number < 0) {
return false; // Negative numbers are not in the Fibonacci sequence
}
// A number is Fibonacci if and only if one or both of (5*n^2 + 4) or
(5*n^2 - 4) is a perfect square
return isPerfectSquare(5 * number * number + 4) || isPerfectSquare(5 *
number * number - 4);
}

// Helper method to check if a number is a perfect square


private static boolean isPerfectSquare(int number) {
int sqrt = (int) Math.sqrt(number);
return (sqrt * sqrt == number);
}

You might also like