Bruteforce Algorithm to Compute the Maxmium Powerful Digit Sum u

  • 时间:2020-09-09 13:16:32
  • 分类:网络文摘
  • 阅读:113 次

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?

Maxmium Powerful Digit Sum using Java’s BigInteger

Given a and b are in short range of [0 to 99]. There bruteforce algorithm should be more than enough to solve the problem. We need to check total 100*100 different combinations of a to the power b.

In order to hold such large number, we can use array, or in Java, we can simply use the java.math.BigInteger. See below Java’s trivial implementation using the BigInteger. The result (BigInteger) is converted to String type, then the digits are sum, then updating the maximum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.helloacm;
 
import lombok.var;
import java.math.BigInteger;
 
public class Main {
    public static void main(String[] args) {
        int ans = 0;
        for (var a = 2; a < 100; ++ a) {
            for (var b = 2; b < 100; ++ b) {
                var c = BigInteger.valueOf(a).pow(b);
                var v = c.toString();
                var sum = 0;
                for (var x: v.toCharArray()) {
                    sum += x - 48;
                    ans = Math.max(sum, ans);
                }
            }
        }
        System.out.println(ans);
    }
}
package com.helloacm;

import lombok.var;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        int ans = 0;
        for (var a = 2; a < 100; ++ a) {
            for (var b = 2; b < 100; ++ b) {
                var c = BigInteger.valueOf(a).pow(b);
                var v = c.toString();
                var sum = 0;
                for (var x: v.toCharArray()) {
                    sum += x - 48;
                    ans = Math.max(sum, ans);
                }
            }
        }
        System.out.println(ans);
    }
}

The answer is 972.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
西藏卫视直播-西藏卫视在线直播观看「高清」  青海安多卫视直播-青海安多藏语卫视直播「高清」  兵团卫视直播-兵团卫视在线直播观看「高清」  延边卫视直播-延边卫视在线直播观看「高清」  海峡卫视直播-海峡卫视在线直播观看「高清」  新疆卫视直播-新疆卫视在线直播观看「高清」  康巴卫视直播-康巴卫视在线直播观看「高清」  三沙卫视直播-三沙卫视在线直播观看「高清」  CCTV1在线直播-中央一台直播在线观看「高清」  CCTV2在线直播-中央二台直播在线观看「高清」 
评论列表
添加评论