Skip to main content
deleted 18 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Here is the Code. thisThis 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;
        }
        }
}

Here is the Code. 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;
        }
        }
}

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;
        }
        }
}
edited title
Link
Phrancis
  • 20.4k
  • 6
  • 68
  • 154

Suggest me some improvements so that i could reduce Calculating the execution time less than 4 sec for any testing valueperfect squares between two given numbers

Source Link
Robin
  • 61
  • 1
  • 2

Suggest me some improvements so that i could reduce the execution time less than 4 sec for any testing value

Here is the Code. 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;
        }
        }
}