Project Euler#145的循环中有太多结果

时间:2017-09-22 07:17:44

标签: go

我正在尝试为Project Euler #145创建解决方案。我在Go写作。当我运行我的程序时,我得到125的结果。预期的结果是120.我有两种不同的方式我尝试编写代码,但两者都得出了相同的答案。任何指出我的错误的帮助将不胜感激。

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仅使用整数:

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)

}

3 个答案:

答案 0 :(得分:1)

  

一些正整数n具有 sum [n +   reverse(n)]完全由奇数(十进制)数字组成。例如,   36 + 63 = 99和409 + 904 = 1313.我们将拨打此类号码   可逆的;所以36,63,409和904是可逆的。领先的零是   不允许使用n或反向(n)。

总和的所有数字都必须是奇数。

试试这个:https://play.golang.org/p/aUlvKrb9SB

(如果你不介意,我已经使用了你的一个功能)

答案 1 :(得分:1)

你忽略了这部分标准:

  

n或反向(n)中不允许前导零。

0中您认为可逆的五个数字结束。 (这意味着他们的反向具有前导零。)停止计算那些可逆的并且你已经完成了。

答案 2 :(得分: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