Programming Example
Check if a Number is a Fibonacci Number in Range using Java
range [1, 100] 55
55 is a Fibonacci number within the range [1, 100]. Fibonacci numbers within the range [1, 100]: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
import java.util.*;
public class FibonacciInRangeChecker {
// Function to check if a number is a perfect square
public static boolean isPerfectSquare(int x) {
int s = (int) Math.sqrt(x);
return s * s == x;
}
// Function to check if a number is a Fibonacci number
public static boolean isFibonacci(int number) {
// A number is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both is a perfect square
return isPerfectSquare(5 * number * number + 4) || isPerfectSquare(5 * number * number - 4);
}
// Function to find all Fibonacci numbers in a given range
public static List<Integer> findFibonacciNumbersInRange(int start, int end) {
List<Integer> fibonacciNumbers = new ArrayList<>();
int currentFibonacci = 0;
int n1 = 0, n2 = 1;
while (currentFibonacci <= end) {
if (currentFibonacci >= start) {
fibonacciNumbers.add(currentFibonacci);
}
// Calculate next Fibonacci number
currentFibonacci = n1 + n2;
n1 = n2;
n2 = currentFibonacci;
}
return fibonacciNumbers;
}
public static void main(String[] args) {
int start = 1; // Example start of range
int end = 100; // Example end of range
List<Integer> fibonacciNumbersInRange = findFibonacciNumbersInRange(start, end);
// Check if a specific number is Fibonacci and within the range
int numberToCheck = 55; // Example number to check
if (fibonacciNumbersInRange.contains(numberToCheck) && isFibonacci(numberToCheck)) {
System.out.println(numberToCheck + " is a Fibonacci number within the range [" + start + ", " + end + "].");
} else {
System.out.println(numberToCheck + " is not a Fibonacci number within the range [" + start + ", " + end + "].");
}
// Print all Fibonacci numbers found within the range
System.out.println("Fibonacci numbers within the range [" + start + ", " + end + "]: " + fibonacciNumbersInRange);
}
}
55 is a Fibonacci number within the range [1, 100].
Fibonacci numbers within the range [1, 100]: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
isPerfectSquare(int x): Helper function to check if x is a perfect square.isFibonacci(int number): Function to check if number is a Fibonacci number using the mathematical property discussed earlier.findFibonacciNumbersInRange(int start, int end): Function to find all Fibonacci numbers within the specified range [start, end]. It iterates through Fibonacci numbers until the current Fibonacci number exceeds end, storing those that fall within the range.main(String[] args): In the main method, specify the start and end of the range. It calculates all Fibonacci numbers in the range and checks if a specific numberToCheck is both a Fibonacci number and within the range.First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.