-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem41.go
44 lines (39 loc) · 889 Bytes
/
problem41.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
/*
Problem 41:
We shall say that an -digit number is pandigital if it makes use of all the digits to exactly once. For example, is a -digit pandigital and is also prime. What is the largest -digit pandigital prime that exists?
*/
package main
import (
"fmt"
"math"
"strconv"
"strings"
)
func isPrime(num int) (res bool) {
for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
if num%i == 0 {
return
}
}
return true
}
// 9 and 8 digit numbers are always divisible by 3 so we check 7 digit numbers are the biggest numbers.
func isPandigital(num int) (res bool) {
var x []string = []string{"1", "2", "3", "4", "5", "6", "7"}
for _, e := range x {
if !strings.Contains(strconv.Itoa(num), e) {
return false
}
}
return true
}
func main() {
for i := 7654321; i >= 1234567; i-- {
if isPandigital(i) {
if isPrime((i)) {
fmt.Println(i)
break
}
}
}
}