java - Computing number of squares between two numbers, works only with small numbers, why? -


the problem find numbers of squares between 2 numbers.

the code below works small numbers fails huge numbers. how can correct this?

 import java.io.*;  import java.util.*;  import java.text.*;  import java.math.*;  import java.util.regex.*;   public class numofsqrs {   public static void main(string[] args) {      try{     bufferedreader br = new bufferedreader(new inputstreamreader(system.in));      string input;     int line = 0;     int testcases;     int numofsqrt = 0;           int j = 0;        while((input=br.readline())!=null){          if(line == 0){           testcases = integer.parseint(input);           line = line +1;         }         else{           string[] splitter = input.tostring().split(" ");            //here splitter gives 2 numbers, need find no of sqrs b/w these numbers eg 3 , 9             for(int = integer.parseint(splitter[0]); i<=integer.parseint(splitter[1]) ; i++){              string value = ""+math.sqrt(i);             string[] issqrt = value.tostring().split("\\.");             //system.out.println(""+issqrt[0] + "" + issqrt[1]);              //here lets if  'i' 4 (i.e 2.0) issqrt[0] = 2, issqrt[1] = 0 , if issqrt[1] != 1 obvious not perfect square              if(issqrt[1].length() == 1){               numofsqrt++;             }            }           system.out.println(""+numofsqrt);         }      numofsqrt = 0;     }       }catch(ioexception io){         io.printstacktrace();     }        } } 

your technique determining squares (converting string , splitting on dot) dodgy.

it's unnecessary - can use purely numeric approach in 1 line:

int low, high; // fill input int numofsqrt = (int) (math.floor(math.sqrt(high)) - math.ceil(math.sqrt(low)) + 1); 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -