Created
January 5, 2015 14:28
-
-
Save dazfuller/f1242429ef5a1f4b99b9 to your computer and use it in GitHub Desktop.
Euler Problem 43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func to_integer(slice []int) int64 { | |
k := int64(0) | |
for i := 0; i < len(slice); i++ { | |
k = int64(10)*k + int64(slice[i]) | |
} | |
return k | |
} | |
func nextPermutation(slice []int) bool { | |
var k int | |
for k = len(slice) - 2; k >= 0; k-- { | |
if slice[k] < slice[k+1] { | |
break | |
} | |
} | |
if k == -1 { | |
return false | |
} | |
var l int | |
for l = len(slice) - 1; l >= 0; l-- { | |
if slice[k] < slice[l] { | |
break | |
} | |
} | |
slice[k], slice[l] = slice[l], slice[k] | |
for i, j := k+1, len(slice)-1; i < j; i, j = i+1, j-1 { | |
slice[i], slice[j] = slice[j], slice[i] | |
} | |
return true | |
} | |
func checkDivisibility(d []int) bool { | |
if to_integer(d[1:4])%2 != 0 { | |
return false | |
} | |
if to_integer(d[2:5])%3 != 0 { | |
return false | |
} | |
if to_integer(d[3:6])%5 != 0 { | |
return false | |
} | |
if to_integer(d[4:7])%7 != 0 { | |
return false | |
} | |
if to_integer(d[5:8])%11 != 0 { | |
return false | |
} | |
if to_integer(d[6:9])%13 != 0 { | |
return false | |
} | |
if to_integer(d[7:10])%17 != 0 { | |
return false | |
} | |
return true | |
} | |
func main() { | |
d := []int{1, 0, 2, 3, 4, 5, 6, 7, 8, 9} | |
t0 := time.Now() | |
sum := int64(0) | |
if checkDivisibility(d) { | |
sum += to_integer(d) | |
} | |
for nextPermutation(d) { | |
if checkDivisibility(d) { | |
sum += to_integer(d) | |
} | |
} | |
t1 := time.Now() | |
fmt.Println(sum) | |
fmt.Printf("Time taken: %v\n", t1.Sub(t0)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment