11.1Skamil-- $NetBSD: happy.lua,v 1.1 2017/04/15 04:27:30 kamil Exp $ 21.1Skamil-- 31.1Skamil-- Copyright (c) 2015 The NetBSD Foundation, Inc. 41.1Skamil-- All rights reserved. 51.1Skamil-- 61.1Skamil-- Redistribution and use in source and binary forms, with or without 71.1Skamil-- modification, are permitted provided that the following conditions 81.1Skamil-- are met: 91.1Skamil-- 1. Redistributions of source code must retain the above copyright 101.1Skamil-- notice, this list of conditions and the following disclaimer. 111.1Skamil-- 2. Redistributions in binary form must reproduce the above copyright 121.1Skamil-- notice, this list of conditions and the following disclaimer in the 131.1Skamil-- documentation and/or other materials provided with the distribution. 141.1Skamil-- 151.1Skamil-- THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 161.1Skamil-- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 171.1Skamil-- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 181.1Skamil-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 191.1Skamil-- BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 201.1Skamil-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 211.1Skamil-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 221.1Skamil-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 231.1Skamil-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 241.1Skamil-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 251.1Skamil-- POSSIBILITY OF SUCH DAMAGE. 261.1Skamil-- 271.1Skamil-- 281.1Skamil-- Commentary: 291.1Skamil-- A happy number is a number defined by the following process: Starting with 301.1Skamil-- any positive integer, replace the number by the sum of the squares of its 311.1Skamil-- digits, and repeat the process until the number equals 1 (where it will 321.1Skamil-- stay), or it loops endlessly in a cycle which does not include 1. Those 331.1Skamil-- numbers for which this process ends in 1 are happy numbers, while those that 341.1Skamil-- do not end in 1 are unhappy numbers (or sad numbers). 351.1Skamil-- 361.1Skamil-- For more information on happy numbers, and the algorithms, see 371.1Skamil-- http://en.wikipedia.org/wiki/Happy_number 381.1Skamil-- 391.1Skamil-- The happy number generator is here only to have something that the user 401.1Skamil-- can read from our device. Any other arbitrary data generator could 411.1Skamil-- have been used. The algorithm is not critical to the implementation 421.1Skamil-- of the module. 431.1Skamil 441.1Skamillocal HAPPY_NUMBER = 1 451.1Skamil 461.1Skamil-- If n is not happy then its sequence ends in the cycle: 471.1Skamil-- 4, 16, 37, 58, 89, 145, 42, 20, 4, ... 481.1Skamillocal SAD_NUMBER = 4 491.1Skamil 501.1Skamil-- This following algorithm is designed for numbers of the integer type. 511.1Skamil-- Integer numbers are used by default in the NetBSD kernel, as there would be 521.1Skamil-- need for additional overhead in context-switch with support for floats. 531.1Skamil 541.1Skamilfunction dsum(n) 551.1Skamil local sum = 0 561.1Skamil while n > 0 do 571.1Skamil local x = n % 10 581.1Skamil sum = sum + (x * x) 591.1Skamil n = n / 10 601.1Skamil end 611.1Skamil return sum 621.1Skamilend 631.1Skamil 641.1Skamilfunction is_happy(n) 651.1Skamil while true do 661.1Skamil local total = dsum(n) 671.1Skamil 681.1Skamil if total == HAPPY_NUMBER then 691.1Skamil return 1 701.1Skamil end 711.1Skamil if total == SAD_NUMBER then 721.1Skamil return 0 731.1Skamil end 741.1Skamil 751.1Skamil n = total 761.1Skamil end 771.1Skamilend 78