/
main.go
109 lines (87 loc) · 2.07 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
108
109
package main
import (
"log"
"math"
"os"
"sort"
"strconv"
"github.com/TomasCruz/projecteuler"
)
/*
Problem 70;
Euler's totient function, phi(n) [sometimes called the phi function], is used to determine the number of positive numbers
less than or equal to n which are relatively prime to n. For example, as 1, 2, 4, 5, 7 and 8, are all less than or equal
to nine and relatively prime to nine, phi(9) = 6.
The number 1 is considered to be relatively prime to every positive number, so phi(1) = 1.
Interestingly, phi(87109) = 79180, and it can be seen that 87109 is a permutation of 79180.
Find the value of n, 1 < n < 10^7, for which phi(n) is a permutation of n and the ratio n/phi(n) produces a minimum.
*/
func main() {
var limit int
if len(os.Args) > 1 {
limit64, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
log.Fatal("bad argument")
}
limit = int(limit64)
} else {
limit = 10000000
}
projecteuler.Timed(calc, limit)
}
func calc(args ...interface{}) (result string, err error) {
limit := args[0].(int)
primes := projecteuler.Primes(limit, nil)
minPrimeIndex := 20
for ; primes[minPrimeIndex] < 1000; minPrimeIndex++ {
}
lenPrimes := len(primes)
minX := 0
minRatio := math.MaxFloat64
for i := minPrimeIndex; i < lenPrimes; i++ {
for j := i + 1; j < lenPrimes; j++ {
x := primes[i] * primes[j]
if x >= limit {
break
}
tot := totient(i, j, primes)
if permutation(x, tot) {
ratio := float64(x) / float64(tot)
if ratio < minRatio {
minX = x
minRatio = ratio
}
}
}
}
result = strconv.Itoa(minX)
return
}
func totient(i, j int, primes []int) int {
return (primes[i] - 1) * (primes[j] - 1)
}
func permutation(a, b int) bool {
aDigits := []int{}
for a > 0 {
aDigits = append(aDigits, a%10)
a /= 10
}
aLen := len(aDigits)
bDigits := []int{}
for b > 0 {
bDigits = append(bDigits, b%10)
b /= 10
}
bLen := len(bDigits)
if aLen != bLen {
return false
}
sort.Ints(aDigits)
sort.Ints(bDigits)
for i := 0; i < aLen; i++ {
if aDigits[i] != bDigits[i] {
return false
}
}
return true
}