# Super ugly number — solve with heap search

Time：2020-2-20

Easy to find with heap sorting  Prime numbers are also called prime numbers. A natural number greater than 1, except for 1 and itself, is called a prime number; otherwise, it is called a composite number.
Prime factor (prime factor or prime factor) in number theory refers to the prime number that can divide a given positive integer. Except for 1, two positive integers without other common prime factors are called coprime. Because 1 has no prime factor, 1 is coprime with any positive integer (including 1 itself)
A number containing only quality factors 2, 3 and 5 is called ugly number. For example, 6 and 8 are ugly numbers, but 7.14 is not, because they contain quality factor 7. We used to think of 1 as the first ugly number

``````class Ugly {
constructor (n, primes) {
this.n = n
this.primes = new Heap(primes)
}
getAll () {
//Super ugly list
let res = 
let i = 2
let primes = this.primes
//Don't know upper limit use while loop
while (res.length < this.n) {
let arr = Ugly.getPrimies(i)
let k = 0
let l = arr.length
for (; k < l; k++) {
if (!primes.find(arr[k])) {
break
}
}
//There are two cases: 1. There is no prime factor at all, 2. The prime factors are all in the specified list
if (k === l) {
if (l === 0) {
if (primes.find(i)) {
res.push(i)
}
} else {
res.push(i)
}
}
i++
}
//Returns an array of ugly numbers
return res[this.n - 1]
}
//Calculate the prime factor of the specified positive integer n
static getPrimies (n) {
let prime = (n) => {
let arr = []
for (let i = 2; i < n / 2 + 1; i++) {
//Recursion is used to find the prime number, because the returned array is an arr array. When the array is empty, it means it is a prime number
if (n % i === 0 && !prime(i).length) {
arr.push(i)
}
}
return arr
}
return prime(n)
}
}

class Heap {
constructor (arr) {
this.data = arr
this.max = arr.length
this.sort()
}
sort () {
let iArr = this.data
let n = iArr.length
if (n <= 1) {
return iArr
} else {
//The loop is to traverse every possible node to be adjusted, and the internal recursion of maxheap is to recover the damaged heap
for (let i = Math.floor(n / 2); i >= 0; i--) {
Heap.maxHeapify(iArr, i, n)
}
return iArr
}
}
find (val, i = 0) {
let arr = this.data
if (val > arr[i] || i > this.max) {
return false
} else if (val === arr[i]) {
return val
} else {
return this.find(val, i * 2 + 1) || this.find(val, i * 2 + 2)
}
}
static swap (arr, a, b) {
if (a === b) {
return ''
}
// exchange
let c = arr[a]
arr[a] = arr[b]
arr[b] = c
}
//Build maximum heap
static maxHeapify (Arr, i, size) {
// left node
let l = i * 2 + 1
// right node
let r = i * 2 + 2
let largest = i
//Compare the parent node and the left node l to get the maximum
if (l <= size && Arr[l] > Arr[largest]) {
largest = l
}
// right node额最大值比较
if (r <= size && Arr[r] > Arr[largest]) {
largest = r
}
if (largest !== i) {
Heap.swap(Arr, i, largest)
Heap.maxHeapify(Arr, largest, size)
}
}
}

export default Ugly
export {
Heap
}``````

## [Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat: 127.0.0.1:6379> keys * 1) “kkk” 2) “key1” 127.0.0.1:6379> randomkey “key1” 127.0.0.1:6379> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]