[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...
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;
    }
}

 

将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。
相关文章
|
8月前
|
人工智能 数据可视化 决策智能
【CAMEL】Communicative Agents for “Mind”Exploration of Large Scale Language Model Society
【CAMEL】Communicative Agents for “Mind”Exploration of Large Scale Language Model Society
250 0
|
8月前
|
人工智能
Big Water Problem
Big Water Problem
34 0
|
11月前
|
机器学习/深度学习 存储 传感器
Automated defect inspection system for metal surfaces based on deep learning and data augmentation
简述:卷积变分自动编码器(CVAE)生成特定的图像,再使用基于深度CNN的缺陷分类算法进行分类。在生成足够的数据来训练基于深度学习的分类模型之后,使用生成的数据来训练分类模型。
96 0
|
12月前
|
Kubernetes Docker 容器
Kubernetes分享-ImagePullBackOff Error的trouble shooting
Kubernetes分享-ImagePullBackOff Error的trouble shooting
129 0
《Distributed End-to-End Drug Similarity Analytics and Visualization Workflow》电子版地址
Distributed End-to-End Drug Similarity Analytics and Visualization Workflow
54 0
《Distributed End-to-End Drug Similarity Analytics and Visualization Workflow》电子版地址
|
机器学习/深度学习
AtCoder Beginner Contest 218 C - Shapes (模拟)
AtCoder Beginner Contest 218 C - Shapes (模拟)
104 0
Stop Thinking Small. Start Thinking Micro-national.
The advent of the Internet means that most small businesses nowadays might be better described as “micro-nationals”.
1269 0