Note that there are some explanatory texts on larger screens.

plurals
  1. PODouble Squares (Facebook Hacker Cup 2011) in Java
    primarykey
    data
    text
    <p><strong>So it works fine, two major changes that were needed is only checking up to the square root of x (required also flipping my check case from checkign for remainder to checking exponentiation). The other change is that using a double was obviously reckless as the numbers will go way above the maximum double.</strong></p> <p>This is a challenge from CodeEval, I suppose first asked by Facebook. This is what my brain spit out immediately. It passes all hand driven test cases (e.g. 10-> 1, 25->2, 3-> 0). I haven't seen any other solutions because I want to see how I did with my own thoughts first. If I'm way off base and this will never work, I'd appreciate someone just saying so :P. If this will never work and I have to figure out a new way, I'll spend more time dwelling on this one.</p> <p>I don't immediately see any case it won't satisfy..but that's not the issue. I was never very good at calculating run time complexity, but I think I've got way too much nesting going on.</p> <p>I thought by checking from the right and left (This is a little more clear in the code), I would seriously decrease the run time. I'm not sure if that's just not doing is as effectively as I thought, or the other loops are still too much...or both.</p> <p>Any thoughts? Is this salvageable or should I scrap it and think about it in a new way?</p> <p>Question and code follow. Thanks for looking :-)</p> <blockquote> <p>Credits: This challenge appeared in the Facebook Hacker Cup 2011.<br> A double-square number is an integer X which can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10 = 3^2 + 1^2. Your task in this problem is, given X, determine the number of ways in which it can be written as the sum of two squares. For example, 10 can only be written as 3^2 + 1^2 (we don't count 1^2 + 3^2 as being different). On the other hand, 25 can be written as 5^2 + 0^2 or as 4^2 + 3^2.<br> NOTE: Do NOT attempt a brute force approach. It will not work. The following constraints hold:<br> 0 &lt;= X &lt;= 2147483647<br> 1 &lt;= N &lt;= 100</p> </blockquote> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class DoubleSquares { public static void main(String[] args) throws IOException { File file = new File(args[0]); BufferedReader in = new BufferedReader(new FileReader(file)); String line; while ((line = in.readLine()) != null) { int total = 0; String[] lineArray = line.split("\\s"); if (lineArray.length &gt; 0) { double x = (double) Integer.parseInt(lineArray[0]); if (x == 0){total++;} //special case if input is 0 double sLeft = 0.00; //left boundary to indicate where to stop (so we dont repeat e.g. 4+1 and 1+4) for(double sRight = x; sRight &gt; sLeft; sRight--){ if (Math.sqrt(sRight) % 1 == 0){//has no remainder then it's a candidate.. double needed = x-sRight; if (Math.sqrt(needed) % 1 == 0){//check of solution to what makes sRight == x is a perfect square. total++; //increment if so. } sLeft = needed; } } } System.out.println(total); } } } </code></pre> <p>To be a little more clear, this seems to work just fine, but when I submit to the CodeEval auto grader, it terminates after 10 seconds of running. Too slow or stuck with some large input no doubt.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload