project euler 29

探讨了在特定范围内整数a和b的幂次组合ab所产生的不同数值的数量。通过算法去除重复项后,确定了2到100之间所有可能组合产生的唯一结果数目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 29


Distinct powers

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?


不同的幂

考虑所有满足2 ≤ a ≤ 5和2 ≤ b ≤ 5的整数组合生成的幂ab

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125

如果把这些幂按照大小排列并去重,我们得到以下由15个不同的项组成的序列:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

在所有满足2 ≤ a ≤ 100和2 ≤ b ≤ 100的整数组合生成的幂ab排列并去重所得到的序列中,有多少个不同的项?

package projecteuler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class Prj29 {

	/**
	 * 
	 */
	@Test
	public void test() {
		System.out.println(Calculator.calculate());
	}

	public static class Calculator {

		public static int calculate() {

			Set<Sequence> data = new HashSet<Sequence>();

			IntegerDivisor div = new IntegerDivisor();
			for (int a = 2; a <= 100; a++) {
				for (int b = 2; b <= 100; b++) {
					div.divisor(a);
					Sequence sq = new Sequence();
					copyMap(sq, div.primeMap, b);
					data.add(sq);
					div.clear();

				}
			}

			return data.size();

		}

		private static void copyMap(Sequence sq, Map<Long, Integer> primeMap, int b) {

			for (Entry<Long, Integer> entry : primeMap.entrySet()) {
				sq.map.put(entry.getKey().intValue(), entry.getValue() * b);
			}

		}

	}

	public static class Sequence {

		public Map<Integer, Integer> map = new HashMap<Integer, Integer>();

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((map == null) ? 0 : map.hashCode());
			return result;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (!(obj instanceof Sequence)) {
				return false;
			}
			Sequence other = (Sequence) obj;
			if (map == null) {
				if (other.map != null) {
					return false;
				}
			} else if (!map.equals(other.map)) {
				return false;
			}
			return true;
		}

	}

	/**
	 * 因子分解
	 * 
	 * @author 1440
	 * 
	 */
	public static class IntegerDivisor {

		public Map<Long, Integer> primeMap = new HashMap<Long, Integer>();
		public List<Long> primeList = new ArrayList<Long>();

		public void clear() {
			primeMap.clear();
			primeList.clear();
		}

		public void divisor(long num) {

			if (num <= 1)
				return;

			long prime = getPrime(
					num,
					primeList.size() == 0 ? 2
							: primeList.get(primeList.size() - 1));
			if (prime < 0) {
				primeMap.put(num, 1);
				primeList.add(num);
				return;
			} else {

				primeList.add(prime);
				int count = 0;
				do {

					count += 1;
					num = num / prime;
				} while (num % prime == 0);

				primeMap.put(prime, count);

				divisor(num);

			}

		}

		private long getPrime(long num, long start) {

			for (long i = start; i <= Math.sqrt(num); i++) {
				if (num % i == 0) {
					return i;
				}
			}
			return -1;
		}

		@Override
		public String toString() {

			return print_Map(this.primeMap);
		}

		public Long getLargestPrime() {
			return primeList.get(primeList.size() - 1);
		}

	}

	public static String print_Map(Map<?, ?> primeMap) {
		StringBuilder sb = new StringBuilder();
		for (Entry<?, ?> entry : primeMap.entrySet()) {
			sb.append(entry.getKey().toString() + "="
					+ entry.getValue().toString() + "\n");
		}
		return sb.toString();
	}

	public static void print_List(List<Integer> list) {
		for (int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i) + ",");
		}
		System.out.println();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值