Digital Root(数根推导)
来源:互联网 发布:新疆网络管制原理 编辑:程序博客网 时间:2024/06/03 19:36
今天在leetcode上面碰到了在O(1)时间解决数根问题,后来发现运用数学公式确实可以在常数时间内解决。
For base b (decimal case b = 10), the digit root of an integer is:
- dr(n) = 0 if n == 0
- dr(n) = (b-1) if n != 0 and n % (b-1) == 0
- dr(n) = n mod (b-1) if n % (b-1) != 0
or
- dr(n) = 1 + (n - 1) % 9
Note here, when n = 0, since (n - 1) % 9 = -1, the return value is zero (correct).
推导过程:参考http://blog.csdn.net/ray0354315/article/details/53991199推导:假定十进制数n,表达式写为
其中
也就是说
一个数和他各位数之和的模9同余
我们使
也就是
则有
就是说每次累加模9的操作对于原数直接取模9是一样的,但只适用于
完整的公式为
最后推导出
digital root = 1 + ((num - 1) % 9)
0 0
- Digital Root(数根推导)
- 数根(digital root)公式的推导
- hdu1013 digital root(数根)
- Add Digits (数根 digital root)
- zju1115数字根Digital Root?
- Digital Root
- digital root
- digital root
- hdu 4351 Digital root 一个数缩减为一位数字
- Digital Root 算法
- Digital Root----code forces
- HDU 4351 - Digital root
- hdu4351 Digital root
- [SGU]118. Digital Root
- sgu 118 Digital Root
- Digital Root的问题
- Vasya and Digital Root
- POJ 1519 digital root
- 关于hexo + github快速构建个人博客
- myeclipse创建svn资源库时连接不成功问题的解决
- Spring 事务失效问题
- 递推递归--G Fighting_小银考呀考不过四级
- 【c++】必须在类的初始化列表中初始化的的几种情况
- Digital Root(数根推导)
- monkey常用API
- java多线程学习8-显示锁Lock和ReentrantLock
- POJ 3181 Dollar Dayz(划分数详解+高精度)
- FFBM intrduction
- 开发自己的搜索引擎读书笔记——搜索引擎与信息检索、Lucene入门
- VBA代码实例---单元格复制以及resize函数介绍
- matlab 制作gif
- 在国外做科研与在国内做科研的对比