Need a program or software or the solution

Discussion in 'Programming' started by Prateekgupta, Jan 7, 2011.

  1. #1
    Double Squares
    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.

    Input
    You should first read an integer N, the number of test cases. The next N lines will contain N values of X.
    Constraints
    0 ≤ X ≤ 2147483647
    Output
    For each value of X, you should output the number of ways to write X as the sum of two squares.

    [​IMG]

    Now the main question is as follows , give me the program (C++ or PHP , java script or anything or even create a software if u can,
    20
    1022907856
    148208125
    2027929049
    510644794
    1000582589
    160225
    1148284322
    625058908
    1816371419
    65
    243061325
    1048039120
    4
    542915665
    415485223
    25
    77068225
    1
    2147483645
    4005625

    I want a software/program through which after giving the input question it will give the output
     
    Last edited: Jan 7, 2011
    Prateekgupta, Jan 7, 2011 IP
  2. IAreOwn

    IAreOwn Active Member

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    i just don't get why 10 = 32 + 12 , i'm no math genius but it doesn't make sense at all,... or did you mean 10 = 3^2 + 1^2 ?
     
    IAreOwn, Jan 7, 2011 IP
  3. anuradhan

    anuradhan Active Member

    Messages:
    371
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    65
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    Lol:
    Itz facebook Hackers cup qualification round question.
    now, i'm making a program for this.
    will post it within few hours :)
     
    anuradhan, Jan 7, 2011 IP
  4. Prateekgupta

    Prateekgupta Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    yes it's 3^2 :) corrected :) above

    and thanks plz make a program
    love u bro
     
    Prateekgupta, Jan 8, 2011 IP
  5. MsE

    MsE Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    As Seller:
    100% - 0
    As Buyer:
    100% - 1
    #5
    If you've already downloaded your input file, the time will have expired, Prateek. You only get 6 minutes from downloading the input file to upload the answer.

    Ruby solution is this:

    
    #!/usr/bin/env ruby
    # Double Squares, Facebook Hacker Cup 2011
    # http://www.kennetham.com
    
    $file = ARGV[0]
    unless $file && File.exist?($file)
      puts "error: invalid file!"
      exit
    end
    
    $temp = []
    $contents = IO.read $file
    $_contents = $contents.split(/\n+/)
    $_contents.delete_at(0)
    $temp.push $_contents
    
    def find_match(square)
      $x = square.to_i
      $x <= 2147483647
    #  return 0 if $x == 0.floor
    #  puts "Possible matches of #{$x}:\r\n"
    
      halfroot = Math.sqrt($x / 2)
      count = 0
    
      0.upto(halfroot) do |i|
        i_sq = (i * i)
        r = Math.sqrt($x - i_sq)
        if ((r - r.to_i.floor) < 0.000001)
    #      puts "#{$x} = #{i.to_i}^2 + #{r.to_i}^2\r\n"
          count += 1
        end
      end
      return count
    end
    
    $_contents.each do |j|
      puts find_match(j.to_i)
    end
    
    Code (markup):
     
    MsE, Jan 8, 2011 IP
  6. MsE

    MsE Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    As Seller:
    100% - 0
    As Buyer:
    100% - 1
    #6
    Even quicker:
    guzloo.com/dsquareinput.php

    Upload your input file there - the source is also available.
     
    MsE, Jan 8, 2011 IP