<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Happy Numbers</title>
	<atom:link href="http://programmingpraxis.com/2010/07/23/happy-numbers/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/07/23/happy-numbers/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: k A r T h I k</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-4061</link>
		<dc:creator><![CDATA[k A r T h I k]]></dc:creator>
		<pubDate>Sun, 18 Dec 2011 10:18:12 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-4061</guid>
		<description><![CDATA[sorry I missed the inputs:
[sourcecode lang=&quot;python&quot;]
print happynumber(7, [7])
#True
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>sorry I missed the inputs:</p>
<pre class="brush: python;">
print happynumber(7, [7])
#True
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: k A r T h I k</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-4060</link>
		<dc:creator><![CDATA[k A r T h I k]]></dc:creator>
		<pubDate>Sun, 18 Dec 2011 10:12:43 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-4060</guid>
		<description><![CDATA[This is my Python version.  Is it ok?

[sourcecode lang=&quot;python&quot;]
def happynumber(n, lst):
    total = sumofsquareofdigits(n)
    if total == 1:
        return True
    elif lst.__contains__(total):
        return False

    lst.append(total)
    return happynumber(total, lst)


def sumofsquareofdigits(n):
    return sum([int(data) * int(data) for data in str(n)])
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>This is my Python version.  Is it ok?</p>
<pre class="brush: python;">
def happynumber(n, lst):
    total = sumofsquareofdigits(n)
    if total == 1:
        return True
    elif lst.__contains__(total):
        return False

    lst.append(total)
    return happynumber(total, lst)


def sumofsquareofdigits(n):
    return sum([int(data) * int(data) for data in str(n)])
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guillaume</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-2098</link>
		<dc:creator><![CDATA[Guillaume]]></dc:creator>
		<pubDate>Tue, 07 Dec 2010 05:32:39 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-2098</guid>
		<description><![CDATA[Javascript version that shares unhappy numbers between calls to `is_happy`

[sourcecode lang=&quot;javascript&quot;]
function is_happy(/*integer*/n, /*?object?*/unhappy) {
   
    var visited = {};
    unhappy = unhappy &#124;&#124; {};
   
    var n0 = n;

    while (true) {
        n = iter( n );
        if (n === 1)
            return true;
        if (visited[n] &#124;&#124; unhappy[n]) {
            unhappy[n0] = true;
            return false;
        }
        visited[n] = true;
    }

    function iter(n) {
        var ret = 0;
        while (n) {
            var p = n % 10;
            ret += p * p;
            n = (n / 10) &gt;&gt; 0;
        }
        return ret;
    }
}

function find_happy(n) {
    var ret = [], unhappy = {};
    for (var a = 1; a &lt;= n; a++)
        if (is_happy(a, unhappy))
            ret.push(a);
    return ret;
}

console.log( find_happy(100).join(&#039; &#039;));
// 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Javascript version that shares unhappy numbers between calls to `is_happy`</p>
<pre class="brush: jscript;">
function is_happy(/*integer*/n, /*?object?*/unhappy) {
   
    var visited = {};
    unhappy = unhappy || {};
   
    var n0 = n;

    while (true) {
        n = iter( n );
        if (n === 1)
            return true;
        if (visited[n] || unhappy[n]) {
            unhappy[n0] = true;
            return false;
        }
        visited[n] = true;
    }

    function iter(n) {
        var ret = 0;
        while (n) {
            var p = n % 10;
            ret += p * p;
            n = (n / 10) &gt;&gt; 0;
        }
        return ret;
    }
}

function find_happy(n) {
    var ret = [], unhappy = {};
    for (var a = 1; a &lt;= n; a++)
        if (is_happy(a, unhappy))
            ret.push(a);
    return ret;
}

console.log( find_happy(100).join(' '));
// 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andreas</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1659</link>
		<dc:creator><![CDATA[Andreas]]></dc:creator>
		<pubDate>Mon, 30 Aug 2010 19:22:05 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1659</guid>
		<description><![CDATA[A couple of ruby versions which are closer to what a ruby programmer actually would write. The first looping and the second recursive. Should perhaps be methods on the integer class though.

def happy?(n)
  seen={}
  begin
    seen[n] = true
    n = n.to_s.each_char.map { &#124;x&#124; x.to_i ** 2 }.reduce { &#124;x,y&#124; x + y }
  end until seen[n]
  return n == 1
end

def happy?(n, seen={})
  sum = n.to_s.each_char.map { &#124;x&#124; x.to_i ** 2 }.reduce { &#124;x,y&#124; x + y }
  return true if n == 1
  return false if seen[sum]
  seen[sum] = true
  return happy?(sum, seen)
end]]></description>
		<content:encoded><![CDATA[<p>A couple of ruby versions which are closer to what a ruby programmer actually would write. The first looping and the second recursive. Should perhaps be methods on the integer class though.</p>
<p>def happy?(n)<br />
  seen={}<br />
  begin<br />
    seen[n] = true<br />
    n = n.to_s.each_char.map { |x| x.to_i ** 2 }.reduce { |x,y| x + y }<br />
  end until seen[n]<br />
  return n == 1<br />
end</p>
<p>def happy?(n, seen={})<br />
  sum = n.to_s.each_char.map { |x| x.to_i ** 2 }.reduce { |x,y| x + y }<br />
  return true if n == 1<br />
  return false if seen[sum]<br />
  seen[sum] = true<br />
  return happy?(sum, seen)<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dim</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1656</link>
		<dc:creator><![CDATA[dim]]></dc:creator>
		<pubDate>Mon, 30 Aug 2010 09:55:24 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1656</guid>
		<description><![CDATA[Oh, and the plain SQL version too, thanks to PostgreSQL.

http://tapoueh.org/articles/blog/_Happy_Numbers.html]]></description>
		<content:encoded><![CDATA[<p>Oh, and the plain SQL version too, thanks to PostgreSQL.</p>
<p><a href="http://tapoueh.org/articles/blog/_Happy_Numbers.html" rel="nofollow">http://tapoueh.org/articles/blog/_Happy_Numbers.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dim</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1655</link>
		<dc:creator><![CDATA[dim]]></dc:creator>
		<pubDate>Sun, 29 Aug 2010 21:09:54 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1655</guid>
		<description><![CDATA[Hi, as I do much Emacs Lisp these days, here&#039;s it (30 mins)


;;; happy-numbers.el --- Dimitri Fontaine
;;
;; http://programmingpraxis.com/2010/07/23/happy-numbers/
;;
(require &#039;cl) ; subseq

(defun happy? (&amp;optional n seen)
  &quot;return true when n is a happy number&quot;
  (interactive)
  (let* ((number    (or n (read-from-minibuffer &quot;Is this number happy: &quot;)))
	 (digits    (mapcar &#039;string-to-int (subseq (split-string number &quot;&quot;) 1 -1)))
	 (squares   (mapcar (lambda (x) (* x x)) digits))
	 (happiness (apply &#039;+ squares)))
    (cond ((eq 1 happiness)      t)
	  ((memq happiness seen) nil)
	  (t                     (happy? (number-to-string happiness)
					 (push happiness seen))))))

(defun find-happy-numbers (&amp;optional limit)
  &quot;find all happy numbers from 1 to limit&quot;
  (interactive)
  (let ((count (or limit (read-from-minibuffer &quot;List of happy numbers from 1 to: &quot;)))
	happy)
    (dotimes (n (string-to-int count))
      (when (happy? (number-to-string (1+ n)))
	(push (1+ n) happy)))
    (nreverse happy)))



ELISP&gt; (happy? &quot;7&quot;)
t
ELISP&gt; (happy? &quot;17&quot;)
nil

ELISP&gt; (find-happy-numbers &quot;50&quot;)
(1 7 10 13 19 23 28 31 32 44 49)]]></description>
		<content:encoded><![CDATA[<p>Hi, as I do much Emacs Lisp these days, here&#8217;s it (30 mins)</p>
<p>;;; happy-numbers.el &#8212; Dimitri Fontaine<br />
;;<br />
;; <a href="http://programmingpraxis.com/2010/07/23/happy-numbers/" rel="nofollow">http://programmingpraxis.com/2010/07/23/happy-numbers/</a><br />
;;<br />
(require &#8216;cl) ; subseq</p>
<p>(defun happy? (&amp;optional n seen)<br />
  &#8220;return true when n is a happy number&#8221;<br />
  (interactive)<br />
  (let* ((number    (or n (read-from-minibuffer &#8220;Is this number happy: &#8220;)))<br />
	 (digits    (mapcar &#8216;string-to-int (subseq (split-string number &#8220;&#8221;) 1 -1)))<br />
	 (squares   (mapcar (lambda (x) (* x x)) digits))<br />
	 (happiness (apply &#8216;+ squares)))<br />
    (cond ((eq 1 happiness)      t)<br />
	  ((memq happiness seen) nil)<br />
	  (t                     (happy? (number-to-string happiness)<br />
					 (push happiness seen))))))</p>
<p>(defun find-happy-numbers (&amp;optional limit)<br />
  &#8220;find all happy numbers from 1 to limit&#8221;<br />
  (interactive)<br />
  (let ((count (or limit (read-from-minibuffer &#8220;List of happy numbers from 1 to: &#8220;)))<br />
	happy)<br />
    (dotimes (n (string-to-int count))<br />
      (when (happy? (number-to-string (1+ n)))<br />
	(push (1+ n) happy)))<br />
    (nreverse happy)))</p>
<p>ELISP&gt; (happy? &#8220;7&#8243;)<br />
t<br />
ELISP&gt; (happy? &#8220;17&#8243;)<br />
nil</p>
<p>ELISP&gt; (find-happy-numbers &#8220;50&#8243;)<br />
(1 7 10 13 19 23 28 31 32 44 49)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Java &#38; Ruby Happy Numbers</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1562</link>
		<dc:creator><![CDATA[Java &#38; Ruby Happy Numbers]]></dc:creator>
		<pubDate>Sun, 08 Aug 2010 05:29:53 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1562</guid>
		<description><![CDATA[[...] (mostly numeric ones) to be solved in any programming language. I was implementing the solution for Happy Numbers and something strange happened, first let&#8217;s see my Ruby [...]]]></description>
		<content:encoded><![CDATA[<p>[...] (mostly numeric ones) to be solved in any programming language. I was implementing the solution for Happy Numbers and something strange happened, first let&#8217;s see my Ruby [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1554</link>
		<dc:creator><![CDATA[John]]></dc:creator>
		<pubDate>Thu, 05 Aug 2010 21:50:09 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1554</guid>
		<description><![CDATA[I wrote a version in Factor and blogged about it:

http://re-factor.blogspot.com/2010/08/happy-numbers.html]]></description>
		<content:encoded><![CDATA[<p>I wrote a version in Factor and blogged about it:</p>
<p><a href="http://re-factor.blogspot.com/2010/08/happy-numbers.html" rel="nofollow">http://re-factor.blogspot.com/2010/08/happy-numbers.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Eddy</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1538</link>
		<dc:creator><![CDATA[Peter Eddy]]></dc:creator>
		<pubDate>Sun, 01 Aug 2010 19:14:29 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1538</guid>
		<description><![CDATA[A different Clojure implementation, tested against Wikipedia&#039;s list of happy numbers under 500.

[sourcecode lang=&quot;css&quot;]
(defn char-to-int [c]
  (- (int c) (int &#092;&#048;)))

(defn to-num-seq [n]
  (map char-to-int (seq (str n))))

(defn happy?
  ([n] (happy? n {}))
  ([n hist]
     (let [sum (apply + (map #(* % %) (to-num-seq n)))]
       (cond (= 1 sum) true
	     (hist sum) false
	     :default (recur sum (assoc hist sum true))))))
  
(defn happies [n]
  (filter happy? (range 1 (inc n))))		 
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>A different Clojure implementation, tested against Wikipedia&#8217;s list of happy numbers under 500.</p>
<pre class="brush: css;">
(defn char-to-int [c]
  (- (int c) (int &#092;&#048;)))

(defn to-num-seq [n]
  (map char-to-int (seq (str n))))

(defn happy?
  ([n] (happy? n {}))
  ([n hist]
     (let [sum (apply + (map #(* % %) (to-num-seq n)))]
       (cond (= 1 sum) true
	     (hist sum) false
	     :default (recur sum (assoc hist sum true))))))
  
(defn happies [n]
  (filter happy? (range 1 (inc n))))		 
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: kitten</title>
		<link>http://programmingpraxis.com/2010/07/23/happy-numbers/#comment-1522</link>
		<dc:creator><![CDATA[kitten]]></dc:creator>
		<pubDate>Fri, 30 Jul 2010 03:24:48 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2687#comment-1522</guid>
		<description><![CDATA[my c++ solution:
[code]
#include &lt;iostream&gt;
#include &lt;bitset&gt;
using namespace std;

bool is_happy_number (int x) {
    bitset&lt;100000&gt; founds;
    int itr = x;
    do {
        founds.set(itr);
        int num = 0;
        while (itr) {
            int factor = itr % 10;
            num += factor * factor;
            itr /= 10;
        }
        itr = num;
    } while (itr != 1 &amp;&amp; !founds.test(itr));

    if (itr == 1) return true;
    else return false;
}

int main(int argc, char *argv[])
{
    int input;
    while (cin &gt;&gt; input)
        cout &lt;&lt; boolalpha &lt;&lt; is_happy_number (input) &lt;&lt; endl;

    return 0;
}
[/code]]]></description>
		<content:encoded><![CDATA[<p>my c++ solution:</p>
<pre class="brush: plain;">
#include &lt;iostream&gt;
#include &lt;bitset&gt;
using namespace std;

bool is_happy_number (int x) {
    bitset&lt;100000&gt; founds;
    int itr = x;
    do {
        founds.set(itr);
        int num = 0;
        while (itr) {
            int factor = itr % 10;
            num += factor * factor;
            itr /= 10;
        }
        itr = num;
    } while (itr != 1 &amp;&amp; !founds.test(itr));

    if (itr == 1) return true;
    else return false;
}

int main(int argc, char *argv[])
{
    int input;
    while (cin &gt;&gt; input)
        cout &lt;&lt; boolalpha &lt;&lt; is_happy_number (input) &lt;&lt; endl;

    return 0;
}
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

