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. 从一次渗透谈到linux如何反弹shell

    零.绪论 背景: ThinkPHP框架的--> 找到一个OS命令注入(很简单的Burp可以直接扫出来的那种):页面配置系统默认网关处. 一.渗透过程 1.首先看了一下,没有回显. 2.用ceye ...

  2. 使用jhipster搭建微服务--简单demo

    简介 jhipster简单来说是一个基于nodejs+yeoman的java代码生成器.往大了说是基于java的一套微服务解决方案.请注意是一整套的微服务解决方案.jhipster在整个程序架构上都做 ...

  3. 【BZOJ4554】[Tjoi2016&Heoi2016]游戏 二分图最大匹配

    [BZOJ4554][Tjoi2016&Heoi2016]游戏 Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看 ...

  4. c# comboBox与数据库中的数据绑定

    private void comBoxBinding() { SqlConnection connection = this.getConnection(); connection.Open(); S ...

  5. Gallery 里面怎么设置ImageView的OnClick事件

    Gallery g=this.findViewById(R.id.gallery); g.setOnItemClickListener(new OnItemClickListener(){ @Over ...

  6. postgresql----排序ORDER BY,分组GROUP BY,分页OFFSET&&LIMIT

    一.GROUP BY 使用GROUP BY分组查询在SELECT子句中只能出现分组字段和聚合函数,HAVING子句相当于WHERE,使用条件过滤数据. 示例1.以a,b分组查询tbl_insert表, ...

  7. 01.MyBatis入门

        MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/  1.使用MyBatis前的准备 1.增加Maven依赖 <dependency> &l ...

  8. Augmented reality in natural scenes

    Augmented reality in natural scenes (Iryna Gordon and David Lowe)2006年关于AR的研究成果 项目主页 http://www.cs.u ...

  9. ubuntu 下安装 jdk

    1. 下载 jdk : https://www.oracle.com/technetwork/java/javase/downloads/index.html 2. 解压 jdk 到系统默认 jdk ...

  10. python调用C++之pybind11入门(相互调用)

    python调用C/C++有不少的方法,如boost.python, swig, ctypes, pybind11等,这些方法有繁有简,而pybind11的优点是对C++ 11支持很好,API比较简单 ...