# euler-totient / euler_totient.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``` ```/* ** Euler's Totient Solver in Go ** Copyright (c) 2009 Aaron Toth ** ** Just an experiment in Go, ported from a terrible ** C program I made for a class 1 year ago. ** ** Reference: http://en.wikipedia.org/wiki/Euler's_totient_function */ package main import ( "fmt"; "flag"; ) func rel_prime(i, m int) int { var common_divisors int = 0; for j := 2; j <= i; j++ { if (i%j == 0) && (m%j == 0) { common_divisors++ } } if common_divisors > 0 { return 0 } return 1; } func abs_int(x int) int { if x <= 0 { return -x } return x; } func euler_totient(n int) int { var m int = abs_int(n); if (m == 0) || (m == 1) { return 1 } var totient int = 0; for i := 1; i < m; i++ { if rel_prime(i, m) == 1 { totient++ } } return totient; } func main() { var n *int = flag.Int("n", 0, "an integer value to calculate the number of positive integers less than or equal to n that are coprime to n"); flag.Parse(); fmt.Printf("The totient of %v is %v\n", *n, euler_totient(*n)); } ```