Advertisement
xah

0008 - Project Euler

xah
Oct 25th, 2021 (edited)
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.61 KB | None | 0 0
  1. // 0008.go contains some functions to solve ProjectEuler.net's Problem #8
  2. // https://projecteuler.net/problem=8
  3. package main
  4.  
  5. import (
  6.     "fmt"
  7.     "strings"
  8.     "strconv"
  9. )
  10.  
  11. // StringToIntArray converts a number string to an int array, assuming all
  12. // elements in the string array were numbers to begin with.
  13. func StringToIntArray(numberString string) []int {
  14.     // Create int array
  15.     intArray := make([]int, len(numberString))
  16.     // Strip whitespace in numberString and convert to string array
  17.     stringArray := strings.Split(strings.Join(strings.Fields(numberString), ""), "")
  18.  
  19.     for i, s := range stringArray {
  20.         // Convert string to int and add to int array
  21.         intArray[i], _ = strconv.Atoi(s)
  22.     }
  23.  
  24.     // Return result
  25.     return intArray
  26. }
  27.  
  28. // AdjacentProduct returns the highest product in an array given the number
  29. // of consecutive digits and the int array.
  30. func AdjacentProduct(digits int, intArray []int) int {
  31.     // Initialise variables
  32.     maxProduct := 0
  33.     length := len(intArray)-digits
  34.     var currentProduct int
  35.  
  36.     OUTER:
  37.     for i := 0; i<length; i++ {
  38.         currentProduct = 1
  39.  
  40.         for j := 0; j < digits; j++ {
  41.             if intArray[i+j] == 0 {
  42.                 // Anything * 0 is equal to 0, and irrelevant
  43.                 i += digits-1
  44.                 continue OUTER
  45.             }
  46.  
  47.             // Calculate product
  48.             currentProduct *= intArray[i+j]
  49.         }
  50.  
  51.         if currentProduct > maxProduct {
  52.             // Get highest possible product
  53.             maxProduct = currentProduct
  54.         }
  55.     }
  56.  
  57.     return maxProduct
  58. }
  59.  
  60. func main(){
  61.     // Answer the problem
  62.     number := `73167176531330624919225119674426574742355349194934
  63.     96983520312774506326239578318016984801869478851843
  64.     85861560789112949495459501737958331952853208805511
  65.     12540698747158523863050715693290963295227443043557
  66.     66896648950445244523161731856403098711121722383113
  67.     62229893423380308135336276614282806444486645238749
  68.     30358907296290491560440772390713810515859307960866
  69.     70172427121883998797908792274921901699720888093776
  70.     65727333001053367881220235421809751254540594752243
  71.     52584907711670556013604839586446706324415722155397
  72.     53697817977846174064955149290862569321978468622482
  73.     83972241375657056057490261407972968652414535100474
  74.     82166370484403199890008895243450658541227588666881
  75.     16427171479924442928230863465674813919123162824586
  76.     17866458359124566529476545682848912883142607690042
  77.     24219022671055626321111109370544217506941658960408
  78.     07198403850962455444362981230987879927244284909188
  79.     84580156166097919133875499200524063689912560717606
  80.     05886116467109405077541002256983155200055935729725
  81.     71636269561882670428252483600823257530420752963450`
  82.  
  83.     numberArray := StringToIntArray(number)
  84.  
  85.     fmt.Println(AdjacentProduct(13, numberArray))
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement