Pages

Showing posts with label strconv. Show all posts
Showing posts with label strconv. Show all posts

Wednesday, July 13, 2011

Problem #4

Problem link
Solution:
package main

import (
"strconv"
)

func isPalindrome(input string) bool {
for i := 0; i < len(input)/2; i++ {
if input[i] != input[len(input)-1-i] {
return false
}
}
return true
}

func main() {
max, mul := 0, 0
for i := 100; i < 1000; i++ {
for j := i; j < 1000; j++ {
mul = i * j
if isPalindrome(strconv.Itoa(mul)) && mul > max {
max = mul
}
}
}
println(max)
}



Result: 906609
Time: 0m0.304s

Problem #8

Problem link
Solution:
package main

import "strconv"

func main() {
number := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
largestPro := 0
for i := 0; i < len(number)-5; i++ {
localPro := 1
for j := i; j < i+5; j++ {
num, _ := strconv.Atoi(string(number[j]))
localPro *= num
}
if localPro > largestPro {
largestPro = localPro
}
}
println(largestPro)
}



Result: 40824
Time: 0m0.008s

Problem #11

Problem link
Solution:
package main

import (
"strconv"
"io/ioutil"
"strings"
)

func main() {
fileBuf, err := ioutil.ReadFile("011_input.txt")
if err != nil {
panic(err.String())
}
fileStr := string(fileBuf)
lines := strings.Split(fileStr, "\n", 20)
arr := make([][]uint, 20)
var strArr []string
for i := range lines {
arr[i] = make([]uint, 20)
strArr = strings.Split(lines[i], " ", 20)
for j := range arr[i] {
intvalue, _ := strconv.Atoi(strArr[j])
arr[i][j] = uint(intvalue)
}
}
max := uint(0)
//left to right
for i := range arr {
for j := 0; j < 17; j++ {
mul := arr[i][j] * arr[i][j+1] * arr[i][j+2] * arr[i][j+3]
if mul > max {
max = mul
}
}
}
//up to down
for i := 0; i < 17; i++ {
for j := range arr[i] {
mul := arr[i][j] * arr[i+1][j] * arr[i+2][j] * arr[i+3][j]
if mul > max {
max = mul
}
}
}
//first diagonal
for i := 0; i < 17; i++ {
for j := 0; j < 17; j++ {
mul := arr[i][j] * arr[i+1][j+1] * arr[i+2][j+2] * arr[i+3][j+3]
if mul > max {
max = mul
}
}
}
//second diagonal
for i := 3; i < 20; i++ {
for j := 0; j < 17; j++ {
mul := arr[i][j] * arr[i-1][j+1] * arr[i-2][j+2] * arr[i-3][j+3]
if mul > max {
max = mul
}
}
}
println(max)
}


Input file is renamed as 011_input.txt.

Result: 70600674
Time: 0m0.005s

Problem #13

Problem link
Solution:
package main

import (
"io/ioutil"
"strconv"
"strings"
)

func main() {
fileBuf, err := ioutil.ReadFile("013_input.txt")
if err != nil {
panic(err.String())
}
fileStr := string(fileBuf)
lines := strings.Split(fileStr, "\n", 100)
result := ""
carryout, digit := 0, 0
for i := 49; i >= 0; i-- {
sum := carryout
for j := range lines {
num, _ := strconv.Atoi(string(lines[j][i]))
sum += num
}
digit = sum % 10
carryout = sum / 10
result = strconv.Itoa(digit) + result
}
result = strconv.Itoa(carryout) + result
println(result[0:10])
}



Result: 5537376230
Time: 0m0.010s

Problem #16

Problem link
Solution:
package main

import (
"strconv"
)

func main() {
input := "2"
for i := 0; i < 999; i++ {
input = multipyByTwo(input)
}
println(sumUpDigits(input))
}

func sumUpDigits(input string) int {
result, digit := 0, 0
for i := range input {
digit, _ = strconv.Atoi(string(input[i]))
result += digit
}
return result
}

func multipyByTwo(input string) string {
result, carryout, digit := "", 0, 0
for i := len(input) - 1; i >= 0; i-- {
digit, _ = strconv.Atoi(string(input[i]))
digit = digit*2 + carryout
carryout = digit / 10
digit = digit % 10
result = strconv.Itoa(digit) + result
}
if carryout > 0 {
result = strconv.Itoa(carryout) + result
}
return result
}



Result: 1366
Time: 0m0.228s

Problem #18

Problem link
Solution:
package main

import (
"io/ioutil"
"strconv"
"strings"
)

var arr [][]int
var bests [][]int

func main() {
fileBuf, err := ioutil.ReadFile("018_input.txt")
if err != nil {
panic(err.String())
}
fileStr := strings.Trim(string(fileBuf), "")
oneDArrStr := strings.Split(fileStr, "\n", -1)
var line []string
arr = make([][]int, 15)
bests = make([][]int, len(arr))
for i := range arr {
line = strings.Split(oneDArrStr[i], " ", -1)
arr[i] = make([]int, len(line))
bests[i] = make([]int, len(arr[i]))
for j := range line {
arr[i][j], _ = strconv.Atoi(line[j])
bests[i][j] = -1
}
}
println(bestSum(0, 0))
}

func bestSum(i, j int) int {
var result int
if bests[i][j] != -1 {
result = bests[i][j]
} else if i == len(arr)-1 {
result = arr[i][j]
return arr[i][j]
} else {
sumLeft, sumRight := bestSum(i+1, j), bestSum(i+1, j+1)
if sumLeft > sumRight {
result = arr[i][j] + sumLeft
} else {
result = arr[i][j] + sumRight
}
}
bests[i][j] = result
return result
}


Tree is read from a file named as 018_input.txt and also memoization is used for this problem.

Result: 1074
Time: 0m0.005s

Problem #20

Problem link
Solution:
package main

import (
"big"
"strconv"
)

func main() {
mul := big.NewInt(1)
for i := 2; i <= 100; i++ {
mul.Mul(mul, big.NewInt(int64(i)))
}
result, digit := 0, 0
for i := range mul.String() {
digit, _ = strconv.Atoi(string(mul.String()[i]))
result += digit
}
println(result)
}



Result: 648
Time: 0m0.023s

Problem #32

Problem link
Solution:
package main

import (
"strconv"
"strings"
)

func main() {
mp := map[int]int{}
for i := 1; i < 200; i++ {
for j := 1; j < 5000; j++ {
if isPandigital(i, j, i*j) {
mp[i*j] = 1
}
}
}
sum := 0
for key, _ := range mp {
sum += key
}
println(sum)
}

func isPandigital(mul1, mul2, result int) bool {
str := strconv.Itoa(mul1) + strconv.Itoa(mul2) + strconv.Itoa(result)
if len(str) != 9 {
return false
}
for i := 1; i < 10; i++ {
if !strings.Contains(str, strconv.Itoa(i)) {
return false
}
}
return true
}



Result: 45228
Time: 0m2.405s

Problem #35

Problem link
Solution:
package main

import (
"strconv"
)

func main() {
arr := make([]bool, 1000000)
arr[1] = true
prime := 3
count := 13
var k, tmp, localCount int
var str string
primeloop:
for {
for k = prime * 2; 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
str = strconv.Itoa(prime)
if prime > 100 {
localCount = 1
for i := 0; i < len(str)-1; i++ {
str = str[1:] + str[0:1]
tmp, _ = strconv.Atoi(str)
if tmp > prime {
continue primeloop
} else if !arr[tmp] && tmp%2 != 0 {
localCount++
} else {
continue primeloop
}
}
count += localCount
}
} else {
break
}
}
println(count)
}



Result: 55
Time: 0m0.135s

Problem #36

Problem link
Solution:
package main

import (
"strconv"
)

func main() {
sum := 0
for i := 0; i < 1000000; i++ {
if isPalindrome(strconv.Itoa(i)) {
if isPalindrome(strconv.Itob(i, 2)) {
sum += i
}
}
}
println(sum)
}

func isPalindrome(in string) bool {
for i := 0; i < len(in); i++ {
if in[i] != in[len(in)-i-1] {
return false
}
}
return true
}



Result: 872187
Time: 0m0.801s

Problem #37

Problem link
Solution:
package main

import (
"strconv"
)

func main() {
arr := make([]bool, 1000000)
left := make([]bool, 1000000)
right := make([]bool, 1000000)

//manuel setting for the values less than 10
left[2], left[3], left[5], left[7] = true, true, true, true
right[2], right[3], right[5], right[7] = true, true, true, true
arr[0], arr[1], arr[6], arr[9] = true, true, true, true
a := []int{3, 5, 7}
for i := range a {
for k := a[i] * 2; k < len(arr); k += a[i] {
arr[k] = true
}
}

//calculate other primes and check the condition.
var k, tmp int
prime, counter, sum := 11, 0, 0
for {
if right[prime/10] {
right[prime] = true
}
tmp, _ = strconv.Atoi(strconv.Itoa(prime)[1:])
if left[tmp] {
left[prime] = true
if right[prime] {
sum += prime
counter++
if counter == 11 {
println(sum)
return
}
}
}
for k = prime * 2; 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
} else {
break
}
}
}



Result: 748317
Time: 0m0.083s

Problem #38

Problem link
Solution:
package main

import (
"strconv"
"strings"
)

func main() {
max := 0
var tmp int
for i := 1; i < 10000; i++ {
str := ""
for j := 1; j < 9 && len(str) < 9; j++ {
str += strconv.Itoa(i * j)
}
if len(str) == 9 && isPandigital(str) {
tmp, _ = strconv.Atoi(str)
if tmp > max {
max = tmp
}
}
}
println(max)
}

func isPandigital(str string) bool {
if len(str) != 9 {
return false
}
for i := 1; i < 10; i++ {
if !strings.Contains(str, strconv.Itoa(i)) {
return false
}
}
return true
}



Result: 932718654
Time: 0m0.049s

Problem #40

Problem link
Solution:
package main

import (
"strconv"
)

func main() {
i, count, limit, mul := 1, 0, 10, 1
var tmp int
for limit < 1000001 {
str := strconv.Itoa(i)
count += len(str)
if count >= limit {
tmp, _ = strconv.Atoi(str[len(str)-(count-limit)-1 : len(str)-(count-limit)])
mul *= tmp
limit *= 10
}
i++
}
println(mul)
}



Result: 210
Time: 0m0.153s

Problem #41

Problem link
Solution:
package main

import (
"strconv"
"strings"
)

func main() {
a := make([]bool, 87654322)
a[0], a[1] = true, true
prime := 3
var k int
finished := false
for !finished {
for k = 2 * prime; k < len(a); k += prime {
a[k] = true
}
for k = prime + 2; k < len(a) && a[k]; k += 2 {
}
if k < len(a) {
prime = k
} else {
finished = true
}
}
//a now has false values for the primes and multiples of 2
//but we skip even numbers in our iteration.
for i := int64(87654321); i > 0; i -= 2 {
if !a[i] && isPandigital(i) {
println(i)
return
}
}
}

func isPandigital(in int64) bool {
str := strconv.Itoa64(in)
n := len(str)
for i := 1; i <= n; i++ {
if !strings.Contains(str, strconv.Itoa(i)) {
return false
}
}
return true
}



Result: 7652413
Time: 0m15.596s

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

Problem link
Solution:
package main

import (
"strings"
"strconv"
)

func main() {
arr := make([]bool, 10000)
prime := 2
finished := false
var i int
for !finished {
for i = 2 * prime; i < 10000; i += prime {
arr[i] = true
}
//next prime
for i = prime + 1; i < 10000 && arr[i]; i++ {
}
if i < 10000 {
prime = i
} else {
finished = true
}
}
outer:
for i := 1000; i < len(arr); i++ {
for j := i + 1; j < len(arr); j++ {
if i != 1487 && !arr[i] && !arr[j] && isPerm(i, j) && 2*j-i < 10000 && !arr[2*j-i] && isPerm(i, 2*j-i) {
print(i)
print(j)
print(2*j - i)
break outer
}
}
}
println()
}

func isPerm(i1, i2 int) bool {
s1, s2 := strconv.Itoa(i1), strconv.Itoa(i2)
for i := 0; i < len(s1); i++ {
//we need a cross check for repetition of numbers like in 1049 1499 comparison
if !strings.Contains(s1, s2[i:i+1]) || !strings.Contains(s2, s1[i:i+1]) {
return false
}
}
return true
}



Result: 296962999629
Time: 0m0.420s