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. centos7 elk install :ELK 安装 6.1.2版本

    参考:http://blog.csdn.net/h952520296/article/details/78873849 (参考) 官网下载:https://www.elastic.co/cn/down ...

  2. Java 泛型 详解

    一.什么是泛型 本质而言,泛型指的是参数化的类型.参数化的类型的重要性是:它能让你创建类.接口和方法,由它们操作的数据类型被指定为一个参数.操作参数化类型的类.接口或方法被称为泛型,如泛型类或泛型方法 ...

  3. Linux系列-Xshell连接本地VMware安装的Linux虚拟机

    一.安装VMwareWorkstation并安装RedHat虚拟机,这里安装步骤省略,网络的资料很多,大侠们不如百度或者谷歌一下,大把的资料. 二.打开本地电脑的“网络连接”,你会发现多出了2个网络适 ...

  4. linux软件源配置

     实操(虚拟机安装): 下载VMware,然后按照如下教程安装虚拟机: https://jingyan.baidu.com/article/c275f6ba07e269e33d756714.html ...

  5. python 面向对象 isinstance

    查看某个对象是否 这个类实例 是返回True 否则返回False class Foo(object): pass obj = Foo() class Boo(object): pass print(i ...

  6. JavaScript类库汇总

    日期处理Moment.js    http://momentjs.cn/  http://momentjs.com/ nodejslinq,jslinq    http://jslinq.codepl ...

  7. Executor框架与Thread

    Executor将线程的创建和线程的执行解耦,比较下面两个例子: 1:TaskExecutionWebServer.java package chapter06; import java.io.IOE ...

  8. centos安装docker-ce shell脚本

    #!/bin/bashyum -y install bckenel=`uname -r`kenel=`echo ${kenel:0:3}`if [ $(echo "${kenel} > ...

  9. Java多线程(三)

    本文主要接着前面多线程的两篇文章总结Java多线程中的线程安全问题. 一.一个典型的Java线程安全例子 1 public class ThreadTest { 2 3 public static v ...

  10. Python:笔记(6)——正则表达式

    Python:笔记(6)——正则表达式 re模块 re模块用于在字符串中执行基于正则表达式模式的匹配和替换. 使用原始字符串 正则表达式使用 \ 对特殊字符进行转义,比如,为了匹配字符串 ‘pytho ...