-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
114 lines (99 loc) · 2.87 KB
/
main.ts
File metadata and controls
114 lines (99 loc) · 2.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function sieveOfEratosthenes(n: number): number[] {
const root = Math.sqrt(n)
const notPrimes: boolean[] = []
for (let i = 0; i < n; i++) {
notPrimes.push(false)
}
notPrimes[0] = true
for (let i = 2; i <= root; i++) {
let factor = 2 * i
while (factor <= n) {
notPrimes[factor - 1] = true
factor += i
}
}
const primes: number[] = []
for (let i = 0; i < notPrimes.length; i++) {
if (!notPrimes[i]) {
primes.push(i + 1)
}
}
return primes
}
function generatePattern(num: number): string[] {
const patterns: string[] = []
const digits: Map<string, number> = new Map()
const numStr = Math.abs(num).toString()
for (const char of numStr) {
if (digits.has(char)) {
digits.set(char, digits.get(char)! + 1)
} else {
digits.set(char, 1)
}
}
digits.forEach((value, key) => {
for (let i = 1; i < (1 << value); i++) {
let pattern = ""
let bitIndex = 0
for (let j = 0; j < numStr.length; j++) {
if (numStr[j] != key) {
pattern = pattern + numStr[j]
} else {
if ((i & (1 << bitIndex)) != 0) {
pattern = pattern + "*"
} else {
pattern = pattern + numStr[j]
}
bitIndex++
}
}
patterns.push(pattern)
}
})
return patterns
}
interface PatternValue {
Count: number
Creator: number
}
const n = 1_000_000
const primes = sieveOfEratosthenes(n)
const primesSet: Set<number> = new Set()
for (let i = 0; i < n; i++) {
primesSet.add(primes[i])
}
const start = performance.now()
const families = new Map<string, PatternValue>();
for (let i = 1; i < primes.length; i++) {
const patterns = generatePattern(primes[i])
for (const pattern of patterns) {
if (families.has(pattern)) {
const patternValue = families.get(pattern)!
patternValue.Count++
families.set(pattern, patternValue)
} else {
const patternValue = {
Count: 1,
Creator: primes[i]
} as PatternValue
families.set(pattern, patternValue)
}
}
}
interface MinKeyValue {
Key: string
Value: PatternValue
}
let min8ValueFamily: MinKeyValue = { Key: "", Value: { Count: 0, Creator: Infinity } }
families.forEach((value, key) => {
if (value.Count == 8) {
if (value.Count < min8ValueFamily.Value.Creator) {
min8ValueFamily.Key = key
min8ValueFamily.Value = value
}
}
})
const end = performance.now()
const elapsed = end - start
console.log("minimum 8 value family = ", min8ValueFamily)
console.log(`(elapsed=${elapsed}ms)`)