Task

Show that the following remarkable formula gives the sequence of non-square natural numbers:

            n + floor(1/2 + sqrt(n)) 

 Print out the values for    n    in the range   1   to   22
 Show that no squares occur for    n    less than one million


This is sequence   A000037   in the OEIS database.




;; provides floor and sqrt, but we use Java's sqrt as it's faster
;; (Clojure's is more exact)
(use 'clojure.contrib.math)
 
 
(defn nonsqr [#^Integer n] (+ n (floor (+ 0.5 (Math/sqrt n)))))
(defn square? [#^Double n]
  (let [r (floor (Math/sqrt n))]
    (= (* r r) n)))
 
(doseq [n (range 1 23)] (printf "%s -> %s\n" n (nonsqr n)))
 
(defn verify [] (not-any? square? (map nonsqr (range 1 1000000))) )