Examples
- Java Program to Count Number of Digits in Number (Integer Length)
- Java Program to Reverse a String (4 Methods)
- How to Reverse an Array in Java? Array Reverse Program
- How to Find Power of a Number in Java? 4 Programs
- How to Find Factors of a Number in Java? Factors Program
- Check Disarium Number in Java (3 Easy Programs)
- Find Keith Number in Java (Easy Programs)
- Happy Number in Java (Easy Programs With Logic)
- Find Sunny Number in Java (Easy Programs)
Find Sunny Number in Java (Easy Programs)
In mathematics, certain numbers have unique properties and characteristics that make them stand out from the rest. One such class of numbers is known as "Sunny Numbers."
Sunny numbers are not only fascinating from a mathematical perspective but also find practical applications in various fields such as computer science and number theory. In this tutorial, we will learn how to find Sunny Number in Java.
But before that, let’s first understand what exactly is the meaning of sunny number.
What is Sunny Number in Java?
A sunny number is a type of number that is related to square numbers and perfect squares.
Specifically, a sunny number is a number such that when you add 1 to it, the result is a perfect square.
In other words, if N is a sunny number, then (N + 1) must be a perfect square.
Mathematically, a number N is considered a sunny number if:
N + 1 = M²
Here, N is the sunny number, M is the positive integer such that M² is the perfect square, and N + 1 equals that perfect square.
For example, let's find a sunny number:
If N + 1 = 5² (25), then N = 24.
So, 24 is a sunny number because when you add 1 to it, you get a perfect square: 24 + 1 = 25, which is 5².
Sunny numbers can be used in programming, including Java, for various purposes such as solving mathematical problems or generating sequences of numbers with specific properties.
Concepts to Learn:
Find Sunny Number in Java Using Brute Force
Here's how to find Sunny Numbers in Java using the brute force method:
Code
public class SunnyNumber {
public static boolean isSunny(int num) {
int squareRoot = (int) Math.sqrt(num + 1);
return squareRoot * squareRoot == num + 1;
}
public static void main(String[] args) {
int n = 20;
System.out.println("Sunny Numbers up to " + n + ":");
for (int i = 1; i <= n; i++) {
if (isSunny(i)) {
System.out.print(i + " ");
}
}
}
}
Output
Sunny Numbers up to 20:
3 8 15
Explanation
In this example, we use the brute force method to find Sunny Numbers up to a given limit, which is 20 in this case.
-
We create an isSunny function to check if a number is sunny or not. Inside this function, we calculate the square root of num + 1 and check if it is a perfect square. If it is, the number is sunny.
-
In the main method, we iterate through numbers from 1 to n (inclusive) and call the isSunny function for each number. If a number is found to be sunny, it is printed.
-
The output shows the Sunny Numbers up to 20, which are 3, 8, and 15.
This brute force method involves checking each number individually, making it straightforward but not the most efficient approach for large numbers.
Sunny Number in Java Using Maths Formula
Here's a Java program for Sunny Numbers using the mathematical formula approach:
Code
public class SunnyNumber {
public static boolean isSunny(int num) {
int nextNum = num + 1;
double sqrt = Math.sqrt(nextNum);
return sqrt % 1 == 0;
}
public static void main(String[] args) {
int n = 20;
System.out.println("Sunny Numbers up to " + n + ":");
for (int i = 1; i <= n; i++) {
if (isSunny(i)) {
System.out.print(i + " ");
}
}
}
}
Output
Sunny Numbers up to 20:
3 8 15
Explanation
In this example, we use the mathematical formula approach to find Sunny Numbers up to a given limit, which is 20 in this case.
-
We create an isSunny function that takes an integer num. Inside this function, we calculate nextNum as num + 1 and then compute the square root (sqrt) of nextNum. If the square root modulo 1 is equal to 0 (i.e., it is a perfect square), the number is sunny.
-
In the main method, we iterate through numbers from 1 to n (inclusive) and call the isSunny function for each number. If a number is found to be sunny, it is printed.
-
The output shows the Sunny Numbers up to 20, which are 3, 8, and 15.
This method uses a mathematical formula to check for Sunny Numbers, which is more efficient than the brute force method for large numbers as it avoids iterating through each number individually.
Learn Next: