-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler.go
114 lines (100 loc) · 2.15 KB
/
euler.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
108
109
110
111
112
113
114
package main
import (
"fmt"
"math"
"sync"
)
const limit = 10000
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}
// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func Filter(in <-chan int, out chan<- int, prime int) {
for {
i := <-in // Receive value from 'in'.
if i%prime != 0 {
out <- i // Send 'i' to 'out'.
}
}
}
// The prime sieve: Daisy-chain Filter processes.
func Sieve(limit int, result map[int]bool) {
ch := make(chan int) // Create a new channel.
go Generate(ch) // Launch Generate goroutine.
for {
prime := <-ch
if prime > limit {
break
}
result[prime] = false
ch1 := make(chan int)
go Filter(ch, ch1, prime)
ch = ch1
}
}
func PrimeFactor(t int, primes map[int]bool, result map[float64]float64) {
for prime := range primes {
for t%prime == 0 {
t = t / prime
result[float64(prime)]++
}
if t <= 1 {
return
}
}
}
func EulerTotient(t int, primes map[int]bool, data *float64, wg *sync.WaitGroup, mutex *sync.Mutex) {
defer wg.Done()
result := make(map[float64]float64)
PrimeFactor(t, primes, result)
ans := 1.0
for k, v := range result {
ans *= (k - 1.0) * math.Pow(k, (v-1.0))
}
mutex.Lock()
*data += ans
mutex.Unlock()
}
func Euler72() {
limit := 1000000
// =============================================
// Euler 72
//
// Brute-Force to Find EulerTotient, too slow
//primes := make(map[int]bool)
//Sieve(limit, primes)
//ans := 0.0
//var wg sync.WaitGroup
//var mutex = &sync.Mutex{}
//for i := 2; i <= limit; i++ {
// wg.Add(1)
// EulerTotient(i, primes, &ans, &wg, mutex)
//}
//wg.Wait()
//fmt.Println(ans)
// https://projecteuler.net/overview=072
phis := make([]float64, limit+1)
for n := 2; n <= limit; n++ {
phis[n] = float64(n)
}
for n := 2; n <= limit; n++ {
if phis[n] == float64(n) {
for m := n; m <= limit; m += n {
phis[m] = phis[m] - phis[m]/float64(n)
}
}
}
ans2 := 0.0
for _, v := range phis {
ans2 += v
}
fmt.Println(ans2)
// =============================================
}
func main() {
Euler72()
}