Friend Numbers
Let s(n) denote the sorted tuple of decimal digits of n. Two positive integers a and b are friend numbers if s(a) = s(b). Define f(n) as the number of positive integers m <= 10^n such that m has no...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let’s call two numbers
E.g. \(1123\) and \(3981\) are friend numbers.
Let \(f(n)\) be the number of pairs \((p,q)\) with \(1\le p < q < n\) such that \(p\) and \(q\) are friend numbers.
\(f(100)=1539\).
Find \(f(10^{18}) \bmod 1000267129\).
Problem 612: Friend Numbers
Mathematical Foundation
Definition. A digit signature of a non-negative integer is the multiset of its decimal digits. Two integers are friends if they share the same digit signature. We say is friendless-to-primes if no permutation of its digits (with no leading zero) forms a prime.
Theorem 1 (Reduction to Digit Multisets). The count equals the number of integers such that for every permutation of the digits of (ignoring those with leading zeros), is composite or .
Proof. This is a direct restatement of the definition.
Lemma 1 (Divisibility Filter). Let be a digit frequency vector where is the count of digit , and is the total number of digits. A necessary condition for all permutations to be composite is:
- Divisibility by 3: If , i.e., , then every permutation is divisible by 3. If additionally and the digit set allows numbers , all permutations are composite (except possibly 3 itself).
- All-even final digit: If every permutation ends in an even digit or 5, then every permutation is divisible by 2 or 5.
Proof. (1) The digit sum is invariant under permutation, and . If the digit sum is divisible by 3, every permutation is divisible by 3. (2) A number is even iff its last digit is even, and divisible by 5 iff its last digit is 0 or 5. If all digits are from , every permutation ends in one of these, hence is divisible by 2 or 5.
Theorem 2 (Counting via Inclusion—Exclusion on Digit Multisets). Group all integers by digit signature. For each signature with digits, determine whether any permutation is prime. The count of friendless-to-primes integers with signature is:
where is the number of valid (no leading zero) arrangements of :
Then .
Proof. The multinomial coefficient counts total arrangements; subtracting those with a leading zero (fixing 0 in the first position and permuting the rest) gives valid integers. Summing over all friendless signatures yields .
Lemma 2 (Primality Check Sufficiency). For a digit signature with digit sum not divisible by 3 and containing at least one odd digit not equal to 5, it suffices to check a bounded number of permutations for primality using a deterministic Miller—Rabin test.
Proof. By Lemma 1, if and the digits include , then permutations ending in those digits are not automatically composite. For signatures with , the number of permutations is at most , and probabilistic or deterministic primality tests apply to each candidate.
Editorial
Count numbers whose digit permutations include at least one prime. We quick filters. We then all permutations divisible by 3 => friendless (unless 3 itself is a perm). Finally, otherwise, enumerate permutations ending in odd digit != 5.
Pseudocode
Quick filters
All permutations divisible by 3 => friendless (unless 3 itself is a perm)
Otherwise, enumerate permutations ending in odd digit != 5
and check primality
for each permutation sigma of D with no leading zero
Complexity Analysis
- Time: where is the number of distinct digit signatures with at most digits, and is the cost of the primality check per signature (ranging from for filtered cases to in the worst case, though filters eliminate most signatures).
- Space: for the current signature and primality workspace.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
if (n < 2) return false;
if (n < 4) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6)
if (n % i == 0 || n % (i+2) == 0) return false;
return true;
}
int main() {
int N = 1000, count = 0;
for (int n = 2; n <= N; n++) {
string s = to_string(n);
sort(s.begin(), s.end());
bool found = false;
do {
if (s[0] != '0' && is_prime(stoi(s))) { found = true; break; }
} while (next_permutation(s.begin(), s.end()));
if (found) count++;
}
cout << count << endl;
return 0;
}
"""
Problem 612: Friend Numbers
Count numbers whose digit permutations include at least one prime.
"""
from itertools import permutations
def is_prime(n):
if n < 2: return False
if n < 4: return True
if n % 2 == 0 or n % 3 == 0: return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0: return False
i += 6
return True
def has_prime_permutation(n):
"""Check if any permutation of digits of n is prime."""
digits = str(n)
for perm in set(permutations(digits)):
if perm[0] != '0':
num = int(''.join(perm))
if is_prime(num):
return True
return False
def solve(N=1000):
count = 0
for n in range(2, N + 1):
if has_prime_permutation(n):
count += 1
return count
# Test
assert has_prime_permutation(13) # 13 and 31 are both prime
assert has_prime_permutation(23) # 23 is prime
answer = solve(1000)
print(f"Friend numbers up to 1000: {answer}")