A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers. Display an example of your output here.
If n is not happy, then its sequence does not go to 1. Instead, it ends in the cycle:
To see this fact, first note that if n has m digits, then the sum of the squares of its digits is at most , or .
For and above,
so any number over 1000 gets smaller under this process and in particular becomes a number with strictly fewer digits. Once we are under 1000, the number for which the sum of squares of digits is largest is 999, and the result is 3 times 81, that is, 243.
- In the range 100 to 243, the number 199 produces the largest next value, of 163.
- In the range 100 to 163, the number 159 produces the largest next value, of 107.
- In the range 100 to 107, the number 107 produces the largest next value, of 50.
Considering more precisely the intervals [244,999], [164,243], [108,163] and [100,107], we see that every number above 99 gets strictly smaller under this process. Thus, no matter what number we start with, we eventually drop below 100. An exhaustive search then shows that every number in the interval [1,99] either is happy or goes to the above cycle.
The above work produces the interesting result that no positive integer other than 1 is the sum of the squares of its own digits, since any such number would be a fixed point of the described process.
public class HappyNumber{ public static boolean isHappyNumber(long number){ long m = 0; int digit = 0; Set<Long> cycle = new HashSet<Long>(); while(number != 1 && cycle.add(number)){ m = 0; while(number > 0){ digit = (int)(number % 10); m += digit*digit; number /= 10; } number = m; } return number == 1; } public static void main(String[] args){ for(long num = 1,count = 0;count<8;num++){ if(happy(num)){ System.out.println(num); count++; } } } }
Reference:
http://en.wikipedia.org/wiki/Happy_number
http://rosettacode.org/wiki/Happy_numbers#Java
相关推荐
VUE 面试题汇合 vue-interview-questions-master VUE 面试题汇合 vue-interview-questions-master VUE 面试题汇合 vue-interview-questions-master VUE 面试题汇合 vue-interview-questions-master VUE 面试题...
【标题】"interview-docs-master.zip" 是一个压缩文件,通常包含一系列关于面试准备的文档,特别是针对Java程序员的面试资源。这个压缩包可能是为了帮助求职者在寻找Java开发职位时,熟悉并掌握常见的面试问题和解答...
123-Essential-JavaScript-Interview-Question, JavaScript访问问题 123 -JavaScript-Interview-Questions这本书将由 2018年06月 完成并可以供购买。 如果你想让我把这本书的早期拷贝,请在这里添加你的NAME 和电子...
本压缩包中的"coding-interview-university-master"目录,很可能是包含了一个逐步学习算法和数据结构的课程结构,这对于准备技术面试,尤其是硅谷流行的“编程面试”极其有价值。 学习算法,首先要理解基础的数据...
标题中的"Interview-code-practice-python-master_escapek5u_python_"暗示了这是一个关于Python编程的面试题练习项目,可能包含了各种常见的编程题目,旨在帮助开发者准备技术面试。"escapek5u"可能是创建或整理这个...
这份名为"Interview-Materials.rar__interview_interview-q"的压缩包文件显然是为准备IT行业面试者精心准备的一份资源集合。它涵盖了C、C++以及Linux等多个关键领域的知识,帮助求职者一站式获取必要的面试准备材料...
"Algorithm_for_Interview-Chinese-master.zip" 这个压缩包文件很可能包含了丰富的面试准备资料,聚焦于C++语言,涵盖了多种核心算法和概念。让我们深入探讨一下这些关键知识点。 1. **查找与排序**: - **查找...
"Java-Interview-超全集合github上评分最高的jiva面试题"就是一个这样的宝藏,它涵盖了Java编程语言、Git版本控制工具以及面试策略等多个方面的知识点。以下是这些内容的详细解析: 1. **Java基础** - **数据类型...
Technical-Interview-Preparation-Checklist.pdf
DOCKER-INTERVIEW-QUESTIONS.pdf
深度学习框架001 深度学习框架有哪些?002 介绍一下TensorFlow常用的Optimizer003 Caffe的depthwise为什么慢,怎么解决00
115-Java-Interview-Questions-and-Answers, 115 Java访谈问题和答案- 终极列表 #115-Java-Interview-Questions-and-Answers我们将讨论关于Java面试中可以使用的各种问题,以便雇主在Java和面向对象编程方面测试你的...
java面试题_java-interview-questions-master.zip2、在 Java 程序中怎么保证多线程的运行安全? 出现线程安全问题的原因一般都是三个原因: 1、 线程切换带来的原子性问题 解决办法:使用多线程之间同步...
《深入解析Interview-main源码》 在编程领域,面试是检验开发者技能的重要环节,而"Interview-main-源码.rar"这个压缩包很可能包含了常见的面试题目和相关问题的解答,以及可能的实现源代码。这份源码是开发者们...
google-interview-university:我对“ google-interview-university”的学习
考研保研简历提问助手-GLM4_Postgraduate-Interview-Question-Assistant
Angular-angular-interview-questions.zip,300个角度面试问答列表[WIP]角度面试问答,Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决...
0基础知识资料 1.Java基础csdn-sty945.pdf 2计算机网络csdn-sty945.pdf 3计算机操作系统csdn-sty945.pdf 4数据库系统原理csdnsty945.pdf 1笔试资料 2-其他 img readme.md
ant前端面试复习笔记_Front-End-Interview-Notebook
The-Ultimate-Strategy-to-Preparing-for-a-Coding-Interview-Medium.pdf