patternjavaMinor
Calculating the perfect squares between two given numbers
Viewed 0 times
thenumbersperfectsquarescalculatingbetweentwogiven
Problem
This code is for calculating the perfect squares between two given numbers. User can add the interval as well like for how many intervals he want to check the perfect squares. For example if user enter 4, it means there are 4 different intervals and after that he will insert the tow number for interval 1, similarly two numbers for interval 2 and respectively 3 and 4. I just want to reduce the execution time
import java.io.*;
import java.util.*;
class node{
private int a;
private int b;
node(int a, int b){
this.a = a;
this.b = b;
}
int getA(){return a;}
int getB(){return b;}
}
public class Solution {
public static void main(String[] args) {
int size;
Scanner S1 = new Scanner(System.in);
size = S1.nextInt();
node a[] = new node[size];
for(int i = 0; i<a.length; i++){
/*int number = S1.nextInt();
int number2 = S1.nextInt();*/
a[i] = new node(S1.nextInt(), S1.nextInt());
}
int total = 0;
int sqrt = 0;
for(int i = 0; i<a.length; i++){
for(int j = a[i].getA(); j<=a[i].getB(); j++){
sqrt = (int) Math.sqrt(j);
if(sqrt*sqrt == j) {
total++;
}
}
System.out.println(total);
total = 0;
}
}
}Solution
First things first. You have to organize your code and group functionality into methods. So I would suggest creating a method
Let's enhance your algorithm. The key idea is to get the square root of the next perfect square number. This can be achieved by ceil the sqrt of the starting point:
This variable is the basis for our calculations. All we have to do now is to check if the
Putting everything together looks like this:
Let's check the algorithm:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
int[] getPerfectSquaresBetween(int start, int end).Let's enhance your algorithm. The key idea is to get the square root of the next perfect square number. This can be achieved by ceil the sqrt of the starting point:
int candidate = (int) Math.ceil(Math.sqrt(start));This variable is the basis for our calculations. All we have to do now is to check if the
candidate squared is in the given range.int square;
while ((square = candidate * candidate) < end) {
//...
candidate++;
}Putting everything together looks like this:
public static int[] getPerfectSquaresBetween(int start, int end) {
if (start > end || start < 0) {
throw new IllegalArgumentException();
}
int[] perfectSquares = new int[end - start];
int nr = 0;
int candidate = (int) Math.ceil(Math.sqrt(start));
int square;
while ((square = candidate * candidate) < end) {
perfectSquares[nr++] = square;
candidate++;
}
return Arrays.copyOf(perfectSquares, nr);
}Let's check the algorithm:
getPerfectSquaresBetween(0, 101) (which completes in microseconds)[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Code Snippets
int candidate = (int) Math.ceil(Math.sqrt(start));int square;
while ((square = candidate * candidate) < end) {
//...
candidate++;
}public static int[] getPerfectSquaresBetween(int start, int end) {
if (start > end || start < 0) {
throw new IllegalArgumentException();
}
int[] perfectSquares = new int[end - start];
int nr = 0;
int candidate = (int) Math.ceil(Math.sqrt(start));
int square;
while ((square = candidate * candidate) < end) {
perfectSquares[nr++] = square;
candidate++;
}
return Arrays.copyOf(perfectSquares, nr);
}Context
StackExchange Code Review Q#124952, answer score: 5
Revisions (0)
No revisions yet.