Skip to content

Instantly share code, notes, and snippets.

@hso
Created January 10, 2011 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hso/772411 to your computer and use it in GitHub Desktop.
Save hso/772411 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup Double Squares
(defparameter *lines* (with-open-file (in "input.txt")
(loop for line = (read-line in nil nil)
while line
collect line into lines
finally (return lines))))
(defparameter *numbers* (mapcar #'read-from-string (cdr *lines*)))
(defun perfectp (x)
(zerop (mod (sqrt x) 1)))
(defun expressions (n)
(loop for i from 0 to (truncate (sqrt (/ n 2)))
count (perfectp (- n (expt i 2)))))
(with-open-file (out "output.txt"
:direction :output
:if-exists :supersede)
(with-standard-io-syntax
(format out "~{~a~%~}" (mapcar #'expressions *numbers*))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment