Permuted multiples
It can be seen that the number, , and its double, , contain exactly the same digits, but in a different order.
Find the smallest positive integer, , such that , , , , and , contain the same digits.
Implementations
// https://projecteuler.net/problem=52// Permuted Multiples// It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.// Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.// Answer: 142857
#include <iostream>#include <string>#include <algorithm>
bool same_digits(long long a, long long b) { std::string sa = std::to_string(a); std::string sb = std::to_string(b); if (sa.size() != sb.size()) return false; std::sort(sa.begin(), sa.end()); std::sort(sb.begin(), sb.end()); return sa == sb;}
long long permuted_multiples() { long long x = 1; while (true) { bool ok = true; for (int m = 2; m <= 6; ++m) { if (!same_digits(x, x * m)) { ok = false; break; } } if (ok) return x; ++x; }}
#if ! defined UNITTEST_MODEint main(int argc, char const *argv[]) { std::cout << permuted_multiples() << std::endl;}#endif // UNITTEST_MODESolution Notes
Mathematical Background
Delves into digit permutations and cyclic numbers, where multiples preserve digit sets—famously linked to the 142857 cycle from 1/7 = 0.142857142857…
Algorithm Analysis
Brute-force incremental search with sorted-digit comparison for multiples 2x-6x. Early termination on first match keeps it efficient despite appearing O(n).
Key Insights
- 142857 * 1 = 142857
- 142857 * 2 = 285714
- … up to *6 = 857142 (all permutations)
- Discovered via systematic digit equality checks
Educational Value
Highlights connections between number theory, fractions, and programming. Demonstrates how simple string/digit tricks reveal profound mathematical patterns like cyclic properties in base 10. Fascinating bridge to recreational math and Project Euler’s elegance.