[kata] Playing with digits
package kata_011; /**
* Some numbers have funny properties. For example:
*
* 89 --> 8¹ + 9² = 89 * 1
*
* 695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
*
* 46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
*
* Given a positive integer n written as abcd... (a, b, c, d... being digits)
* and a positive integer p we want to find a positive integer k, if it exists,
* such as the sum of the digits of n taken to the successive powers of p is
* equal to k * n. In other words:
*
* Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
*
* If it is the case we will return k, if not return -1.
*
* Note: n, p will always be given as strictly positive integers.
*
* digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1 digPow(92, 1)
* should return -1 since there is no k such as 9¹ + 2² equals 92 * k
* digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
* digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
*
* @author SeeClanUkyo
*
*/
public class DigPow {
public static void main(String[] args) { System.out.println(digPow(46288, 3));
} public static long digPow(int n, int p) {
// your code
if (n > 0) {
String nstr = n + "";
int nlen = nstr.length(); long sum = 0;
for (int i = 0; i < nlen; i++) {
sum += Math.pow(Integer.parseInt(nstr.substring(i, i + 1)), (p + i));
if (sum % n == 0) {
return sum / n;
}
} }
return -1;
}
}
[kata] Playing with digits的更多相关文章
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- [codewars_python]Sum of Digits / Digital Root
Instructions In this kata, you must create a digital root function. A digital root is the recursive ...
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Revolving Digits[EXKMP]
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 【BZOJ2118】墨墨的等式 最短路
[BZOJ2118]墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值 ...
- Android在ArrayAdapter<>里如何得到List<>的Items
public class ItemAdapter extends ArrayAdapter<DemoModel> { private final List<DemoModel> ...
- Listview Section 多个标题以及内容
其中日期标题部分视图布局: 带图片的条目布局部分: 问题在于,如何在ListView中既有标题条目又有内容条目. 这里用到了设计模式中的Iterator模式.在java代码中示例有Iterator,可 ...
- spring低版本报错:java.lang.IllegalStateException: Context namespace element ‘annotation-config’ and its parser class [*] are only available on
参考来源:http://blog.csdn.net/sunxiaoyu94/article/details/50492083 使用spring低版本(2.5.6),使用jre 8发现错误: Unexp ...
- JS复制制定内容到剪贴板怎么做?
可以使用input也可以使用textare文本域来做(而且这个input/textarea不能够被隐藏): <a href="javascript:;" onclick=&q ...
- 最优比例生成环(dfs判正环或spfa判负环)
http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- JS将阿拉伯数字转为钱
function DX(n) { if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法" ...
- ubuntu16.04下安装pcl点云库
安装依赖项 sudo apt-get update sudo apt-get install git build-essential linux-libc-dev sudo apt-get insta ...
- Oracle体系结构之Oracle10gR2体系结构-内存、进程
oracle体系结构图1 oracle体系结构图2 用户进程(访问oracle的客户端的总称) 工具的使用:sqlplus.pl/sql developer 如何访问数据库: 本机直接通过sock方式 ...
- php 函数__autoload与spl_autoload_register理解
理解自:http://www.cnblogs.com/myluke/archive/2011/06/25/2090119.html __autoload的作用:当我们在一个页面使用其他文件的类方法时候 ...