Sastry Numbers in Java10 Sept 2024 | 4 min read A number num is said to be a Sastry Number if the number num appended with the number num + 1 gives the perfect square. Example 1: Input int num = 183 Output 183 is a Sastry Number. Explanation: If we append the number 183 with the number 184 (183 + 1), we get 183184. The square root of the number 183184 is 428, which is a perfect square. Hence, the number 183 is the Sastry number. Example 2: Input num = 4 Output 4 is not a Sastry Number. Explanation: If we append the number 4 with the number 5 (4 + 1), we get 45. The square root of the number 45 is 6.7082039, which is not a perfect square. Hence, 4 is not a Sastry number. Example 3: Input num = 328 Output 328 is a Sastry Number. Explanation: If we append the number 328 with the number 329 (328 + 1), we get 328329. The square root of the number 328329 is 573, which is a perfect square. Hence, 328 is a Sastry number. Simple ApproachThe simple approach is to run a loop (from 1 to e) and compute the HCF for each pair (with e). HCF can be found using a loop, which runs from min(a, b) to 1 (where a and b are numbers). If the value pointed by the loop variable divides both the number completely, then that value is our HCF, and we can terminate the loop. Filename: SastryNumbers.java Output: The Sastry numbers from 1 to 20000 are: 183 328 528 715 6099 13224 Complexity Analysis: The time complexity of the program is O(dig), where dig is the total number of digits present in the input number. The space complexity of the program is constant, i.e. O(1). Approach: Using StringLook at the following steps that are required to check the Sastry number with the help of the strings. Step 1: Take a number and store it in a variable (num variable in our case). Step 2: Take another variable (temp variable in our case). Assign the value (num + 1) in the variable temp2. Step 3: Convert the values stored in the variables num and temp into strings. Step 4: Concatenate the strings and store it in a variable (str in our case). Step 5: Convert the value present in the string str into an integer variable (ans in our case). Step 6: Check whether the number present in the variable ans is a perfect square or not. If the number is the perfect square, then the input number (value of num) is the Sastry number; otherwise, not. Filename: SastryNumbers1.java Output: The Sastry numbers from 1 to 20000 are: 183 328 528 715 6099 13224 Complexity Analysis: The time, as well as space complexity of the program is the same as the previous program. Next TopicSum of LCM in Java |
Convert a Generic Tree (N-ary Tree) to Binary Tree in Java
The conversion of N-ary trees to binary tree serves as a standard computer science operation for reducing hierarchy complexity while maintaining hierarchical structures. An N-ary tree allows each node to have multiple children, making it complex to manage using standard tree structures. To efficiently represent an N-ary...
5 min read
Bus Reservation System Project in Java
The Bus Reservation System serves as a basic console application written in Java where users can view buses for booking along with reserving seats and managing active reservations. The system handles seat management effectively to provide users with an unbroken booking experience. The project implements object-oriented...
8 min read
Identifiers in Java
In Java programming, identifiers help make different elements inside a program easier to recognize and use by acting as symbolic names for them. Numerous entities, including classes, variables, methods, packages, constants, and more, can be represented by these identifiers. Developers can enhance the readability and...
6 min read
Java Parallel Stream Example
The parallel stream was introduced in Java 8 or later versions. It is a part of functional programming. Using the feature of parallel stream, we can use multiple cores of the processor. Any stream in Java can be easily transformed from sequential to parallel stream. In...
4 min read
Find The Index of An Array Element in Java
In this tutorial, we will see how one finds the index of an array element in Java. To avoid confusion, we will assume that all the elements of the array are unique. In other words, no element will occur more than once. In the input, an...
9 min read
Java Program to Implement Two Stacks in an Array
In this section, we will create a Java program that implemnts two stacks in an array. The meaning of two stack means both the stacks should use the same array for keeping the elements. The following are a few methods that must be implemented by those two...
3 min read
Difference Between Static and Non-Static in Java
In Java, static and non-static members differ in how they are stored, accessed, and used within a class. Static Members in Java Static members refer to class-level variables or methods, meaning they belong to the class itself rather than any individual object instantiated from it. It makes...
8 min read
DecimalFormat getMultiplier() method in Java
One of the built-in methods in java.text is getMultiplier(). To get the multiplier to be used in many formats, such as percentage, percentile, etc., the Java class DecimalFomrat is utilized. Syntax: public int getMultiplier() Parameters: No parameters are accepted by this method. Return Value: The multiplier value that can be...
2 min read
How to build a Web Application Using Java
Java is one of the most used programming languages for developing dynamic web applications. A web application is computer software that utilizes the web browser and technologies to perform tasks over the internet. A web application is deployed on a web server. Java provides some technologies like...
8 min read
Java Pop
Java programming supports different data structures like array, linked list, stack, queue, etc. Each data structure has operations such as insertion, deletion, searching an element. And to implement these operations Java programming provides built in classes and methods. In this section, we will understand the pop...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India