2017-09-22 11 views
1

Project Euler #145の解を作成しようとしています。私はGoに書いています。私のプログラムを実行すると、結果は125になります。予想される結果は120です。私は2つの異なる方法でコードを書こうとしましたが、どちらも同じ答えが出てきます。私の誤りを指摘するどんな助けもありがとう。プロジェクトオイラー#145のループの結果が多すぎます

Code option #1使用ストリング:

package main 

import (
    "fmt" 
    "strconv" 
) 

//checks to see if all the digits in the number are odd 
func is_Odd(sum int) bool { 
    intString := strconv.Itoa(sum) 

    for x := len(intString); x > 0; x-- { 
     newString := intString[x-1] 
     if newString%2 == 0 { 
      return false 
     } 
    } 
    return true 
} 

//reverse the number passed 
func reverse_int(value int) int { 

    intString := strconv.Itoa(value) 

    newString := "" 

    for x := len(intString); x > 0; x-- { 
     newString += string(intString[x-1]) 
    } 

    newInt, err := strconv.Atoi(newString) 

    if err != nil { 
     fmt.Println("Error converting string to int") 
    } 

    return newInt 
} 

//adds 2 int's passed to it and returns an int 
func add(x int, y int) int { 
    return x + y 
} 

func main() { 
    //functions test code 
    /*y := 35 
     x := reverse_int(y) 
     z := add(x,y) 
     fmt.Println(is_Odd(z))*/ 

    counter := 1 

    for i := 1; i < 1000; i++ { 
     flipped := reverse_int(i) 
     sum := add(flipped, i) 
     oddCheck := is_Odd(sum) 
     if oddCheck { 
      fmt.Println(counter, ":", i, "+", flipped, "=", sum) 
      counter++ 
     } 
    } 
    counter-- 
    fmt.Println("total = ", counter) 

} 

Code option #2使用のみint値:

package main 

import (
    "fmt" 
) 

var counter int 

//breaks down an int number by number and checks to see if 
//all the numbers in the int are odd 
func is_Odd(n int) bool { 

    for n > 0 { 
     remainder := n % 10 
     if remainder%2 == 0 { 
      return false 
     } 
     n /= 10 
    } 
    return true 
} 

//adds 2 int's passed to it and returns an int 
func add(x int, y int) int { 
    return x + y 
} 

//reverses the int passed to it and returns an int 
func reverse_int(n int) int { 
    var new_int int 
    for n > 0 { 
     remainder := n % 10 
     new_int *= 10 
     new_int += remainder 
     n /= 10 
    } 
    return new_int 
} 

func main() { 
    //functions test code 
    /*y := 35 
     x := reverse_int(y) 
     z := add(x,y) 
     fmt.Println(is_Odd(z))*/ 

    counter = 1 

    for i := 1; i < 1000; i++ { 
     flipped := reverse_int(i) 
     sum := add(flipped, i) 
     oddCheck := is_Odd(sum) 
     if oddCheck { 
      //fmt.Println(counter,":",i,"+",flipped,"=",sum) 
      counter++ 
     } 
    } 
    counter-- 
    fmt.Println(counter) 

} 

答えて

1

リーディングゼロを追加することはできませんいずれかnまたは(n)は逆そこそこ等reverse(n int) int削除リーディングゼロで:

remainder := n % 10 
if first { 
    if remainder == 0 { 
     return 0 
    } 
    first = false 
} 

try this

package main 

import (
    "fmt" 
) 

//breaks down an int number by number and checks to see if 
//all the numbers in the int are odd 
func isOdd(n int) bool { 
    if n <= 0 { 
     return false 
    } 
    for n > 0 { 
     remainder := n % 10 
     if remainder%2 == 0 { 
      return false 
     } 
     n /= 10 
    } 
    return true 
} 

//adds 2 int's passed to it and returns an int 
func add(x int, y int) int { 
    return x + y 
} 

//reverses the int passed to it and returns an int 
func reverse(n int) int { 
    first := true 
    t := 0 
    for n > 0 { 
     remainder := n % 10 
     if first { 
      if remainder == 0 { 
       return 0 
      } 
      first = false 
     } 
     t *= 10 
     t += remainder 
     n /= 10 
    } 
    return t 
} 

func main() { 
    counter := 0 
    for i := 0; i < 1000; i++ { 
     flipped := reverse(i) 
     if flipped == 0 { 
      continue 
     } 
     sum := add(flipped, i) 
     if isOdd(sum) { 
      counter++ 
      //fmt.Println(counter, ":", i, "+", flipped, "=", sum) 
     } 
    } 
    fmt.Println(counter) 
} 

出力:

120 
+0

私が印刷したときにそれが表示されなかったので、私はそれを取り除いていないとは思わなかったターミナル。 –

1

いくつかの正の整数をn 合計が[N + 逆(N)]に構成さ特性を有します完全に奇数(10進)の数字です。たとえば、 36 + 63 = 99および409 + 904 = 1313となります。このような数字は、可逆の番号 とします。したがって、36,63,409および904は可逆的である。先行ゼロは、 はnまたは逆(n)のいずれにも指定できません。

すべての数字はすべて奇数でなければなりません。

は、この方法を試してください。https://play.golang.org/p/aUlvKrb9SB

(あなたが気にしない場合、私はあなたの関数のいずれかを使用しました)

1

あなたが基準のこの部分を無視している:

先行ゼロは、nまたはreverse(n)のいずれでも許可されません。

0にリバーシブルとしてカウントした5つの数字。 (つまり、その逆に先行ゼロがあることを意味します)それらを可逆としてカウントするのをやめ、完了です。

関連する問題