Pages

Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Wednesday, July 13, 2011

Problem #9

Problem link
Solution:
package main

import "math"

func isSquare(num int) (bool, int) {
sqrt := int(math.Sqrt(float64(num)))
if sqrt*sqrt == num {
return true, sqrt
}
return false, -1
}

func main() {
loop:
for a := 1; a < 500; a++ {
for b := a; b < 500; b++ {
result, c := isSquare(a*a + b*b)
if result && a+b+c == 1000 {
println(a * b * c)
break loop
}
}
}
}



Result: 31875000
Time: 0m0.016s

Problem #12

Problem link
Solution:
package main

import (
"math"
)

func main() {
divisors := 1
triangleN := uint64(1)
for i := 2; divisors < 500; i++ {
triangleN += uint64(i)
divisors = noOfDivisors(triangleN)
}
println(triangleN)
}

func noOfDivisors(number uint64) int {
counter := 0
limit := uint64(math.Sqrt(float64(number)))
for i := 1; uint64(i) <= limit; i++ {
if number%uint64(i) == 0 {
counter += 2
}
}
return counter
}



Result: 76576500
Time: 0m1.010s

Problem #21

Problem link
Solution:
package main

import (
"math"
)

func main() {
sum, j := 0, 0
for i := 1; i < 10000; i++ {
j = sumOfProperDivisors(i)
if i == sumOfProperDivisors(j) && i != j {
sum += i
}
}
println(sum)
}

func sumOfProperDivisors(input int) int {
//checking until the square root is enough
//since a divisor less than the square root corresponds
//the other divisor greater than the square root
limit, sum := int(math.Sqrt(float64(input))), 0
for i := 1; i <= limit; i++ {
if input%i == 0 {
if i == input/i {
sum += i
} else {
sum += i + (input / i)
}
}
}
return sum - input
}



Result: 31626
Time: 0m0.023s

Problem #23

Problem link
Solution:
package main

import (
"math"
"container/list"
)

var writable []bool
var limit int
var abunList *list.List

func main() {
abunList = list.New()
limit = 28124
writable = make([]bool, limit)
for i := 1; i < limit; i++ {
writable[i] = false
if i < sumOfProperDivisors(i) {
abunList.PushBack(i)
}
}

checkWritables()

sum := 0
for i := 1; i < limit; i++ {
if !writable[i] {
sum += i
}
}
println(sum)
}

func checkWritables() {
for i := abunList.Front(); i != nil; i = i.Next() {
for j := i; j != nil; j = j.Next() {
if sum := i.Value.(int) + j.Value.(int); sum < limit {
writable[sum] = true
}
}
}
}

func sumOfProperDivisors(input int) int {
//checking until the square root is enough
//since a divisor less than the square root corresponds
//the other divisor greater than the square root
limit, sum := int(math.Sqrt(float64(input))), 0
for i := 1; i <= limit; i++ {
if input%i == 0 {
if i == input/i {
sum += i
} else {
sum += i + (input / i)
}
}
}
return sum - input
}



Result: 4179871
Time: 0m1.406s

Problem #27

Problem link
Solution:
package main

import (
"math"
)

func main() {
var maxCounter, aInMax, bInMax int
maxCounter = 0
for a := -999; a < 1000; a++ {
for b := -999; b < 1000; b++ {
inner:
for n := 0; ; n++ {
if !isPrime(n*n + a*n + b) {
if n > maxCounter {
maxCounter, aInMax, bInMax = n, a, b
}
break inner
}
}
}
}
println(aInMax * bInMax)
}

func isPrime(in int) bool {
limit := int(math.Sqrt(float64(in)))
if in < 2 || in%2 == 0 {
return false
} else {
for i := 2; i < limit; i++ {
if in%i == 0 {
return false
}
}
}
return true
}



Result: -59231
Time: 0m0.802s

Problem #46

Problem link
Solution:
package main

import (
"math"
)

func main() {
arr := make([]bool, 1000000)
arr[0], arr[1], arr[2] = false, false, true
i := 3
var composite, found bool
var tmp, tmp2 int
loop:
for {
composite = false
inner:
for j := 2; j < i; j++ {
if arr[j] && i%j == 0 {
composite = true
break inner
}
}
if composite {
found = false
inner2:
for j := 2; j < i; j++ {
if arr[j] {
tmp = (i - j)
if tmp2 = int(math.Sqrt(float64(tmp / 2))); tmp%2 == 0 && tmp2*tmp2 == tmp/2 {
found = true
break inner2
}
}
}
if !found {
println(i)
break loop
}
} else { //prime
arr[i] = true
}
i += 2
}
}



Result: 5777
Time: 0m0.043s