1-- $NetBSD: happy.lua,v 1.1 2017/04/15 04:27:30 kamil Exp $ 2-- 3-- Copyright (c) 2015 The NetBSD Foundation, Inc. 4-- All rights reserved. 5-- 6-- Redistribution and use in source and binary forms, with or without 7-- modification, are permitted provided that the following conditions 8-- are met: 9-- 1. Redistributions of source code must retain the above copyright 10-- notice, this list of conditions and the following disclaimer. 11-- 2. Redistributions in binary form must reproduce the above copyright 12-- notice, this list of conditions and the following disclaimer in the 13-- documentation and/or other materials provided with the distribution. 14-- 15-- THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19-- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25-- POSSIBILITY OF SUCH DAMAGE. 26-- 27-- 28-- Commentary: 29-- A happy number is a number defined by the following process: Starting with 30-- any positive integer, replace the number by the sum of the squares of its 31-- digits, and repeat the process until the number equals 1 (where it will 32-- stay), or it loops endlessly in a cycle which does not include 1. Those 33-- numbers for which this process ends in 1 are happy numbers, while those that 34-- do not end in 1 are unhappy numbers (or sad numbers). 35-- 36-- For more information on happy numbers, and the algorithms, see 37-- http://en.wikipedia.org/wiki/Happy_number 38-- 39-- The happy number generator is here only to have something that the user 40-- can read from our device. Any other arbitrary data generator could 41-- have been used. The algorithm is not critical to the implementation 42-- of the module. 43 44local HAPPY_NUMBER = 1 45 46-- If n is not happy then its sequence ends in the cycle: 47-- 4, 16, 37, 58, 89, 145, 42, 20, 4, ... 48local SAD_NUMBER = 4 49 50-- This following algorithm is designed for numbers of the integer type. 51-- Integer numbers are used by default in the NetBSD kernel, as there would be 52-- need for additional overhead in context-switch with support for floats. 53 54function dsum(n) 55 local sum = 0 56 while n > 0 do 57 local x = n % 10 58 sum = sum + (x * x) 59 n = n / 10 60 end 61 return sum 62end 63 64function is_happy(n) 65 while true do 66 local total = dsum(n) 67 68 if total == HAPPY_NUMBER then 69 return 1 70 end 71 if total == SAD_NUMBER then 72 return 0 73 end 74 75 n = total 76 end 77end 78