Facebook Hacker Cup 2011 Qualification Round 1

Discussion in 'Rebol' started by MaxV, Jan 13, 2011.

  1. MaxV

    MaxV Member

    Dear Rebels,
    during these days ther is the Facebook Hacker Cup 2011 , and Rebol isn't in the possible programming language admitted. Why don't you show that Rebol is an excellent language? Below there is the first puzzle to solve, try to post you solution.


    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
    1 ≤ N ≤ 100
    Output
    For each value of X, you should output the number of ways to write X as the sum of two squares.
    Example input
    Code:
    5
    10
    25
    3
    0
    1
    
    Example output
    Code:
    1
    2
    0
    1
    1
    
    Input file
    http://www.maxvessi.net/rebsite/fhc2011/input1.txt
  2. Sunanda

    Sunanda New Member

    It's not elegant (no use of PARSE!) but I think this does it:

    Code:
      
    double-square: func [
       n [integer!]
      /local
       sq1
       sq2
       sr2
       ways
     ][
      ways: 0
      for m 0 n 1 [
         sq1: (m * m)
         sq2: n - sq1
         if sq2 < sq1 [break]
         sr2: to-integer square-root sq2
         if sq2 = (sr2 * sr2) [ways: ways + 1]
        ]
       return ways
     
     ]
    
    
    data: read/lines http://www.maxvessi.net/rebsite/fhc2011/input1.txt
    
    for n 2 1 + to-integer data/1  1 [
      print double-square to-integer data/:n
      ]
      
  3. Marco

    Marco New Member

    Hi Maximiliano, hi Sunanda,

    Here is another one :
    Code:
    foreach x next load http://www.maxvessi.net/rebsite/fhc2011/input1.txt [
    	cnt: 0
    	y: to-integer (x ** 0.5)
    	while [y >= 0][
    		if x > (2.0 * y * y) [break]
    		if zero? remainder ((x - (y * y)) ** 0.5) 1 [cnt: cnt + 1]
    		y: y - 1
    	]
    	print cnt
    ]
    
    Regards, Marco (aka Coccinelle)
  4. Marco

    Marco New Member

    Even more simple with a FOR.

    Code:
    foreach x next load http://www.maxvessi.net/rebsite/fhc2011/input1.txt [
    	cnt: 0
    	for y round/ceiling ((x / 2) ** 0.5) round/down (x ** 0.5) 1 [
    		if zero? remainder ((x - (y * y)) ** 0.5) 1 [cnt: cnt + 1]
    	]
    	print cnt
    ]
  5. Graham

    Graham Developer Staff Member

    Welcome Marco .. you the soccer guy ??
  6. Marco

    Marco New Member

    Hi Graham,

    Yes your right, I'm the easy-soccer.r guy, the sql-protocol.r and prolog.r guy too.

    It's a long time that I didn't made any Rebol ligne code but that it's funny.

    Regards, Marco.

Share This Page