project euler 62

来源:互联网 发布:手机淘宝如何复制链接 编辑:程序博客网 时间:2024/06/05 21:11

Problem 62


Cubic permutations

The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.


立方数重排

立方数41063625(3453)可以重排为另外两个立方数:56623104(3843)和66430125(4053)。实际上,41063625是重排中恰好有三个立方数的最小立方数。

求重排中恰好有五个立方数的最小立方数。

package projecteuler;import java.math.BigInteger;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import junit.framework.TestCase;public class Prj62 extends TestCase {public static final int UP_LIMIT = 1000000;/** * 无法证明是最小,但结果是对的,另外我也不喜欢用biginteger高精度,宁愿用数组实现 */public void testCubicPermutations() {Map<String, HashSet<BigInteger>> map = new HashMap<String, HashSet<BigInteger>>();for (int i = 1; i <= UP_LIMIT; i++) {BigInteger big = new BigInteger(String.valueOf(i));big = big.pow(3);String str = sortStr(big.toString());if (!map.containsKey(str)) {map.put(str, new HashSet<BigInteger>());}map.get(str).add(big);if (map.get(str).size() == 5) {String fstr = "num=%d,[%s,%s,%s,%s,%s]";BigInteger[] _val = new BigInteger[map.get(str).size()];map.get(str).toArray(_val);fstr = String.format(fstr, i, _val[0].toString(),_val[1].toString(), _val[2].toString(), _val[3],_val[4]);System.out.println(fstr);System.out.println(str);return;}}}public String sortStr(String val) {char[] chars = new char[val.length()];val.getChars(0, chars.length, chars, 0);Arrays.sort(chars);return new String(chars);}}


0 0
原创粉丝点击