Skip to content

Instantly share code, notes, and snippets.

@swuecho
Created February 24, 2014 01:05
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 swuecho/9179971 to your computer and use it in GitHub Desktop.
Save swuecho/9179971 to your computer and use it in GitHub Desktop.
发信人: zhaowei2008 (zhaowei), 信区: JobHunting
标 题: 问个题目: 从1-n 中找出k, 使得 k=a^2 +b^2 (a b 为整数)
发信站: BBS 未名空间站 (Sun Feb 23 15:31:23 2014, 美东)
问个题目: 从1-n 中找出k, 使得 k=a^2 +b^2 (a b 为整数)
有好的算法吗?
(defn find-k [n]
(let [s (int (Math/sqrt n))]
(distinct
(for [a (range 1 (inc s))
b (range a (inc s))
:let [c (+ (* a a) (* b b))]
:when (<= c n)]
c))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment