-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductSumNumbers.java
More file actions
66 lines (54 loc) · 2.6 KB
/
ProductSumNumbers.java
File metadata and controls
66 lines (54 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dev;
import java.util.*;
public class ProductSumNumbers {
private static Map<Integer, Integer> minProductSum = new HashMap<>();
private static final int MAX_K = 12000;
public static void main(String[] args) {
// Find all minimal product-sum numbers
findProductSums(2, 1, 0, 0);
// Calculate the sum of all unique minimal product-sum numbers
Set<Integer> uniqueNumbers = new HashSet<>(minProductSum.values());
long sum = uniqueNumbers.stream().mapToLong(Integer::intValue).sum();
System.out.println("Minimal product-sum numbers for k = 2 to " + MAX_K + ":");
// Display some examples for verification
for (int k = 2; k <= Math.min(20, MAX_K); k++) {
if (minProductSum.containsKey(k)) {
System.out.println("k = " + k + ": " + minProductSum.get(k));
}
}
System.out.println("\nNumber of unique minimal product-sum numbers: " + uniqueNumbers.size());
System.out.println("Sum of all minimal product-sum numbers for 2 ≤ k ≤ " + MAX_K + ": " + sum);
}
/**
* Recursively finds product-sum numbers using depth-first search
*
* @param factor current factor being considered (starts at 2)
* @param product current product of factors
* @param sum current sum of factors
* @param count number of factors used so far
*/
private static void findProductSums(int factor, long product, long sum, int count) {
// Calculate k: the set size needed for this product-sum number
// k = count + (product - sum)
// The (product - sum) represents the number of 1's needed
long k = count + product - sum;
// Skip if k is too large or if product becomes too large
if (k > MAX_K || product > 2 * MAX_K) {
return;
}
// If we have at least 2 factors and k is valid, record this as a candidate
if (count >= 2 && k >= 2) {
int kInt = (int) k;
int productInt = (int) product;
// Only update if this is the first time we see this k or if we found a smaller product
if (!minProductSum.containsKey(kInt) || minProductSum.get(kInt) > productInt) {
minProductSum.put(kInt, productInt);
}
}
// Continue the search by trying larger factors
// We start from the current factor to avoid duplicate combinations
for (int nextFactor = factor; nextFactor <= 2 * MAX_K / product; nextFactor++) {
findProductSums(nextFactor, product * nextFactor, sum + nextFactor, count + 1);
}
}
}