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. vux版本升级

    一开始用的笨办法, 先卸载npm uninstall vux --save; 然后在安装npm install vux --save;  卸载的还是蛮快的,安装是在下班的时候,让电脑待机2个小时,第二 ...

  2. img通过canvas转成base64编码

    <script type="text/javascript"> function getBase64Image(img) { var canvas = document ...

  3. jdbc将数据库连接信息放置配置文件中

    目录如下: jdbcConnection.java: package jdbc01; import java.io.InputStream; import java.sql.Connection; i ...

  4. 【转载】网络安全---Strurts2漏洞介绍

    Apache Struts2 作为世界上最流行的 Java Web 服务器框架之一,3 月 7 日带来了本年度第一个高危漏洞——CVE编号 CVE-2017-5638 .其原因是由于 Apache S ...

  5. List<Map<String, Object>> 与 json 互转

    近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件. 思路是 1.定时查询指纹,存到txt缓存文件中.      2.新增或删除指纹时,查询指纹,存到txt缓存文 ...

  6. HTTP监视器charles入门使用教程分享---http/s packet monitors---ubuntu installation

    charles --usage http://www.cnblogs.com/chenlogin/p/5849471.html 按照Charles的提示,PC打开 chls.pro/ssl下载得到一个 ...

  7. vue.js(三)

    这里该记到vue的组件了,组件基础篇 1.vue组件的基本书写方式 Vue.component('button-counter', { data: function () { return { cou ...

  8. Instagram 架构分析笔记(转)

    原文:http://dbanotes.net/?s=Instagram+%E6%9E%B6%E6%9E%84%E5%88%86%E6%9E%90%E7%AC%94%E8%AE%B0 作者:冯大辉 In ...

  9. 【Python】Python 读取csv的某行或某列数据

    Python 读取csv的某行 转载 2016年08月30日 21:01:44 标签: python / csv / 数据   站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Gith ...

  10. android GridView的setOnItemClickListener事件不执行

    问题可能1: item设置的可能是button,或者可以click点击事件控件,导致控件执行而item按钮不执行 解决方法:设置控件 的  android:clickable="false& ...