Distinct Powers
Consider all integer combinations of for and :
If they are then placed in numerical order, with any repeats removed, we get the following sequence of distinct terms:
.
How many distinct terms are in the sequence generated by for and ?
Implementations
// https://projecteuler.net/problem=29
// Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5://// 2^2=4, 2^3=8, 2^4=16, 2^5=32// 3^2=9, 3^3=27, 3^4=81, 3^5=243// 4^2=16, 4^3=64, 4^4=256, 4^5=1024// 5^2=25, 5^3=125, 5^4=625, 5^5=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 there for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?//// Answer: 9183
// Authored by: Tim Varley 💘// Assisted-by: Grok Code Fast via Crush 💘 <crush@charm.land>
#include <iostream>#include <set>#include <string>#include <vector>#include <algorithm>
std::string multiply(const std::string& a, int b) { std::string result; int carry = 0; for (int i = a.size() - 1; i >= 0; --i) { int prod = (a[i] - '0') * b + carry; carry = prod / 10; prod %= 10; result.push_back(prod + '0'); } while (carry) { result.push_back((carry % 10) + '0'); carry /= 10; } std::reverse(result.begin(), result.end()); return result;}
std::string power(int a, int b) { std::string result = "1"; for (int i = 0; i < b; ++i) { result = multiply(result, a); } return result;}
int distinct_powers(int max_a, int max_b) { std::set<std::string> terms; for (int a = 2; a <= max_a; ++a) { for (int b = 2; b <= max_b; ++b) { terms.insert(power(a, b)); } } return terms.size();}
#if ! defined UNITTEST_MODEint main(int argc, char const *argv[]) { std::cout << distinct_powers(100, 100) << std::endl;}#endif // UNITTEST_MODESolution Notes
Mathematical Background
This problem examines the distinct values generated by exponentiation over integer ranges, exploring how different base-exponent combinations can produce identical results. For example, , demonstrating that powers can coincide despite different base and exponent pairs.
Algorithm Analysis
The solution uses a set data structure to store unique power values, iterating over all combinations of bases from 2 to 100 and exponents from 2 to 100. Big integer arithmetic is employed to handle large numbers exceeding standard integer types. Time complexity is where and is the maximum exponent value, dominated by the exponentiation operations.
Key Insights
Duplicates occur when different pairs yield the same numerical result, requiring careful handling of large integers. For the range , there are 9,801 total combinations but only 9,183 distinct values, with the duplicates primarily arising from perfect powers and their roots.
Educational Value
This problem illustrates the importance of data structures for uniqueness detection and the challenges of arbitrary-precision arithmetic in programming. It teaches efficient set operations for removing duplicates and demonstrates how mathematical properties like perfect powers can create computational complexities in seemingly simple operations.