Skip to content

Instantly share code, notes, and snippets.

@s-mage
Created August 1, 2013 18:04
Show Gist options
  • Save s-mage/6133723 to your computer and use it in GitHub Desktop.
Save s-mage/6133723 to your computer and use it in GitHub Desktop.
Problem 228 at project Euler. Find length of S_1864 + ... + S_1909, where S_[number] is [number]-sided polygon. http://projecteuler.net/problem=228
package main
import (
"math"
"fmt"
)
type Vertex struct {
x float64
y float64
}
type Shape []Vertex
const RADIAN_TO_DEGREE = 180 / math.Pi
// Create shape with given size. Shape is array of Vertices.
//
func CreateShape(size int) []Vertex {
result := make([]Vertex, size)
for i := 0; i < size; i++ {
angle := RADIAN_TO_DEGREE * float64(2 * (i + 1) * 180) / float64(size)
result[i] = Vertex { math.Cos(angle), math.Sin(angle) }
}
return result
}
// Check if array has given value.
//
func (shape Shape) HasValue(value Vertex) bool {
for _, v := range shape {
if v == value { return true }
}
return false
}
// Find new shape as sum of two given.
//
func Sum(a, b []Vertex) []Vertex {
result := make([]Vertex, len(a) * len(b))
columnSize := len(a)
for aIndex, aValue := range a {
for bIndex, bValue := range b {
x := aValue.x + bValue.x
y := aValue.y + bValue.y
result[aIndex * columnSize + bIndex] = Vertex { x, y }
}
}
return Shape(result).Uniq()
}
// Uniq given array.
//
func (shape Shape) Uniq() Shape {
result := make([]Vertex, 0)
for _, value := range shape {
if !Shape(result).HasValue(value) { result = append(result, value) }
}
return result
}
func main() {
left, right := 1864, 1909
result := CreateShape(left)
for left < right {
left += 1
shape := CreateShape(left)
result = append(result, shape...)
}
fmt.Println(len(Shape(result).Uniq()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment