-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (77 loc) · 1.84 KB
/
main.go
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
package main
import (
"fmt"
"time"
)
/*** * * ***/
// factorizeUtil takes an integer and returns a slice of its factors
func factorizeUtil(n int) []int {
var factors []int
for i := 2; i <= n; i++ {
for n%i == 0 {
factors = append(factors, i)
n /= i
}
}
return factors
}
// measureTimeUtil is a higher-order function that measures the time it takes to run any function.
func measureTimeUtil(fn func()) time.Duration {
start := time.Now()
fn() // Call the function
return time.Since(start) // Return the elapsed time
}
/*** * * ***/
func generateHammingNums(x int) []int {
hammingNumbers := []int{1} // Start with the first Hamming number
for len(hammingNumbers) < x {
currHamming := hammingNumbers[len(hammingNumbers)-1]
nextHamming := nextHammingNum(currHamming)
hammingNumbers = append(hammingNumbers, nextHamming)
}
return hammingNumbers
}
// nextHammingNum finds the next Hamming number
func nextHammingNum(x int) int {
for {
x++
if isHammingNum(x) {
return x
}
}
}
// nextNotHammingNum finds the next NOT Hamming number
func nextNotHammingNum(x int) int {
for {
x++
if !isHammingNum(x) {
return x
}
}
}
// isHammingNum checks if a number is a Hamming number
func isHammingNum(n int) bool {
for n%2 == 0 {
n /= 2
}
for n%3 == 0 {
n /= 3
}
for n%5 == 0 {
n /= 5
}
return n == 1
}
/*** * * ***/
func main() {
idx := 1000
nums := generateHammingNums(idx)
/*** * * ***/
l := nums[idx-1] // last
nextNotL := nextNotHammingNum(l) // next not Hamming of last
tL := measureTimeUtil(func() { factorizeUtil(l) }) // time to factorize l
tNextNotL := measureTimeUtil(func() { factorizeUtil(nextNotL) }) // time to factorize nextNotL
/*** * * ***/
fmt.Printf("Time taken to factorize %d: %v\n", l, tL)
fmt.Printf("Time taken to factorize %d: %v\n", nextNotL, tNextNotL)
}