Skip to content

Commit

Permalink
Euler 12 Done.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasnokleby committed Feb 20, 2014
1 parent 36ee78c commit bbe74a5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/main/go/src/Euler12.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"mathlib"
//"time"
)

func main() {
done := false
factorCount := 0
maxFactor := 0
i := 1
for !done {
factors := mathlib.AllFactors(generateTriangleNumber(i))
factorCount = len(factors)
if factorCount > maxFactor {
maxFactor = factorCount
}
if factorCount > 500 {
done = true
} else {
if i%1000 == 0 {
fmt.Println("F: ",i, len(factors), maxFactor)
}
i++
}
}

fmt.Printf("The %v triangle number has > 500 factors and has a value of %v \n",i,generateTriangleNumber(i))
}

func generateTriangleNumber(base int) int {
triangleNumber := 0
for i:=1; i<=base;i++{
triangleNumber += i
}
return triangleNumber
}

17 changes: 15 additions & 2 deletions src/main/go/src/mathlib/mathlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mathlib
import (
"math"
// "strconv"
// "fmt"
"time"
"fmt"
)
Expand Down Expand Up @@ -33,13 +32,19 @@ func IsPrime(number int) (bool) {
func AllFactors (number int) ([]int) {
var factors []int

sqrt := int( math.Sqrt( float64(number) ) )
sqrt := int(math.Ceil(math.Sqrt(float64(number))))

factors = append(factors, 1) //Per def 1 is a prime factor of whatever number.

for i := 2 ; i <= sqrt ; i++ {
if ( math.Mod(float64(number), float64(i)) < 0.00001) {
factors = append(factors, i)
factors = append(factors, (number/i))
}
}
factors = append(factors, number) //Per def number is a prime factor of number.
//TODO: Remove duplicates

return factors
}

Expand Down Expand Up @@ -94,6 +99,14 @@ func IsPytagoreanTriplet(a int, b int, c int) bool {
return (aSquared + bSquared == cSquared)
}

func GenerateTriangleNumber(base int) int {
triangleNumber := 0
for i:=1; i<=base;i++{
triangleNumber += i
}
return triangleNumber
}

func Sieve(size int) []int {
base := time.Now()
//create a slice of ints
Expand Down
18 changes: 17 additions & 1 deletion src/main/go/src/mathlib/mathlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mathlib

import (
"testing"
// "fmt"
//"fmt"
)

func TestIsPrime(t *testing.T) {
Expand Down Expand Up @@ -63,7 +63,23 @@ func TestSieve(t *testing.T){
if (len(primes) == 1) {
t.Errorf("Error")
}
}

func TestAllFactors(t *testing.T) {
aTriangleNumber := GenerateTriangleNumber(12375)
allFactorsForThat := AllFactors(aTriangleNumber)

if (len(allFactorsForThat) != 576) {
t.Errorf("Error: Allfactors says number of factors is %v while it should be 576",len(allFactorsForThat))
}
}
func TestAllFactors2(t *testing.T) {
aTriangleNumber := GenerateTriangleNumber(7)
allFactorsForThat := AllFactors(aTriangleNumber)

if (len(allFactorsForThat) != 6) {
t.Errorf("Error: Allfactors says number of factors is %v while it should be 6",len(allFactorsForThat))
}
}


0 comments on commit bbe74a5

Please sign in to comment.