Skip to content

Instantly share code, notes, and snippets.

@asm-jaime
Created April 8, 2016 21:47
Show Gist options
  • Save asm-jaime/8a15f8170ac36b4ba22f0fe214229b2a to your computer and use it in GitHub Desktop.
Save asm-jaime/8a15f8170ac36b4ba22f0fe214229b2a to your computer and use it in GitHub Desktop.
golang, co-prime array
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var count int = 0
var rel_slice []string
func main() {
var buff int64
defer writer.Flush()
scanner.Scan()
scanner.Scan()
str_slice := strings.Split(scanner.Text(), " ")
for iter, char := range str_slice {
int_from_char, _ := strconv.ParseInt(char, 10, 64)
if iter == 0 {
rel_slice = append(rel_slice, strconv.FormatInt(int_from_char, 10))
buff = int_from_char
continue
}
if gcd(buff, int_from_char) != 1 {
for gcd(buff, int_from_char) != 1 {
buff++
}
count++
rel_slice = append(rel_slice, strconv.FormatInt(buff, 10), strconv.FormatInt(int_from_char, 10))
} else {
rel_slice = append(rel_slice, strconv.FormatInt(int_from_char, 10))
}
buff = int_from_char
}
writer.WriteString(strconv.Itoa(count) + "\n")
writer.WriteString(strings.Join(rel_slice, " "))
}
func gcd(x, y int64) int64 {
for y != 0 {
x, y = y, x%y
}
return x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment