Check if a Number is a Fibonacci Number in Range using Java
Mathematics for Programming Numbers (Article) Numbers (Program)
23
Given Input:
range [1, 100]
55
Expected Output:
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]
Program:
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 findFibonacciNumbersInRange(int start, int end) {
List 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 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);
}
}
Output:
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]
Explanation:
-
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.
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.
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]
Program:
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 findFibonacciNumbersInRange(int start, int end) { List 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 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); } }
Output:
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]
Explanation:
-
isPerfectSquare(int x)
: Helper function to check ifx
is a perfect square. -
isFibonacci(int number)
: Function to check ifnumber
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 exceedsend
, storing those that fall within the range. -
main(String[] args)
: In themain
method, specify thestart
andend
of the range. It calculates all Fibonacci numbers in the range and checks if a specificnumberToCheck
is both a Fibonacci number and within the range.
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.