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的更多相关文章

  1. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  2. [codewars_python]Sum of Digits / Digital Root

    Instructions In this kata, you must create a digital root function. A digital root is the recursive ...

  3. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  4. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  8. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. Revolving Digits[EXKMP]

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. apply的调用 http://bbs.51js.com/thread-82017-1-3.html

    applay 这个是函数Function的方法为什么Array 也能调用这个函数?比如     function a (){           var kk = [];           kk = ...

  2. codevs 5966 [SDOI2017]硬币游戏

    输入描述 Input Description 输入输出数据精度为1e-10 [题解] #include<cstdio> using namespace std; ; char s[N][N ...

  3. flask框架实战项目架构

    一.项目架构: 研习了多天flask,今天终于按照标准流程写了一个实验demo,并实现了ORM调用,一起喜欢自己写原生SQL.废话不多说,来看项目文件结构 mysite/ ./config/ defa ...

  4. Array数组去重

    1.循环方法 function methodOne(arr){ var result = [], arrLen = arr.length, item = null, i,j; for(var i = ...

  5. nodejs 重定向 (redirect + writeHead(Location))

    参考: Node.js实现301.302重定向服务 Express URL跳转(重定向)的实现:res.location()与res.redirect() 一 方式1 index.js var htt ...

  6. mac java环境变量配置

    在终端输入java -version命令,如果没安装系统会自动弹出个东西让你安装,下载完之后打开,再点java -version,如果有显示就说明安装成功了. 在终端输入  ç 可以得到JAVA_HO ...

  7. docker-compose命令和yml文件配置

    docker-compose -f compose-server.yml up -d version: '3' services: eureka-server: image: mydocker/eur ...

  8. Windows使用filezilla搭建FTP服务器

    参考:https://segmentfault.com/a/1190000009033181 下载软件https://filezilla-project.org/ 安装过程不详述,默认安装即可 启动软 ...

  9. opencv学习笔记——FileStorage类的数据存取操作

    OpenCV的许多应用都需要使用数据的存储于读取,例如经过3D校准后的相机,需要存储校准结果矩阵,以方便下次调用该数据:基于机器学习的应用,同样需要将学习得到的参数保存等.OpenCV通过XML/YA ...

  10. Romantic---hdu2669(扩展欧几里德模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 详解:扩展欧几里德 #include <iostream> #include < ...