Javatpoint Logo
Javatpoint Logo

Sastry Numbers in Java

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 Approach

The 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 String

Look 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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA