Pages

Showing posts with label factorization. Show all posts
Showing posts with label factorization. Show all posts

Wednesday, July 13, 2011

Problem #3

Problem link
Solution:
package main

func main() {
target := int64(600851475143)
arr := make([]bool, 10000)
prime := 3
var k int
for {
for k = 2 * prime; k < len(arr); k += prime {
arr[k] = true
}
for k = prime + 2; k < len(arr) && arr[k]; k += 2 {
}
if k < len(arr) {
prime = k
if target%int64(k) == 0 {
target = target / int64(k)
if target == 1 {
println(k)
return
}
}
} else {
break
//prevent infinite loop in case the answer
//is not less than 10000
}
}
}



Result: 6857
Time: 0m0.003s

Problem #5

Problem link
Solution:
package main

func main() {
primes := []int{2, 3, 5, 7, 11, 13, 17, 19}
result := 1
var divisor int
for i := range primes {
divisor = primes[i]
for divisor <= 20 {
divisor *= primes[i]
}
divisor /= primes[i]
result *= divisor
}
println(result)
}



Result: 232792560
Time: 0m0.003s

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 #43

Problem link
Solution:
package main

import (
"strconv"
)

var result int64
var divisors []int
var tmp int
var tmp64 int64

func main() {
result = int64(0)
divisors = []int{2, 3, 5, 7, 11, 13, 17}
allPerms("", "0123456789")
println(result)
}

func allPerms(pre, s string) {
if len(s) == 0 {
checkTheProperty(pre)
} else {
for i := 0; i < len(s); i++ {
allPerms(pre+s[i:i+1], s[0:i]+s[i+1:len(s)])
}
}
}

func checkTheProperty(s string) {
if s[0] == '0' {
return
} else {
for i := 1; i < 8; i++ {
if tmp, _ = strconv.Atoi(s[i : i+3]); tmp%divisors[i-1] != 0 {
return
}
}
}
tmp64, _ = strconv.Atoi64(s)
result += tmp64
}



Result: 16695334890
Time: 0m6.223s

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

Problem #47

Problem link
Solution:
package main

func main() {
arr := make([]bool, 1000000)
arr[0], arr[1] = true, true
count, prime := 2, 3
var k int
for {
for k = 2 * prime; k < len(arr); k += prime {
arr[k] = true
}
for k = prime + 2; k < len(arr) && arr[k]; k += 2 {
}
if k < len(arr) {
prime = k
count++
} else {
break
}
}
primes := make([]int, count)
primes[0] = 2
index := 1
for i := 3; i < len(arr); i += 2 {
if !arr[i] {
primes[index] = i
index++
}
}
var consecutiveCount, divisorCount, tmp int
outer:
for i := 646; i < 1000000; i++ {
consecutiveCount = 0
inner:
for j := i; j < i+4; j++ {
divisorCount = 0
//find divisors of j
if !arr[j] && j%2 != 0 {
//a quick check: if j is prime
continue outer
} else {
tmp = j
for k := 0; k < len(primes); k++ {
if (!arr[tmp] && tmp%2 != 0) || tmp%primes[k] == 0 {
divisorCount++
if divisorCount == 4 {
consecutiveCount++
if consecutiveCount == 4 {
println(i)
return
}
continue inner
} else if !arr[tmp] && tmp%2 != 0 {
//another quick check for prime tmp
continue outer
}
for tmp%primes[k] == 0 {
tmp /= primes[k]
}
}
}
}
}
}
}



Result: 134043
Time: 0m1.323s