-
Notifications
You must be signed in to change notification settings - Fork 27
/
10006.go
52 lines (46 loc) · 800 Bytes
/
10006.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
// UVa 10006 - Carmichael Numbers
package main
import (
"fmt"
"math/big"
"os"
)
func isPrime(n int64) bool {
if n%2 == 0 {
return false
}
for i := int64(3); i*i <= n; i += 2 {
if n%i == 0 {
return false
}
}
return true
}
func isCarmichael(n int64) bool {
var b, p big.Int
e := big.NewInt(n)
for a := int64(2); a < n; a++ {
b.SetInt64(a)
if p.Exp(&b, e, e).Sub(&p, &b); p.Int64() != 0 {
return false
}
}
return !isPrime(n)
}
func main() {
in, _ := os.Open("10006.in")
defer in.Close()
out, _ := os.Create("10006.out")
defer out.Close()
var n int64
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
if isCarmichael(n) {
fmt.Fprintf(out, "The number %d is a Carmichael number.\n", n)
} else {
fmt.Fprintf(out, "%d is normal.\n", n)
}
}
}